I execute the following code via CLR, is there a reason why the message is not printed to the SQL Server, does it need to wait until the Stored Procedure returns all the rows (there is about 7 Billion rows to return)
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();
}
My Stored Procedure:
SELECT ProductId FROM ProductTable
WHERE ProductInfoId IN
(
SELECT ProductInfoId from DeletedProducts
)
Here’s how you delete 7 billion rows using a nice set based operation. You don’t
abuseiterate through a datareader in CLR.For more, see this question Bulk DELETE on SQL Server 2008
But to answer your question, yes, SQL Server will not PRINT (which is what you’re doing) immediately