-
@@ERROR returns 0 after error
I execute an UPDATE statement using EXEC after I build it into a string:
@lcSQLString = 'UPDATE ' + @@mytable + 'SET x = y ...'
EXEC(@lcSQLString)
When the UPDATE fails, @@ERROR is nevertheless set to 0, because it returns the status of the EXEC command (which succedded) and not the @lcSQLString (which failed). At least, that is the explanation, I think. If I execute the statement directly by substituting the variables with their values, @@ERROR displays the correct value.
How can I capture @@ERROR within EXEC?
Thank you,
Aristotle
-
@@ERROR returns 0 after error (reply)
Gurukiran,
here is a very nice fix that someone suggested to me:
DECLARE @cmd VARCHAR(1000), @ExecError INT
CREATE TABLE #ErrFile (ExecError INT)
SET @cmd = 'SELECT * FROM MyTable WHERE ...' +
'INSERT #ErrFile VALUES(@@ERROR)'
EXEC(@cmd)
SET @ExecError = (SELECT * FROM #ErrFile)
And it works very well!
Aristotle
------------
Gurukiran at 1/29/01 12:39:20 PM
I have come across this situation before, but I can honestly say that I have not been able to find a direct solution. there is a rather involved and circuitous solution ( some may consider it bizarre, but it works ) :
If you can build your update statement and throw it into a table created expressly for this purpose, ( A table with a single varchar 8000 column ) The next line inserted can be an error checking statement like if @@ERROR ...., bcp out the contents of this table (using xp_cmdshell ) into a sql script file on to the hard disk, run xp_cmdshell 'osql.exe -i"file" ' etc, the desired effect is achieved. If you are using this as a part of a transaction, then it might become tougher.
------------
Aristotle Spyropoulos at 1/26/01 4:57:16 PM
I execute an UPDATE statement using EXEC after I build it into a string:
@lcSQLString = 'UPDATE ' + @@mytable + 'SET x = y ...'
EXEC(@lcSQLString)
When the UPDATE fails, @@ERROR is nevertheless set to 0, because it returns the status of the EXEC command (which succedded) and not the @lcSQLString (which failed). At least, that is the explanation, I think. If I execute the statement directly by substituting the variables with their values, @@ERROR displays the correct value.
How can I capture @@ERROR within EXEC?
Thank you,
Aristotle
-
@@ERROR returns 0 after error (reply)
I guess something like this might work for your situation.
declare @lcSQLString nvarchar(4000)
@lcSQLString = 'UPDATE ' + @@mytable + 'SET x = y ...' + char(13) +
'if @@error = 0 begin' + char (13) + '<process statements>' + char(13) +
'end ' + char(13)
'else begin' + '<raiseerror statements>' + char(13) + ' end'
sp_executesql(@lcSQLString)
This way we are evaluating @@error immediately after the execution of the
update statement.
good luck.
Sushruth Nanduri.
------------
Aristotle at 1/29/01 2:13:36 PM
Gurukiran,
This sounds like a complicated process. And, unfortunately, this statement is part of a transaction. All I am trying to accomplish is execute the statement. If it fails, I want to roll back the transaction and discontinue this process.
I am glad that someone else has come across something similar. Perhaps I should experiment with RAISEERROR. Maybe that reports the right error message.
Aristotle
------------
Gurukiran at 1/29/01 12:39:20 PM
I have come across this situation before, but I can honestly say that I have not been able to find a direct solution. there is a rather involved and circuitous solution ( some may consider it bizarre, but it works ) :
If you can build your update statement and throw it into a table created expressly for this purpose, ( A table with a single varchar 8000 column ) The next line inserted can be an error checking statement like if @@ERROR ...., bcp out the contents of this table (using xp_cmdshell ) into a sql script file on to the hard disk, run xp_cmdshell 'osql.exe -i"file" ' etc, the desired effect is achieved. If you are using this as a part of a transaction, then it might become tougher.
------------
Aristotle Spyropoulos at 1/26/01 4:57:16 PM
I execute an UPDATE statement using EXEC after I build it into a string:
@lcSQLString = 'UPDATE ' + @@mytable + 'SET x = y ...'
EXEC(@lcSQLString)
When the UPDATE fails, @@ERROR is nevertheless set to 0, because it returns the status of the EXEC command (which succedded) and not the @lcSQLString (which failed). At least, that is the explanation, I think. If I execute the statement directly by substituting the variables with their values, @@ERROR displays the correct value.
How can I capture @@ERROR within EXEC?
Thank you,
Aristotle
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|