0

我想将一个包含 20k 个 ID 的数组传递给我的存储过程参数,以便更新某个表。

我不想单独运行 20k 更新查询,而是想运行 1 个查询来更新所有查询,它应该会提高我的性能。

任何人都知道我可以将参数传递给我的存储过程吗?

我知道 NVARCHAR(MAX) 限制为 8000 个字符,是否有可能使用存储的 proc 参数发送如此庞大的数据?

4

1 回答 1

1

请改用表值参数。请参阅使用表值参数(数据库引擎)。顾名思义,TVP 就是一个参数表。您从您的客户端代码中为其分配一个 DataTable,并且该过程(或您的临时 SQL 代码)接收整个 DataTable 作为参数。这是 MSDN 复制的示例:

// Assumes connection is an open SqlConnection.
using (connection)
{
// Create a DataTable with the modified rows.
DataTable addedCategories = CategoriesDataTable.GetChanges(
    DataRowState.Added);

// Define the INSERT-SELECT statement.
string sqlInsert = 
    "INSERT INTO dbo.Categories (CategoryID, CategoryName)"
    + " SELECT nc.CategoryID, nc.CategoryName"
    + " FROM @tvpNewCategories AS nc;"

// Configure the command and parameter.
SqlCommand insertCommand = new SqlCommand(
    sqlInsert, connection);
SqlParameter tvpParam = insertCommand.Parameters.AddWithValue(
    "@tvpNewCategories", addedCategories);
tvpParam.SqlDbType = SqlDbType.Structured;
tvpParam.TypeName = "dbo.CategoryTableType";

// Execute the command.
insertCommand.ExecuteNonQuery();
}
于 2012-09-06T21:25:56.917 回答