Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know whether a sql Update statement executed successfully or failed?

Tags:

sql-server

How to know whether a sql Update statement executed successfully or failed ?

I use sql server 2005 and C# asp.net.

Can I get the successful or failed infomation in C# without adding some sql code into the old sql statement?

like image 829
Mike108 Avatar asked Jun 03 '10 06:06

Mike108


1 Answers

You can use @@ROWCOUNT to get the number of rows affected by the last query. This can be used to decide whether your WHERE clause actually matched something, for example.

UPDATE mytable SET
  field = 'SomeValue'
WHERE
  id = 1234

IF @@ROWCOUNT = 0
BEGIN
   --  No row with id=1234
END
like image 157
Dean Harding Avatar answered Sep 25 '22 14:09

Dean Harding