menacheb
menacheb

Reputation: 485

Update/Insert SQL Server database table

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,

Upvotes: 0

Views: 722

Answers (3)

Ajay
Ajay

Reputation: 6590

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)

Upvotes: 0

marc_s
marc_s

Reputation: 755361

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

Upvotes: 1

Kevin Ross
Kevin Ross

Reputation: 7215

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

Upvotes: 0

Related Questions