1、不带参数的存储过程

Use NorthWind

if Exists(select name from sysobjects where name='GetName' and xtype='p')

drop procedure GetName

go

Create Procedure GetName

as

go

select FirstName,LastName from Employees

go

执行存储过程

EXECUTE GetName或者是Exec GetName

如果该过程是批处理中的第一句

GetName

2、带参数的存储过程

if exists(select name from sysobjects where name='GetParaName' and xtype='p')

drop procedure GetParaName

go

create procedure GetParaName

@firstname varchar(30),

@lastname varchar(30)

as

go

select firstname ,lastname from Employees where FirstName=@firstname and LastName=@lastname

go

存储过程的执行方法

Execute GetParaName 'an','bd'

或者

Execute GetParaName @firstname='an',@lastname='dd'

或者

execute GetParaName @lastname='d',@firstname='d'

或者

Exec GetParaName 'and','bddd'

3、使用带有通配符的简单参数存储过程

if exists (select name from sysobjects where name='au_info' and xtype='p')
drop procedure au_info
go
create procedure au_info
@location varchar(100)='北%',
@valuetype varchar(100)='%'
as
begin
select * from TTotal
where 地区 like @location and 数值类别 like @valuetype
end

 调用

exec au_info 或者execute au_info '上海',或者execute au_info '上%','最大值'

或者execute au_info @location='上海',@valuetype='%'

或者

execute au_info @valuetype='最小值',@location='北京'

 

4、使用OUTPUT参数

OUTPUT参数允许外部过程,批处理过程或多条Transact-SQL 语句访问在过程执行期间设置的某个值,下面的示例创建一个存储过程(SumValue),并使用一个可选参数和一个输出参数

首先创建一个存储过程

if exists(select name from sysobjects where name='SumValue' and xtype='p')
DROP PROCEDURE SumValue
go
CREATE PROCEDURE SumValue
@location varchar(100)='%',
@sumcount INT OUTPUT
AS
BEGIN
select @sumcount=sum(数值) from TTotal where 地区 like @location
END

接下来,将该OUTPUT参数用于控制流语言。

说明OUTPUT变量必须在创建表和使用该变量是进行定义

参数名和变量名不一定要匹配,不过数据类型和参数位置必须匹配(除非使用@@SUM = variable 形式)。

declare @totalcount int
execute SumValue '北%',@totalcount output
select @totalcount
if @totalcount<10
begin
print ''
print 'all of these value can be purchased for less than ¥200.'
end
else
select 'The total cost of these title   '+RTRIM(CAST(@totalcount as varchar(20)))

执行结果如下:

SQL SERVER存储过程_SQL SERVER 

 

5、使用OUTPUT游标参数

OUTPUT 游标参数用来存储过程的局部游标传递回调批处理、存储过程、触发器。

首先,创建一个存储过程,在TTotal表上声明并打开一个游标:

if exists(select name from sysobjects where name='Tcursor' and xtype='p')
drop procedure Tcursor
go
create procedure Tcursor @Totalcursor cursor varying output
as
set @Totalcursor=cursor
forward_only static for
select * from TTotal
open @Totalcursor
go

接下来,执行一个批处理,声明一个局部游标变量,执行上述过程已将游标赋给局部变量,然后从游标中提取行

go
declare @mycursor cursor
exec Tcursor @Totalcursor=@mycursor output
while(@@FETCH_STATUS=0)
begin
fetch next from @mycursor
end
close @mycursor
deallocate @mycursor
go

6、使用With Encryption选项

with Encryption子句对用户隐藏存储过程的文本

if exists(select name from sysobjects where name='encrypproc' and xtype='p')
drop procedure encrypproc
go
create procedure encryproc
with encryption
as
select * from TTotal
go

 

exec sp_helptext encrypproc

下面是结果集:

The text for object 'encryproc' is encrypted.

接下了,选择加密存储过程内容的标识号和文本

SELECT c.id,c.text FROM syscomments c INNER JOIN sysobjects o ON c.id = o.id WHERE o.name = 'encryproc'

以下是执行结果:

SQL SERVER存储过程_SQL SERVER_02