复制代码 代码如下:
CREATE TABLE [dbo].[Xtest](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[XName] [nvarchar](50) NULL,
[XDemo] [nvarchar](500) NULL,
CONSTRAINT [PK_xtest] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
复制代码 代码如下:
declare @i int
set @i=1
while(@i<500001)
begin
insert into dbo.Xtest(XName,XDemo) values(CAST(@i as varchar)+'name'+CAST(@i as varchar),'这是第'+CAST(@i as varchar)+'的内容')
set @i=@i+1
end
复制代码 代码如下:
-- =============================================
-- Author: <jiangrod>
-- Create date: <2010-03-03>
-- Description: <SQL2000通用分页存储过程>调用方法: sp_Pager2000 'xtest','*','ID','ORDER BY ID ASC','xname like ''%222name%''',3,20,0,0
-- =============================================
ALTER PROCEDURE [dbo].[sp_Pager2000]
@tblName varchar(255), -- 表名如:'xtest'
@strGetFields varchar(1000) = '*', -- 需要返回的列如:'xname,xdemo'
@pkName nvarchar(50)='ID', -- 主键名
@strOrder varchar(255)='', -- 排序的字段名如:'order by id desc'
@strWhere varchar(1500) = '', -- 查询条件(注意:不要加where)如:'xname like ''%222name%'''
@pageIndex int = 1, -- 页码如:2
@pageSize int = 20, -- 每页记录数如:20
@recordCount int=0 out, -- 记录总数
@doCount bit=0 -- 非0则统计,为0则不统计(统计会影响效率)
AS
Declare @sql nvarchar(4000)
Declare @sqltemp nvarchar(1000)
set @sql=' From '+@tblName
if(@strWhere!='') set @sql=@sql+' Where '+@strWhere
declare @strCount nvarchar(1000)
--总记录条数
if(@doCount!=0)
begin
if(@strWhere !='')
begin
set @strCount='set @num=(select count(1) from '+ @tblName + ' where '+@strWhere+' )'
end
else
begin
set @strCount='set @num=(select count(1) from '+ @tblName + ' )'
end
EXECUTE sp_executesql @strCount ,N'@num INT output',@RecordCount output
end
if @strWhere !=''
begin
set @strWhere=' where '+@strWhere
end
if (@PageIndex>1)
begin
set @sqltemp=@PKName+' not in (Select Top '+cast((@PageSize*(@PageIndex-1)) as nvarchar)+' '+@PKName+' '+@sql
if(@strOrder!='')set @sqltemp=@sqltemp+' '+@strOrder
set @sqltemp=@sqltemp+')'
set @sql='Select Top '+cast(@PageSize as nvarchar)+' '+@strGetFields+' '+@sql
if(@strWhere!='')set @sql=@sql+' And '+@sqltemp
else set @sql=@sql+' Where '+@sqltemp
end
else
begin
set @sql='Select Top '+cast(@PageSize as nvarchar)+' '+@strGetFields+' '+@sql
end
if(@strOrder!='') set @sql=@sql+' '+@strOrder
exec(@sql)
-- =============================================
-- Author: <jiangrod>
-- Create date: <2010-03-03>
-- Description: <SQL2005及后续版本通用分页存储过程>调用方法: sp_Pager2005 'xtest','*','ORDER BY ID ASC','xname like ''%222name%''',2,20,0,0
-- =============================================
ALTER PROCEDURE [dbo].[sp_Pager2005]
@tblName varchar(255), -- 表名如:'xtest'
@strGetFields varchar(1000) = '*', -- 需要返回的列如:'xname,xdemo'
@strOrder varchar(255)='', -- 排序的字段名如:'order by id desc'
@strWhere varchar(1500) = '', -- 查询条件(注意:不要加where)如:'xname like ''%222name%'''
@pageIndex int = 1, -- 页码如:2
@pageSize int = 20, -- 每页记录数如:20
@recordCount int output, -- 记录总数
@doCount bit=0 -- 非0则统计,为0则不统计(统计会影响效率)
AS
declare @strSQL varchar(5000)
declare @strCount nvarchar(1000)
--总记录条数
if(@doCount!=0)
begin
if(@strWhere !='')
begin
set @strCount='set @num=(select count(1) from '+ @tblName + ' where '+@strWhere+' )'
end
else
begin
set @strCount='set @num=(select count(1) from '+ @tblName + ' )'
end
EXECUTE sp_executesql @strCount ,N'@num INT output',@RecordCount output
end
if @strWhere !=''
begin
set @strWhere=' where '+@strWhere
end
set @strSQL='SELECT * FROM (SELECT ROW_NUMBER() OVER ('+@strOrder+') AS ROWID,'
set @strSQL=@strSQL+@strGetFields+' FROM ['+@tblName+'] '+@strWhere
set @strSQL=@strSQL+') AS sp WHERE ROWID BETWEEN '+str((@PageIndex-1)*@PageSize+1)
set @strSQL=@strSQL+' AND '+str(@PageIndex*@PageSize)
exec (@strSQL)
新闻热点
疑难解答