Wednesday, 29 January 2014

Handle Exception in Stored Procedure using RAISERROR & @@ERROR in Sql Server

Ans:-

Create proc spCheckException(@id int,@name varchar(30),@sal money,@deptid int,@doj date,@mgrno int)
as
begin
if @deptid>=120
begin
RAISERROR ('You entered %d, the deptid can not be greater than 120.', 10, 1, @deptid)
end
else
begin
begin transaction
insert into Emp(id,name,sal,deptid,doj,mgrno) values(@id,@name,@sal,@deptid,@doj,@mgrno)
if @@ERROR<>0
rollback transaction
else
commit transaction
end
end

exec spCheckException 12,'hhhh',5575,124,'01-21-2014',1

This msg will arise-You entered 124, the deptid can not be greater than 120.

No comments:

Post a Comment