Solved

SP FOR Loop?

Posted on 2014-11-03
7
130 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 39

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 45

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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 45

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:
ScottPletcher 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 31

Expert Comment

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

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to select Week Start and Week End dates 5 27
IN with @variable 5 20
sql query Help 12 34
DATETIMEOFFSET feature 1 0
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article I will describe the Copy Database Wizard 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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

746 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

15 Experts available now in Live!

Get 1:1 Help Now