Skip to content

Instantly share code, notes, and snippets.

@nisar1
Last active August 29, 2015 13:57
Show Gist options
  • Save nisar1/9595327 to your computer and use it in GitHub Desktop.
Save nisar1/9595327 to your computer and use it in GitHub Desktop.
Insert Update with single storeprocedure
--TSQL
create procedure uspPhoneNumberInsertUpdateSingleItem
@id int,
@phonenumber nvarchar(20),
@phonenumbertype int,
@contactpersonid int
as
declare @ReturnValue int
if(@id is null) --new Item
begin
insert into phonenumber
(
phonenumber,
phonenumbertype,
contactpersonid
)
values
(
@phonenumber,
@phonenumbertype,
@contactpersonid
)
select @ReturnValue = SCOPE_IDENTITY()
end
else
begin
update phonenumber set
phonenumber = @phonenumber,
phonenumbertype = @phonenumbertype,
contactpersonid = @contactpersonid
where
id = @id
select @ReturnValue = @id
end
if (@@ERROR != 0)
begin
return -1
end
else
begin
return @returnvalue
end
go
--MYSQL
create procedure uspCommonMasterInsertUpdateSingleItem
(
p_id int,
p_name varchar(50),
p_head int,
p_desc varchar(500),
p_ct_nm varchar(50)
)
begin
declare p_ct_cd int ;
set p_ct_cd = (select ct_cd from com_typ where ct_nm = p_ct_nm);
if (p_id is null) then
insert into com_mst(
ct_cd,
cm_nm,
cm_hed,
cm_dsc
)
values
(
p_ct_cd,
p_name,
p_head,
p_desc
) ;
select LAST_INSERT_ID() as returnvalue;
else
update com_mst set
ct_cd=p_ct_cd,
cm_nm=p_name,
cm_hed =p_head,
cm_dsc = p_desc
where cm_cd = p_id ;
select p_id as returnvalue ;
end if ;
end
--MYSQL call procedure
call uspCommonMasterInsertUpdateSingleItem (null,'kk2','1','des','Asset Type') ;
call uspCommonMasterInsertUpdateSingleItem (11,'kk2','1','des','Asset Type') ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment