SQL 利用存储过程实现对表数据有则更新无则添加

/*建立存储过程*/  
create procedure insertOrUpdate  
@BookName varchar (50),  
@Price float,  
@Publish varchar (50),  
@id int,  
@Storage int,  
@returnValue int output  
as  
  
/*判断该id号的书是否已经存在*/  
if exists (select * from Book where id=@id)    
begin  
/*更新数据*/  
update Book set BookName=@BookName ,Price=@Price where id=@id  
/*设置返回值*/  
set @returnValue=0  
end   
  
else   
begin   
/*插入*/  
insert into Book values(@BookName,@Price,@Publish,@id,@Storage)  
set @returnValue=1  
end  
posted @ 2022-09-29 14:10  ꧁༺星星的轨迹方程式༻꧂  阅读(36)  评论(0)    收藏  举报