1.创建无参存储过程
if (exists (select * from sys.objects where name = 'getAllBooks'))
drop proc proc_get_student
go
create procedure getAllBooks
as
select * from books;
--调用,执行存储过程
exec getAllBooks;
2.修改存储过程
alter procedure dbo.getAllBooks
as
select book_auth from books;
3.删除存储过程
drop procedure getAllBooks;
4.重命名存储过程
sp_rename getAllBooks,proc_get_allBooks;
5.创建带参数的存储过程
if (exists (select * from sys.objects where name = 'searchBooks'))
drop proc searchBooks
go
create proc searchBooks(@bookID int)
as
--要求book_id列与输入参数相等
select * from books where book_id=@bookID;
--执行searchBooks
exec searchBooks 1;
带两个参数在int后,往下直接写参数
6.创建有返回值的存储过程
if (exists (select * from sys.objects where name = 'getBookId'))
drop proc getBookId
go
create proc getBookId(
@bookAuth varchar(20),--输入参数,无默认值
@bookId int output --输入/输出参数 无默认值
)
as
select @bookId=book_id from books where book_auth=@bookAuth
--执行getBookId这个带返回值的存储过程
declare @id int --声明一个变量用来接收执行存储过程后的返回值
exec getBookId '孔子',@id output
select @id as bookId;--as是给返回的列值起一个名字
7.创建带通配符的存储过程
if (exists (select * from sys.objects where name = 'charBooks'))
drop proc charBooks
go
create proc charBooks(
@bookAuth varchar(20)='金%',
@bookName varchar(20)='%'
)
as
select * from books where book_auth like @bookAuth and book_name like @bookName;
--执行存储过程charBooks
exec charBooks '孔%','论%';
8.创建加密存储过程
with encryption子句对用户隐藏存储过程的文本.下例创建加密过程,使用 sp_helptext 系统存储过程获取关于加密过程的信息,然后尝试直接从 syscomments 表中获取关于该过程的信息.
复制代码
if (object_id('books_encryption', 'P') is not null)
drop proc books_encryption
go
create proc books_encryption
with encryption
as
select * from books;
--执行此过程books_encryption
exec books_encryption;
exec sp_helptext 'books_encryption';--控制台会显示"对象 'books_encryption' 的文本已加密。"
9.不缓存存储过程
--with recompile不缓存
if (object_id('book_temp', 'P') is not null)
drop proc book_temp
go
create proc book_temp
with recompile
as
select * from books;
go
exec book_temp;
exec sp_helptext 'book_temp';
10.创建带游标参数的存储过程
if (object_id('book_cursor', 'P') is not null)
drop proc book_cursor
go
create proc book_cursor
@bookCursor cursor varying output
as
set @bookCursor=cursor forward_only static for
select book_id,book_name,book_auth from books
open @bookCursor;
go
--调用book_cursor存储过程
declare @cur cursor,
@bookID int,
@bookName varchar(20),
@bookAuth varchar(20);
exec book_cursor @bookCursor=@cur output;
fetch next from @cur into @bookID,@bookName,@bookAuth;
while(@@FETCH_STATUS=0)
begin
fetch next from @cur into @bookID,@bookName,@bookAuth;
print 'bookID:'+convert(varchar,@bookID)+' , bookName: '+ @bookName
+' ,bookAuth: '+@bookAuth;
end
close @cur --关闭游标
DEALLOCATE @cur; --释放游标
11.创建分页存储过程
if (object_id('book_page', 'P') is not null)
drop proc book_page
go
create proc book_page(
@TableName varchar(50), --表名
@ReFieldsStr varchar(200) = '*', --字段名(全部字段为*)
@OrderString varchar(200), --排序字段(必须!支持多字段不用加order by)
@WhereString varchar(500) =N'', --条件语句(不用加where)
@PageSize int, --每页多少条记录
@PageIndex int = 1 , --指定当前为第几页
@TotalRecord int output --返回总记录数
)
as
begin
--处理开始点和结束点
Declare @StartRecord int;
Declare @EndRecord int;
Declare @TotalCountSql nvarchar(500);
Declare @SqlString nvarchar(2000);
set @StartRecord = (@PageIndex-1)*@PageSize + 1
set @EndRecord = @StartRecord + @PageSize - 1
SET @TotalCountSql= N'select @TotalRecord = count(*) from ' + @TableName;--总记录数语句
SET @SqlString = N'(select row_number() over (order by '+ @OrderString +') as rowId,'+@ReFieldsStr+' from '+ @TableName;--查询语句
--
IF (@WhereString! = '' or @WhereString!=null)
BEGIN
SET @TotalCountSql=@TotalCountSql + ' where '+ @WhereString;
SET @SqlString =@SqlString+ ' where '+ @WhereString;
END
--第一次执行得到
--IF(@TotalRecord is null)
-- BEGIN
EXEC sp_executesql @totalCountSql,N'@TotalRecord int out',@TotalRecord output;--返回总记录数
-- END
----执行主语句
set @SqlString ='select * from ' + @SqlString + ') as t where rowId between ' + ltrim(str(@StartRecord)) + ' and ' + ltrim(str(@EndRecord));
Exec(@SqlString)
END
--调用分页存储过程book_page
exec book_page 'books','*','book_id','',3,1,0;
--
declare @totalCount int
exec book_page 'books','*','book_id','',3,1,@totalCount output;
select @totalCount as totalCount;--总记录数。
转载:https://www.cnblogs.com/selene/p/4483612.html