Discription about following Sp:-(I know its too late to answer but plz check the following approach)
It lets the user decide to update or insert. if user inter @id = 0 he is callinf this Sp to insert and if @id = 0 = non zero then he is willing to update..
ALTER procedure [dbo].[USP_Insert_Update_User](@Id int,@chvEndUserTypeId varchar(3),@chvName varchar(50), @chvPhone1 varchar(14),@chvPhone2 varchar(14),@chvMobile1 varchar(14),@chvMobile2 varchar(14),@chvAddress varchar(500),@chvEmailId varchar(50),@intSchoolId int,@IsActive int,@intout int output)
as
Begin
If(@Id = 0)
if exists(select EmailId from TblUser where( Mobile1 = @chvMobile1 and SchoolId = @intSchoolId) or (SchoolId = @intSchoolId and UserTypeId = 'A') )
set @intout =-1
else
begin
begin try
INSERT INTO SBTS_TestingDB.[dbo].[TblUser]
([UserTypeId]
,[Name]
,[Phone1]
,[Phone2]
,[Mobile1]
,mobile2
,[Address]
,[EmailId]
,[SchoolId]
,Password,IsActive )---0=inactive;1=active
VALUES(@chvEndUserTypeId,@chvName,@chvPhone1,@chvPhone2,@chvMobile1,@chvMobile2,@chvAddress,@chvEmailId,@intSchoolId,@chvMobile1,@IsActive)
set @intout=SCOPE_IDENTITY()
end try
begin catch
set @intout=-2 -- not insertd
end catch
end
else
begin
begin try
UPDATE SBTS_TestingDB.[dbo].[TblUser]
SET [Name] = @chvName
,[Phone1] = @chvPhone1
,[Phone2] = @chvPhone2
,[Mobile1] = @chvMobile1
,[Mobile2] = @chvMobile2
,[Address] = @chvAddress
,[EmailId] = @chvEmailId
,Isactive = @IsActive
WHERE Id = @Id
set @intout = @Id --No need to update CodeIf
end try
begin catch
set @intout = -3 --neot updated
end catch
end
End