SQLServer用存储过程实现插入更新数据示例
作者:whsnow 时间:2024-01-27 23:48:05
实现
1)有相同的数据,直接返回(返回值:0);
2)有主键相同,但是数据不同的数据,进行更新处理(返回值:2);
3)没有数据,进行插入数据处理(返回值:1)。
【创建存储过程】
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 --有相同的数据,直接返回值
end
else
begin
update dbo.DemoData set Name=@Name,Telephone=@Telephone,Address=@Address,Job=@Job where id=@Id
set @returnValue=2 --有主键相同的数据,进行更新处理
end
end
else
begin
insert into dbo.DemoData values(@Id,@Name,@Telephone,@Address,@Job)
set @returnValue=1 --没有相同的数据,进行插入处理
end
【执行方式】
declare @returnValue int
exec Insert_Update '15','hugh15','3823345','长安街','副部长',@returnValue output
select @returnValue
返回值0,已经存在相同的
返回值1,插入成功
返回值2,更新成功
标签:存储过程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用Python爬取小姐姐图片(beautifulsoup法)
2022-05-02 04:01:44
![](https://img.aspxhome.com/file/2023/4/133694_0s.png)
MySQL 8.0新特性 — 管理端口的使用简介
2024-01-28 21:53:46
Vue内部渲染视图的方法
2024-04-28 09:19:57
![](https://img.aspxhome.com/file/2023/7/133117_0s.png)
FCKEditor v2.6 编辑器配置图解教程
2024-01-04 22:16:05
![](https://img.aspxhome.com/file/2023/8/70638_0s.jpg)
Mysql5.7如何修改root密码
2024-01-26 21:22:28
ASP 判断是否有中文的代码
2011-04-15 11:07:00
在Python中操作文件之read()方法的使用教程
2021-10-28 19:23:46
关于Youtube URL的十个技巧
2009-04-21 13:19:00
![](https://img.aspxhome.com/file/UploadPic/20094/21/youtubeoffice01cn1-34s.jpg)
一次性压缩Sqlserver2005中所有库日志的存储过程
2024-01-28 13:40:13
Python连接Hadoop数据中遇到的各种坑(汇总)
2023-09-13 20:16:34
python 多线程中join()的作用
2022-11-27 12:24:24
![](https://img.aspxhome.com/file/2023/9/77669_0s.jpg)
asp form 表单验证函数
2011-04-04 11:10:00
详解Python如何制作自动发送微信的程序
2021-06-04 13:24:53
![](https://img.aspxhome.com/file/2023/8/99558_0s.png)
perl几个文件操作例子
2022-03-23 09:24:09
Python完全新手教程
2021-08-19 18:38:06
基于go手动写个转发代理服务的代码实现
2024-04-25 15:11:15
Jupyter notebook运行后打不开网页的问题解决
2021-07-10 01:35:30
![](https://img.aspxhome.com/file/2023/8/118348_0s.jpg)
解决Tensorflow占用GPU显存问题
2021-09-01 14:34:46
![](https://img.aspxhome.com/file/2023/5/81735_0s.jpg)
Python高级应用实例对比:高效计算大文件中的最长行的长度
2021-11-26 05:36:35
python手写均值滤波
2022-03-16 20:53:19
![](https://img.aspxhome.com/file/2023/7/109927_0s.jpg)