简体   繁体   English

更新/插入SQL Server数据库表

[英]Update/Insert SQL Server database table

I have a program written in C# with a SQL Server database. 我有一个用C#编写的带有SQL Server数据库的程序。 In the database there is a table ( ProcessData ) with 3 columns: ID (PK, auto-ID), Start (DateTime), End (DateTime). 在数据库中,有一个表( ProcessData )包含3列: ID (PK,自动ID), Start (日期时间), End (日期时间)。

There are also a lot of threads which will be inserting new rows into the database table ( ProcessData ) every few minutes/seconds. 还有很多线程每隔几分钟/秒将新行插入数据库表( ProcessData )中。

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. 我只想在ID和Start列的开头插入,并在几分钟/秒后根据ID插入End列。

How can I do that? 我怎样才能做到这一点?

Many thanks, 非常感谢,

So you want to insert a new row, capture the newly created ID, and later on update the row?? 因此,您要插入新行,捕获新创建的ID,然后稍后更新该行?

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?? 那是您要找的东西吗?

Your Table ProcessData Contains 您的表ProcessData包含

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)

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 有很多方法可以给猫蒙皮,但这是T-SQL的方法之一

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM