Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SP FOR Loop?

Posted on 2014-11-03
7
Medium Priority
?
138 Views
Last Modified: 2014-11-19
I have 2 tables
1. tblTrip_Master
2. tblTrackingData


The columns are in  trip master are
      [ipkTripID]
      ,[dStartTime]
      ,[dEndTime]
      ,[nStartEventInst]
      ,[nEndEventInst]
      ,[vpkDeviceID]
 

I need a stored procedure to select all records where:  nEndEventInst IS NULL (on tblTrip_Master)
then update all these records in TRIP table with data found in the tblTrackingData table

i.e.
--get correct END ID
set @EndID =  (select top(1) vSequenceId
from tblTrackingData
where vReportId = 8
and  vSequenceId  > tblTrip_Master.nStartEventInst
Order by vSequenceId)

Update   tblTrip_Master
Set nEndEventInst = @EndID
-- , dEndTime = @EndTIme

*I need to also get the EndTime using the first statement, not sure how i would do that without doing separate selects?

Can someone help me get it working/started

I'm not sure how to go about this.... there could be 1 - 100 records in tblTrip_Master that will need updating everytime its run

I hope there is enough info here to explain myself

any help appreciated
0
Comment
Question by:websss
7 Comments
 
LVL 41

Expert Comment

by:Kyle Abrahams
ID: 40419689
0
 

Author Comment

by:websss
ID: 40419704
Thanks, sounds interesting

Performance /locking wise, are there any issues

are there any better ways of doing it?, I just want to know all the options first!
0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 40419706
I will avoid cursors. They aren't good for performance.
Here's the code for first update:
UPDATE tblTrip_Master m
SET nEndEventInst = (SELECT MAX(d.vSequenceId)
			FROM tblTrackingData d
			WHERE d.vReportId = 8 and d.vSequenceId  > m.nStartEventInst)
WHERE m.nEndEventInst IS NULL 

Open in new window

How will you get the EndTime? You can use a similar solution as above one.
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:websss
ID: 40419719
Thanks Vitor

EndTime comes from:
tblTrackingData.GPSDate
And will be on the same row as tblTrackingData.vSequenceId

How would I change the code to reflect this?
0
 
LVL 52

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 1000 total points
ID: 40419735
Well, you need to run two updates. One for each column:
UPDATE tblTrip_Master
SET EndTime = (SELECT MAX(d.vSequenceId)
			FROM tblTrackingData d
			WHERE d.vReportId = 8 and d.vSequenceId  > tblTrip_Master.nStartEventInst)
WHERE nEndEventInst IS NULL 

UPDATE tblTrip_Master
SET nEndEventInst =tblTrackingData.GPSDate
INNER JOIN tblTrackingData ON tblTrackingData.vSequenceId = tblTrip_Master.nEndEventInst

Open in new window

There's no relationship between these two tables?
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 1000 total points
ID: 40419835
UPDATE tm
SET nEndEventInst = td_outer.vSequenceId, dEndTime = td_outer.dEndTime
FROM tblTrip_Master tm
CROSS APPLY (
    SELECT TOP (1) td.vSequenceId, td.dEndTime
    FROM tblTrackingData td
    WHERE
        td.vReportId = 8 and
        td.vSequenceId > tm.nStartEventInst
    ORDER BY td.vSequenceId
) AS td_outer
WHERE
    tm.nEndEventInst IS NULL
0
 
LVL 32

Expert Comment

by:awking00
ID: 40419958
Can you describe tblTrackingData?
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question