create proc sp_user
(
@userid int,
@username varchar(20)
)
as
begin
insert into tbl_ins(userid, username) values(@userid,@username)
end
This is your code, i have some suggesstion to you
1) Never create a procedure with The starting letter of SP_ see the reason
here[
^]
2) where is the try catch?
To answer your problem you have two way
1) Create a unique constraint(Key) on the column USername in the table tbl_ins
You don't need a extra crap code here?
2)(well some one say that i will maintain my code in my Proc only)inside the proc with extra code here
CREATE PROC sp_user (@userid INT ,@username VARCHAR (20))
AS
BEGIN
IF EXISTS ( SELECT 1
FROM tbl_ins
WHERE UserName = @username )
BEGIN
RAISERROR ( 'UserName %s Already Exists.',
16,
1,
@username
);
END
ELSE
BEGIN
INSERT INTO tbl_ins
(userid, username)
VALUES
(@userid, @username)
END
END
i think now you are clear, reply me what's your solution either 1 or 2