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.
| Author |
Topic |
|
under2811
Constraint Violating Yak Guru
366 Posts |
Posted - 2006-02-09 : 09:13:37
|
| Hi..I have error handling query..like-------------------create table emp (ID int primary key,Name varchar(4))-------------------begin tran Insert into emp values (1,'SS') Insert into emp values (2,'DA') Insert into emp values (3,'BA') Insert into emp values (1,'SD')IF (@@error <> 0) GOTO ErrorHandlercommit tranErrorHandler:If (@@error <> 0) Begin declare @MsgErr as varchar(8000) select @MsgErr = description from dbo.sysmessages where error = @@errorPrint 'Duplicate' Rollback tran end....But what is happening here I am getting error message of duplicate..but all other records got processed..What I want here is as if there is any duplicate records found all the transaction should get rolled back..and also i am not getting print 'Duplicate' statement can anybody have any idea what is wrong with query??T.I.A |
|
|
mallier
Starting Member
24 Posts |
Posted - 2006-02-09 : 09:25:30
|
| --U have to check error after every insert statement-- seee below codebegin trandeclare @err intdeclare @MsgErr as varchar(8000)Insert into emp values (1,'SS')select @err=@@errorIF (@err <> 0) GOTO ErrorHandlerInsert into emp values (2,'DA')select @err=@@errorIF (@err <> 0) GOTO ErrorHandlerInsert into emp values (3,'BA')select @err=@@errorIF (@err <> 0) GOTO ErrorHandlerInsert into emp values (1,'SD')select @err=@@errorIF (@err <> 0) GOTO ErrorHandlergoto EndSaveErrorHandler: if (@@trancount>0) rollback select @MsgErr = description from dbo.sysmessages where error = @@error Print 'Duplicate' EndSave: if (@@trancount>0) commit trancheers,http://mallier.blogspot.com |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-02-09 : 09:30:42
|
From Books Onlinequote: @@ERRORReturns the error number for the last Transact-SQL statement executed.
Which means - If (@@error <> 0) will be 0- where error = @@error will also be 0----------------------------------'KH'everything that has a beginning has an end |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-02-09 : 09:34:08
|
quote: select @MsgErr = description from dbo.sysmessages where error = @@error
Should be master..sysmessages (unless you are running your query in master database). Also change the @@error to @err as mallier suggested----------------------------------'KH'everything that has a beginning has an end |
 |
|
|
under2811
Constraint Violating Yak Guru
366 Posts |
Posted - 2006-02-09 : 10:32:01
|
| thanx ppl :) :) |
 |
|
|
|
|
|
|
|