Create proc Insert_Update
@Id varchar(20),
@Name varchar(20),
@Telephone varchar(20),
@Address varchar(20),
@Job varchar(20),
@returnValue int output
as
declare
@tmpName varchar(20),
@tmpTelephone varchar(20),
@tmpJob varchar(20),
@tmpAddress varchar(20)
if exists(select * from dbo.DemoData where id=@Id)
begin
select @tmpName=Name,@tmpTelephone=Telephone,@tmpAddress=Address,@tmpJob=Job from dbo.DemoData where id=@Id
if ((@tmpName=@Name) and (@tmpTelephone=@Telephone) and (@tmpAddress=@Address)and (@tmpJob=@Job))
begin
set @returnValue=0 --有相同的數(shù)據(jù),直接返回值
end
else
begin
update dbo.DemoData set Name=@Name,Telephone=@Telephone,Address=@Address,Job=@Job where id=@Id
set @returnValue=2 --有主鍵相同的數(shù)據(jù),進(jìn)行更新處理
end
end
else
begin
insert into dbo.DemoData values(@Id,@Name,@Telephone,@Address,@Job)
set @returnValue=1 --沒有相同的數(shù)據(jù),進(jìn)行插入處理
end
declare @returnValue int
exec Insert_Update '15','hugh15','3823345','長安街','副部長',@returnValue output
select @returnValue