0

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,

3 Answers 3

1

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??

Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

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

Your Table ProcessData Contains

ID(PK, auto-ID)
Start(DateTime)
End(DateTime)

Query

Insert Into ProcessData(Start) values (your value)

After some time

Update ProcessData Set End=(your value) where ID= (your ID)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.