Solved

SP FOR Loop?

Posted on 2014-11-03
7
134 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 49

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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 49

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

763 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