I want to update a record in database if a condition exists .
set nocount off;
if exists ( select * from ParentTable where ID=@ParentID )
update ChildTable set Title=@Title,ParentID=@ParentID where ID=@ID;
else return -2;
but it returns -1 if the ParentID doesn’t exists .
I’m using ExecuteNonQuery() to run this procedure
The return value of
ExecuteNonQueryis the number of rows affected, not a custom return code. While-1(the current return value) seems like a suitable replacement in this particular case, to solve the more general case you’ll have to useExecuteScalarand use the pattern below: