mysql stored procedures with return values
存储过程的功能非常强大,在某种程度上甚至可以替代业务逻辑层,
接下来就一个小例子来说明,用存储过程插入或更新语句。
1、数据库表结构
所用数据库为Sql Server2008。
2、创建存储过程
(1)实现功能:1)有相同的数据,直接返回(返回值:0);
2)有主键相同,但是数据不同的数据,进行更新处理(返回值:2);
3)没有数据,进行插入数据处理(返回值:1)。
根据不同的情况设置存储过程的返回值,调用存储过程的时候,根据不同的返回值,进行相关的处理。
(2)下面编码只是实现的基本的功能,具体的Sql代码如下:
Create proc sp_Insert_Student
@No char(10),
@Name varchar(20),
@Sex char(2),
@Age int,
@rtn int output
as
declare
@tmpName varchar(20),
@tmpSex char(2),
@tmpAge int
if exists(select * from Student where No=@No)
begin
select @tmpName=Name,@tmpSex=Sex,@tmpAge=Age from Student where No=@No
if ((@tmpName=@Name) and (@tmpSex=@Sex) and (@tmpAge=@Age))
begin
set @rtn=0 --有相同的数据,直接返回值
end
else
begin
update Student set Name=@Name,Sex=@Sex,Age=@Age where No=@No
set @rtn=2 --有主键相同的数据,进行更新处理
end
end
else
begin
insert into Student values(@No,@Name,@Sex,@Age)
set @rtn=1 --没有相同的数据,进行插入处理
end
execute:
declare @rtn int
exec sp_Insert_Student '1101','张三','男',23,@rtn output
if @rtn=0
print '已经存在相同的。'
else if @rtn=1
print '插入成功。'
else
print '更新成功'
转载于:https://blog.51cto.com/greenp/958650
mysql stored procedures with return values相关推荐
- Calling Oracle stored procedures from Microsoft.NET
摘自:http://www.c-sharpcorner.com/UploadFile/john_charles/CallingOraclestoredproceduresfromMicrosoftdo ...
- Spring, Hibernate and Oracle Stored Procedures
一篇英文博文,写的是利用hibernate处理存储过程中的游标等等: Motivation: While there are a few resources available online for ...
- 自动备份SQL Server数据库中用户创建的Stored Procedures
为了避免意外丢失/损坏辛苦创建的Stored Procedures,或者想恢复到以前版本的Stored Procedures,这样提供了一个有效方法,可以自动将指定数据库中的Stored Proced ...
- smartctl command's RETURN VALUES
smartctl 的返回值是由8个比特位组成的 , 具体的解释参考本为末尾部分. 使用smartctl -H可以根据磁盘已经收集的信息检测磁盘的监控状态.返回的是一个数值类型. 返回值具体代表什么意思 ...
- QMetaMethod::invoke: Unable to invoke methods with return values in queued connections
报错信息为: QMetaMethod::invoke: Unable to invoke methods with return values in queued connections 解决方法是: ...
- 【QML】C++访问QML函数获取返回值时报:Unable to invoke methods with return values in queued connections
QMetaMethod::invoke: Unable to invoke methods with return values in queued connections 相关资料可以参考:http ...
- The return types for the following stored procedures could not be detected
1.使用dbml映射数据库,添加存储过程到dbml文件时报错. 2.原因:存储过程中使用了临时表 3.解决方案 3.1 通过自定义表值变量实现 Ex: DECLARE @TempTable TABLE ...
- mysql+inser+select_在MySQL中结合INSERT,VALUES和SELECT
您可以使用以下语法组合插入,值和选择语句insert into yourFirstTableName(yourColumnName1,yourColumnName2,.......N) select ...
- SQL Server :Stored procedures存储过程初级篇
对于SQL Server,我是个拿来主义.很多底层的原理并不了解,就直接模仿拿着来用了,到了报错的时候,才去找原因进而逐步深入底层.我想,是每一次的报错,逼着我一点点进步的吧. 近期由于项目的原因,我 ...
最新文章
- 使用Crypto++的AES GCM对称加密
- [转]select、poll、epoll的比较
- 机器学习系列之神经网络入门基础知识
- 【Kafka】kafka 消费者组静态成员(static consumer member)
- Spring MVC PathVariable
- 1分钟获取上千ID,暴力破解、端口扫描、拖库攻击如何防范
- 每个设计师都应该了解的IOS编年史
- 计算机桌面美化软件,桌面美化软件哪个好_桌面美化软件排行榜【最新】-太平洋电脑网...
- 阿里云商标注册查询入口(支持图形检索/45分类注册风险)
- 学籍管理系统c语言项目作业,C语言实现学生学籍管理系统
- PyMol2.5的基础使用
- 水溶性ZnCdS/ZnS量子点蓝光PL400nm-480nm
- cad直线和圆弧倒角不相切_CAD倒角技巧
- python+selenium设置chrome代理的方式
- maya如何导入多片段动画文件查看和编辑
- 计算机桌面文件能单独设密码吗,电脑上文件夹怎么加密_桌面文件夹怎么加密码...
- 利用C++逆向知识写CS辅助,轻松实现透视原理!
- postfix mysql 虚拟用户_postfix+extmail+mysql虚拟用户邮件体系的搭建
- 越狱 第四季(1-3)
- python: float64与float32转换、压缩比较与转换偏差