Hi friends
I've written tow type
of code which do the same work, but I want to know which one is better? 1 or 2? Why?
I mean about Try & Catch.
Please Help me.
1.
ALTER Procedure [dbo].[Clerk_Insert]
@clk_Name nvarchar(80),
@clk_Family nvarchar(100),
@clk_FatherName nvarchar(80),
@clk_MeliCode char(10),
@clk_ShomarehShenasname char(10),
@clk_PersonnelNumber nvarchar(100)
As
Begin
Begin Try
Begin Tran a;
If exists(select clk_MeliCode, clk_Name, clk_Family, clk_FatherName, clk_PersonnelNumber
from Clerk
where (clk_MeliCode = @clk_MeliCode)
or (clk_Name= @clk_Name and clk_Family= @clk_Family and clk_FatherName = @clk_FatherName )or( clk_PersonnelNumber
=@clk_PersonnelNumber))
Begin
return -1;
End
else
begin
insert into Clerk
(clk_Name, clk_Family, clk_FatherName,clk_MeliCode, clk_ShomarehShenasname, clk_PersonnelNumber)
values
(@clk_Name, @clk_Family,@clk_FatherName,@clk_MeliCode,@clk_ShomarehShenasname, @clk_PersonnelNumber)
End
commit Tran a;
End Try
Begin Catch
Rollback Tran a;
select
ERROR_LINE() as
line, ERROR_MESSAGE() as messages;
End Catch
End
2.
ALTER Procedure [dbo].[Clerk_Insert]
@clk_Name nvarchar(80),
@clk_Family nvarchar(100),
@clk_FatherName nvarchar(80),
@clk_MeliCode char(10),
@clk_ShomarehShenasname char(10),
@clk_PersonnelNumber nvarchar(100)
As
Begin
If exists(select clk_MeliCode, clk_Name, clk_Family, clk_FatherName, clk_PersonnelNumber
from Clerk
where (clk_MeliCode = @clk_MeliCode)
or (clk_Name= @clk_Name and clk_Family= @clk_Family and clk_FatherName = @clk_FatherName )or( clk_PersonnelNumber
=@clk_PersonnelNumber))
Begin
return -1;
End
else
begin
insert into Clerk
(clk_Name, clk_Family, clk_FatherName,clk_MeliCode, clk_ShomarehShenasname, clk_PersonnelNumber)
values
(@clk_Name, @clk_Family,@clk_FatherName,@clk_MeliCode,@clk_ShomarehShenasname, @clk_PersonnelNumber)
End
End
Begin Try
Begin Tran a;
execute dbo.Clerk_Insert;
commit tran a;
End Try
Begin Catch
select
ERROR_NUMBER() as
Error_Numbers,
ERROR_LINE() as Error_Lines,
ERROR_MESSAGE() as
Error_Messages;
rollback tran a;
End Catch;
go