Solved

SP FOR Loop?

Posted on 2014-11-03
7
133 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 40

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 47

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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 47

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 250 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 69

Accepted Solution

by:
Scott Pletcher earned 250 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

813 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now