存储过程示例整理

--列出服务器上所有的数据库
exec sp_databases

--改数据库的名字
exec sp_renamedb 'QQDB', 'QQ'

--查看表users中的列
exec sp_columns users

《此组件已作为此服务器安全配置的一部分而被关闭》的解决办法

use master
exec sp_configure 'show advanced options',1  --显示高级配置信息
go
reconfigure --重新配置
go
exec sp_configure 'xp_cmdshell',1  --打开xp_cmdshell选项
go
reconfigure --重新配置
go

--在d盘下面创建目录testproc
exec xp_cmdshell 'mkdir d:	estproc',no_output

-----//用户自定义存储过程///-------
---1.创建无参数的存储过程※
use QQDB
--需求:查询李四账户的余额,如果大于500,输出大于500,否则输出小于500
go
create  proc usp_findPriceByLs
as
--sql语句
declare @yue int
select @yue = price from bank where name = '李四'
if(@yue>500)
begin
print '大于500'
end
else
begin
print '小于500'
end


---调用存储过程
exec usp_findPriceByLs

执行结果:

----------------------查询user表中的所有信息的存储过程
if exists (select * from sysobjects where name = 'usp_FindUsersAll')
drop proc usp_FindUsersAll
go
create proc usp_FindUsersAll
as
select * from users
go




--调用存储过程
exec usp_FindUsersAll

执行结果:

---------///创建带输入参数的存储过程///-----
--需求:根据姓名和地址查询用户信息
if exists (select * from sysobjects where name = 'ups_findUsersByUnameAndUaddress')
drop proc ups_findUsersByUnameAndUaddress
go
create proc ups_findUsersByUnameAndUaddress
@name varchar(50) , --姓名
@address varchar(50) --地址
as
select * from users where uname = @name and uaddress =@address
go




----调用带输入参数的存储过程
exec ups_findUsersByUnameAndUaddress '王炎霸','山西吕梁'

执行结果:

--------------/创建带输入和输出参数的存储过程//-----
--需求:根据姓名查询密码
if exists (select * from sysobjects where name = 'usp_findPassByName')
drop proc usp_findPassByName
go
create proc usp_findPassByName
@name varchar(50),
@pass varchar(50) output  --输出参数
as
select upass from users where uname = @name
go




--调用带输入和输出参数的存储过程
declare @pass varchar(50) --存放输出参数的值
exec usp_findPassByName '薛康',@pass output

执行结果:


原文地址:https://www.cnblogs.com/a1111/p/14877268.html