tags:

views:

41

answers:

2

Hi,

I have a program written in C# with a SQL Server database. In the database there is a table (ProcessData) with 3 columns: ID (PK, auto-ID), Start (DateTime), End (DateTime).

There are also a lot of threads which will be inserting new rows into the database table (ProcessData) every few minutes/seconds.

I want to insert at the beginning only the ID and the Start columns, and after few minutes/seconds ACCORDING TO THE ID, add the End column.

How can I do that?

Many thanks,

A: 

Unless I’m missing something you could just do one insert statement at the start

INSERT INTO tblProcessData (Start) VALUES (@Start)

Then later an update statement

UPDATE tblProcessData SET End=@End WHERE ID=@ID

There are many ways to skin a cat but that is one of the T-SQL ways

Kevin Ross
Ok, than the all mighty question: How to get the ID from an insert statement?
Oliver
SCOPE_IDENDITY () function contains the last inserted ID of a scope. Select for it ;)
TomTom
The answer left by marc_s is a more complete version of mine which includes returning the ID etc
Kevin Ross
+1  A: 

So you want to insert a new row, capture the newly created ID, and later on update the row??

Something like this

DECLARE @NewID INT

INSERT INTO dbo.tblProcessData(Start) VALUES (@Start)
SELECT @NewID = SCOPE_IDENTITY()

and later on:

UPDATE dbo.tblProcessData 
SET End = @End 
WHERE ID = @NewID

Is that what you're looking for??

marc_s