繁体   English   中英

从另一个表中插入多个行,并获取SQL Server 2000中插入的行的ID

[英]Insert multiple rows from another table and get the IDs of inserted rows in SQL Server 2000

我有表A和表B,表B具有A的相同列,并且只有一个额外的x列,我想从A到B插入一些行,然后想用一个新行更新这些最后插入的行中的每一行x列的值。

如何在SQL Server 2000中做到这一点?

现在,我在C#代码中使用此查询,我从A中选择了所有需要的行,并将其保存在DataTable

string Query = "Select * from A where ID = 5";

SqlDataAdapter dap = new SqlDataAdapter(Query, Conn);
dap.Fill(DataTablle1);

foreach (DataRow row in DataTablle1.Rows)
{
      string query2 = "Insert into B (col a , col b, col x) values ('" + row["col a"] + "','" + row["col b"] + "',3)";
}

这种方法需要很多插入语句,具体取决于表A中的行数,有什么方法可以用最少的插入语句来实现?

您可能只有一个带有文字3的insert-select语句:

INSERT INTO b ([col a] , [col b], [col x])
SELECT [col a], [col b], 3
FROM   a 
WHERE  id = 5

暂无
暂无

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

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