Sql Server 存储过程

@pageSize 表示每页显示的行数
@pageIndex 表示第几页显示

1: 单独的一个分页查询基本格式为:

select top @pageSize * from Table where Id not in 
(select top @pageSize*(@pageIndex-1) Id from Table order by Id ) 
order by Id

2: 带where条件的分页查询的存储过程的建立

create proc proc_Page
@pageSize int=10,  -- 默认每页显示十条信息
@pageIndex int=1,  -- 默认显示第一页
@where varchar(50) ='1=2'  --默认where条件不成立
as
set nocount on  -- 关掉受影响的行数,以提升性能
declare @sql varchar(300) 
set @sql= 'select top '+convert(varchar,@pageSize)+' * from Table where Id not in (select top '+
          convert(varchar,@pageSize*(@pageIndex-1))+' Id from Table where '+
          @where +' order by Id ) and '+ @where +' order by Id '
exec (@sql) 
go

调用存储过程:
exec proc_Page 5,3,'1=1' --每页5条,
第3页

3:带输出参数的存储过程(新增,插入时使用)

  a: 输出类型为uniqueidentifier,并且是作为主键,默认值是newid() 的输出的存储过程

create procedure proc_TableInsert
@Id uniqueidentifier output,
@Name varchar(50)
as
set nocount on
set @Id=newid()
Insert into table (Id,Name) values (@Id,@Name)
@@error
go


     b: 输出类型为int ,并且是作为主键,默认值自动增长列 的输出的存储过

create procedure proc_TableInsert
@Id int output,
@Name varchar(50)
as
set nocount on
Insert into table (Name) values (@Name)
set @Id=scope_identity()
@@error
go


4: 带输出聚合函数的存储过程

create procedure proc_TableCount
@recordCount int output
as
set nocount on 
declare @sql nvarchar(200)
set @sql=N'select @count=count(-1) from table'
exec sp_executesql @sql , N'@count int output', @recordCount output
return @@error
go
因为相信,所以我去做了......
原文地址:https://www.cnblogs.com/jeffqing/p/2733228.html