all groups > sql server programming > april 2004 >
You're in the

sql server programming

group:

How can I tell if the SQL Statement was successful in MS SQL Server program


Re: How can I tell if the SQL Statement was successful in MS SQL Server program Louis Davidson
4/1/2004 10:16:18 PM
sql server programming: Generally we check @@error to see if anything abnormal occurred. 0 is good,
1-49999 a system error occured (constraint violation, index violation,
hardware error, etc) 50000 up are user defined and/or ad hoc errors from a
raiserror statement.

--
----------------------------------------------------------------------------
Louis Davidson (drsql@hotmail.com)
Compass Technology Management

Pro SQL Server 2000 Database Design
http://www.apress.com/book/bookDisplay.html?bID=266

Note: Please reply to the newsgroups only unless you are
interested in consulting services. All other replies will be ignored :)

[quoted text, click to view]

How can I tell if the SQL Statement was successful in MS SQL Server program Ravi
4/1/2004 10:44:29 PM
In DB2, SQLCODE = 100 and SQLCODE = 0 are the key validations one would
make after an SQL statement to see if the query was run successfully or not.
If the query returned any rows or not. Is there an equivalent syntax in MS
SQL ?

Re: How can I tell if the SQL Statement was successful in MS SQL Server program Aaron Bertrand [MVP]
4/2/2004 12:43:31 AM
You can check @@error for errors, and @@rowcount for number of rows affected
by the statement. If they aren't what you expect, act accordingly.

Note that any statement resets these values, so you'll want to capture them
to local variables immediately, and at the same time if you want both. e.g.

DECLARE @err INT, @rc INT
-- statement here that may cause error
SELECT @err = @@ERROR, @rc = @@ROWCOUNT

--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/




[quoted text, click to view]

Re: How can I tell if the SQL Statement was successful in MS SQL Server program Ravi
4/2/2004 6:55:44 AM
Thank you. I cannot say the select did not find any since @@error was 0
even when the select did not find any row for the criteria requested. I
will try with @@rowcount approach. Probably, that should have 0 to tell
me there were none selected.

- Ravi
[quoted text, click to view]

Re: How can I tell if the SQL Statement was successful in MS SQL Server program Aaron Bertrand [MVP]
4/2/2004 12:49:26 PM
That's because an empty result is not considered an error.

--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/


[quoted text, click to view]

AddThis Social Bookmark Button