存储过程十种语法
创建语法
1 create proc | procedure pro_name 2 [{@参数数据类型} [=默认值] [output], 3 {@参数数据类型} [=默认值] [output], 4 .... 5 ] 6 as 7 SQL_statements
创建不带参数的存储过程
--创建存储过程 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;
修改存储过程
1 --修改存储过程 2 alter proc proc_get_student 3 as 4 select * from student;
带参存储过程
--带参存储过程 if (object_id('proc_find_stu', 'P') is not null) drop proc proc_find_stu go create proc proc_find_stu(@startId int, @endId int) as select * from student where id between @startId and @endId go exec proc_find_stu 2, 4;
带通配符参数存储过程
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%';
带输出参数存储过程
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;
不缓存存储过程
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;
加密存储过程
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';
带游标参数存储过程
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;--删除游标
分页存储过程
1 ---存储过程、row_number完成分页 2 if (object_id('pro_page', 'P') is not null) 3 drop proc proc_cursor 4 go 5 create proc pro_page 6 @startIndex int, 7 @endIndex int 8 as 9 select count(*) from product 10 ; 11 select * from ( 12 select row_number() over(order by pid) as rowId, * from product 13 ) temp 14 where temp.rowId between @startIndex and @endIndex 15 go 16 --drop proc pro_page 17 exec pro_page 1, 4 18 -- 19 --分页存储过程 20 if (object_id('pro_page', 'P') is not null) 21 drop proc pro_stu 22 go 23 create procedure pro_stu( 24 @pageIndex int, 25 @pageSize int 26 ) 27 as 28 declare @startRow int, @endRow int 29 set @startRow = (@pageIndex - 1) * @pageSize +1 30 set @endRow = @startRow + @pageSize -1 31 select * from ( 32 select *, row_number() over (order by id asc) as number from student 33 ) t 34 where t.number between @startRow and @endRow; 35 36 exec pro_stu 2, 2;