sql存储过程详解
作者:hebedich 时间:2024-01-22 07:00:39
1,不带参数的存储过程
2,带输入参数的存储过程
3,带输入和输出参数的存储过程
4,带返回值的存储过程
不带参数的存储过程
例如,以下存储过程返回Employees表中所有职员的记录。
存储过程代码:
USE TSQLFundamentals2008;
GO
IF OBJECT_ID('usp_ProcDemoNoParam','P') IS NOT NULL DROP PROC usp_ProcDemoNoParam;
GO
-- 1,不带参数
CREATE PROC usp_ProcDemoNoParam
AS
BEGIN
SELECT * FROM HR.Employees;
END
GO
调用代码:
USE TSQLFundamentals2008;
GO
-- 1,不带参数存储过程的调用
EXEC usp_ProcDemoNoParam;
结果:
可以看到,共返回了9条记录。
带输入参数的存储过程
例如,该存储过程接受输入参数@empid,然后返回这个职员的信息。
创建存储过程代码:
IF OBJECT_ID('usp_ProcDemoWithInputParam','P') IS NOT NULL DROP PROC usp_ProcDemoWithInputParam;
GO
-- 2,带输入参数
CREATE PROC usp_ProcDemoWithInputParam
@empid AS INT
AS
BEGIN
SELECT * FROM HR.Employees
WHERE empid= @empid;
END
GO
调用:
-- 2,带输入参数存储过程调用
EXEC usp_ProcDemoWithInputParam @empid=5;
结果:
带输入和输出参数的存储过程
例如,以下存储过程接受@empid即职员ID作为输入参数,然后返回该职员的信息,同时返回代码受影响行数作为输出参数。
创建存储过程代码:
IF OBJECT_ID('usp_ProcDemoWithInputOutputParam','P') IS NOT NULL DROP PROC usp_ProcDemoWithInputOutputParam;
GO
-- 3,带输入和输出参数
CREATE PROC usp_ProcDemoWithInputOutputParam
@empid AS INT,
@NumRowsAffected AS INT OUTPUT
AS
BEGIN
SELECT * FROM HR.Employees
WHERE empid= @empid;
SET @NumRowsAffected= @@ROWCOUNT; -- 赋值,也可以使用select赋值
END
GO
调用:
-- 3,带输入和输出参数存储过程的调用
DECLARE @nums AS INT;
EXEC usp_ProcDemoWithInputOutputParam @empid=5,@NumRowsAffected= @nums OUTPUT;
SELECT @nums AS nums;
结果:
带返回值的存储过程
例如,以下存储过程接受@empid即职员ID作为输入参数,然后判断职员表中是否存在该职员的记录,如果存在则返回1,否则返回0.
创建存储过程代码:
IF OBJECT_ID('usp_ProcDemoWithReturnValue','P') IS NOT NULL DROP PROC usp_ProcDemoWithReturnValue;
GO
-- 4,带返回值
CREATE PROC usp_ProcDemoWithReturnValue
@empid AS INT
AS
BEGIN
IF EXISTS (SELECT * FROM HR.Employees WHERE empid=@empid)
RETURN 1
ELSE
RETURN 0; -- 也可以声明一个变量,然后返回这个变量
END
GO
调用:
-- 4,带返回值存储过程的调用
DECLARE @status AS INT=0; --给默认值为0
EXEC @status= dbo.usp_ProcDemoWithReturnValue @empid = 5 -- int
SELECT @status AS thestatus;
结果:
标签:sql,存储过程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
vue实现菜单切换功能
2024-05-03 15:12:08
最简洁的asp多重查询的解决方案
2011-04-15 10:50:00
mysql 服务意外停止1067错误解决办法小结
2024-01-26 05:56:38
![](https://img.aspxhome.com/file/2023/7/112877_0s.gif)
简单三步轻松实现ORACLE字段自增
2024-01-16 06:06:58
Pytorch:dtype不一致问题(expected dtype Double but got dtype Float)
2023-07-05 21:57:33
![](https://img.aspxhome.com/file/2023/1/66281_0s.png)
Python实现普通图片转ico图标的方法详解
2023-08-13 07:09:36
![](https://img.aspxhome.com/file/2023/0/99600_0s.jpg)
pyinstaller打包python3.6和PyQt5中各种错误的解决方案汇总
2021-01-13 18:58:48
Mobile Web下的编码设计
2010-01-28 10:42:00
![](https://img.aspxhome.com/file/UploadPic/20101/28/mobile-web-75s.jpg)
Numpy数组array和矩阵matrix转换方法
2021-06-25 06:17:26
浅谈tensorflow1.0 池化层(pooling)和全连接层(dense)
2021-11-09 05:06:17
MySQL UPDATE 语句的非标准实现代码
2024-01-16 19:08:57
Oracle 8i在P4上的安装
2010-07-31 13:19:00
PHP类的特性实例分析
2024-05-03 15:49:46
Go语言集成开发环境之VS Code安装使用
2023-08-29 13:06:38
![](https://img.aspxhome.com/file/2023/9/101039_0s.png)
mysql遇到load data导入文件数据出现1290错误的解决方案
2024-01-29 09:53:12
![](https://img.aspxhome.com/file/2023/0/89930_0s.png)
从语义开始–概念、意义、实践
2010-06-13 18:06:00
![](https://img.aspxhome.com/file/UploadPic/20106/13/01-73s.jpg)
tensorflow实现tensor中满足某一条件的数值取出组成新的tensor
2021-02-18 02:09:22
![](https://img.aspxhome.com/file/2023/3/131433_0s.jpg)
Python3 使用selenium插件爬取苏宁商家联系电话
2023-12-20 01:39:57
kafka-python批量发送数据的实例
2023-04-24 04:40:23
什么样的分页案例才是好的
2007-11-23 19:08:00
![](https://img.aspxhome.com/file/UploadPic/up/2007112319363210.jpg)