What value should .net SqlCommand.ExecuteNonQuery() return if no rows are affected?

Posted by peacedog on Stack Overflow See other posts from Stack Overflow or by peacedog
Published on 2010-05-27T14:13:38Z Indexed on 2010/05/27 14:31 UTC
Read the original article Hit count: 191

Filed under:
|
|

I have the following code:

int result = -1;
StringBuilder sb = new StringBuilder();
SqlCommand cmd = MyConnection.
sb.AppendLine("delete from Table1 where ID in");
sb.AppendLine("(select id from Table1 t1 where  not exists(select * from Table2 t2 where t2.Table1ID = t1.ID))");
cmd.CommandText = sb.ToString();
result = cmd.ExecuteNonQuery();
_log.Info("StoredXMLDocument Records Deleted: " + result.ToString());

That SQL, in a more readable format, is:

delete from Table1 where ID in
(select id from Table1 t1 where  not exists(select * from Table2 t2 where t2.Table1ID = t1.ID))

I know that the SQL, when executed directly in the database, deletes no rows. When this code runs, however, result gets a value of 1. I was expecting it to be 0. Am I missing something? Why is it 1?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about sql