我通过CLR执行以下代码,是否有消息没有打印到SQL Server的原因,是否需要等待存储过程返回所有行(返回大约7亿行)< / p>
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "spCommand_Select_Rows_For_Delete";
cmd.CommandTimeout = 41600;
SqlDataReader reader = cmd.ExecuteReader();
try
{
string strSQL = "";
SqlContext.Pipe.Send(DateTime.Now.ToString() + " - Started working with ProductTable");
while (reader.Read())
{
strSQL = "DELETE FROM ProductTable WHERE ProductId = " + reader["ProductId"].ToString();
SqlCommand cmdDelete = new SqlCommand(strSQL, conn);
cmdDelete.Connection = conn;
cmdDelete.CommandTimeout = 20800;
cmdDelete.ExecuteNonQuery();
}
SqlContext.Pipe.Send(DateTime.Now.ToString() + " - Completed working with ProductTable");
}
finally
{
// Always call Close when done reading.
reader.Close();
}
我的存储过程:
SELECT ProductId FROM ProductTable
WHERE ProductInfoId IN
(
SELECT ProductInfoId from DeletedProducts
)
答案 0 :(得分:3)
以下是使用基于集合的操作删除70亿行的方法。您没有 abuse 遍历CLR中的datareader。
SELECT 'Starting'
WHILE ROWCOUNT <> 0
DELETE TOP (1000000) P
FROM ProductTable P
WHERE EXISTS (
SELECT * from DeletedProducts DP WHERE P.ProductInfoId = DP.ProductInfoId
)
有关详情,请参阅此问题Bulk DELETE on SQL Server 2008
但要回答你的问题,是的,SQL Server不会立即PRINT(这就是你正在做的事情)
答案 1 :(得分:1)
你可以使用SqlContext.Pipe.ExecuteAndSend和RAISERROR WITH NOWAIT来做你想要的消息。
但我对gbn的答案是不需要CLR进行批量删除。