Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 @@error CHECKING

Author  Topic 

salmonraju
Yak Posting Veteran

54 Posts

Posted - 2006-10-26 : 03:39:35
hi
here in my code contains 3 sqlcommands
1st executes well
2nd one will raise error because it mger is not matched with current row
3rd also raise error because row doesnt exist
PLS GO THROUGH BELOW CODE

BUT MY @ERROR IN THE 2ND SQL CONTAINS 0 INSTEAD SOME VALUE ,WHY IT HAPPENED SIMILAR TO 3RD


create proc errChk
as
delete from Employees where DepartmentID=3
IF @@ERROR <> 0
BEGIN
RAISERROR ('Error in deleting employees in DeleteDepartment.', 16, 1)
--return
end
update Employees set mger='ooo' where DepartmentID=1 and mger='ravi'
IF @@ERROR <> 0
BEGIN
RAISERROR ('Error in updating employees in DeleteDepartment.', 16, 1)
--return
end
delete from Employees where DepartmentID=10
IF @@ERROR <> 0
BEGIN
RAISERROR ('Error in deleting employees in DeleteDepartment.', 16, 1)
--return
end

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-10-26 : 03:43:37
Raiserror gives an error back to front end application over the connection variable.
Also, why do you think a DELETE statement raises an error? Maybe you should use @@ROWCOUNT?



Peter Larsson
Helsingborg, Sweden
Go to Top of Page

salmonraju
Yak Posting Veteran

54 Posts

Posted - 2006-10-26 : 04:28:24
hi peso
in book it is said that the usage of @@ERROR is check whether the statements has any errors
this code is taken from a site it is not written by me
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-10-26 : 04:44:41
I think you have to read the article in the book again.
An UPDATE statement that does not update a row because of wrong WHERE condition, does NOT raise an error. @@ROWCOUNT will be zero and so will @@ERROR.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

salmonraju
Yak Posting Veteran

54 Posts

Posted - 2006-10-26 : 07:44:42
Thank u Mr.Peter Larsson,
will u sugggest me in what cases/situation @@error will be non zero
what is the appropriate scenario to use @@ERROR
I saw almost all programers are using @@error to check for errors,actually i have less knowledge about this please tell any information on this
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-10-26 : 07:49:34
one of the case can be, possibly that you are trying to insert or update the record into the table and there is an referantial indegrity problem and you will have the value of @@Error as Non -Zero.

Secondly while inserting/updating the records, the column has the not null constraint and you are trying to insert the null value then @@Error will have non-zero value.


Chirag

http://chirikworld.blogspot.com/
Go to Top of Page
   

- Advertisement -