存储过程语法二

1、 创建语法

create proc | procedure pro_name
[{@参数数据类型} [=默认值] [output],
{@参数数据类型} [=默认值] [output],
....
]
as
SQL_statements


2、 创建不带参数存储过程
--创建存储过程
if (exists (select * from sys.objects where name = 'proc_get_student'))
drop proc proc_get_student
go
create proc proc_get_student
as
select * from student;

--调用、执行存储过程
exec proc_get_student;



3、 修改存储过程
--修改存储过程
alter proc proc_get_student
as
select * from student;

4、 带参存储过程

 1 --带参存储过程
 2 if (object_id('proc_find_stu', 'P') is not null)
 3     drop proc proc_find_stu
 4 go
 5 create proc proc_find_stu(@startId int, @endId int)
 6 as
 7     select * from student where id between @startId and @endId
 8 go
 9 
10 exec proc_find_stu 2, 4;
View Code

5、 带通配符参数存储过程

 1 --带通配符参数存储过程
 2 if (object_id('proc_findStudentByName', 'P') is not null)
 3     drop proc proc_findStudentByName
 4 go
 5 create proc proc_findStudentByName(@name varchar(20) = '%j%', @nextName varchar(20) = '%')
 6 as
 7     select * from student where name like @name and name like @nextName;
 8 go
 9 
10 exec proc_findStudentByName;
11 exec proc_findStudentByName '%o%', 't%';
View Code

6、 带输出参数存储过程

 1 if (object_id('proc_getStudentRecord', 'P') is not null)
 2     drop proc proc_getStudentRecord
 3 go
 4 create proc proc_getStudentRecord(
 5     @id int, --默认输入参数
 6     @name varchar(20) out, --输出参数
 7     @age varchar(20) output--输入输出参数
 8 )
 9 as
10     select @name = name, @age = age  from student where id = @id and sex = @age;
11 go
12 
13 -- 
14 declare @id int,
15         @name varchar(20),
16         @temp varchar(20);
17 set @id = 7; 
18 set @temp = 1;
19 exec proc_getStudentRecord @id, @name out, @temp output;
20 select @name, @temp;
21 print @name + '#' + @temp;
View Code

7、 不缓存存储过程

 1 --WITH RECOMPILE 不缓存
 2 if (object_id('proc_temp', 'P') is not null)
 3     drop proc proc_temp
 4 go
 5 create proc proc_temp
 6 with recompile
 7 as
 8     select * from student;
 9 go
10 
11 exec proc_temp;
View Code

8、 加密存储过程

 1 --加密WITH ENCRYPTION 
 2 if (object_id('proc_temp_encryption', 'P') is not null)
 3     drop proc proc_temp_encryption
 4 go
 5 create proc proc_temp_encryption
 6 with encryption
 7 as
 8     select * from student;
 9 go
10 
11 exec proc_temp_encryption;
12 exec sp_helptext 'proc_temp';
13 exec sp_helptext 'proc_temp_encryption';
View Code

9、 带游标参数存储过程

 1 if (object_id('proc_cursor', 'P') is not null)
 2     drop proc proc_cursor
 3 go
 4 create proc proc_cursor
 5     @cur cursor varying output
 6 as
 7     set @cur = cursor forward_only static for
 8     select id, name, age from student;
 9     open @cur;
10 go
11 --调用
12 declare @exec_cur cursor;
13 declare @id int,
14         @name varchar(20),
15         @age int;
16 exec proc_cursor @cur = @exec_cur output;--调用存储过程
17 fetch next from @exec_cur into @id, @name, @age;
18 while (@@fetch_status = 0)
19 begin
20     fetch next from @exec_cur into @id, @name, @age;
21     print 'id: ' + convert(varchar, @id) + ', name: ' + @name + ', age: ' + convert(char, @age);
22 end
23 close @exec_cur;
24 deallocate @exec_cur;--删除游标
View Code

10.分页存储过程

 1 create proc [dbo].[p_paging]
 2 @tableName varchar(8000),          --表名、视图名
 3 @indexCol varchar(50) = 'id',      --标识列名(如:比如主键、标识,推荐使用索引列)
 4 @pageSize int = 10,                --页面大小
 5 @pageIndex int = 0,                --当前页
 6 @orderCol varchar(100) = 'id desc',--排序 (如:id)
 7 @where varchar(max) = '',         --条件
 8 @columns varchar(500) = '*'        --要显示的列
 9 as
10 declare @sql varchar(max)
11 declare @sql2 varchar(max)
12 declare @where2 varchar(max)
13 
14 if @where <> '' 
15 begin 
16     select @where2 = ' And ' + @where
17     select @where = ' Where ' + @where
18 end
19 else
20     select @where2 = ''
21 
22 
23 select @sql = 'Select Top ' + Convert(varchar(10),@pageSize) + ' ' + @columns + ' From ' + @tableName
24 select @sql2 = @sql + @where
25 select @sql =  @sql + ' Where ' + '(' + @indexCol + ' Not In (Select Top ' + Convert(varchar(10), @pageSize * @pageIndex) + ' ' + @indexCol + ' From ' + @tableName + @where +  ' Order by '+ @orderCol +'))'
26 select @sql = @sql + @where2 
27 select @sql = @sql + ' Order by ' + @orderCol
28 --获取数据集
29 exec (@sql)
30 select @sql2 = Replace(@sql2,'Top ' + Convert(varchar(10), @pageSize) + ' ' + @columns, 'count(1)')
31 --获取总数据条数
32 exec(@sql2) 
View Code
原文地址:https://www.cnblogs.com/ybb521/p/3201190.html