Flo*_*oyd 8 c# sql-server sql-server-2000 sql-server-2008
我有一个SQL查询的int参数的大列表:
update mytable set col='xyz'
where id in (/* thousands of ints */)
Run Code Online (Sandbox Code Playgroud)
我的问题是在SQL Server 2000中有参数限制.我也可以在SQL Server 2008上运行此查询.
有什么更好的方法来做到这一点.
编辑:
Ids列表来自C#程序.不是从另一张桌子.
对我来说最好的解决方案是SQL Server 2008:表值参数
100000 个 Id 需要 14-20 秒,1000 个 Id 需要约 140 毫秒。
sql = @"
update MyTable
set Col1 = 1
where ID in (select * from @ids)
";
sqlCmd = new SqlCommand {Connection = _sqlConn, CommandText = sql};
//Create a DataTable with one Column("id") and all ids as DataRows
DataTable listOfLeadIDs = new DataTable();
listOfIDs.Columns.Add("id", typeof(int));
Ids.ToList<string>().ForEach(x => listOfIDs.Rows.Add(new object[] { int.Parse(x) }));
//Bind this DataTable to the Command-object
// Node: "IntTable" is an User-Defined-Table-Typ (new feature with SQL-2008)
sqlCmd.Parameters.Add(
new System.Data.SqlClient.SqlParameter("@ids", listOfIDs) {
TypeName = "IntTable"
});
//Execute the Query
sqlCmd.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)
用户定义的表类型:
CREATE TYPE [dbo].[IntTable] AS TABLE(
[id] [int] NULL
)
GO
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4381 次 |
| 最近记录: |