最近用了一个分页控件。要用SP来取数据的。
我想传递的是表名和搜索条件(and ....)
怎么平凑起来啊。
错误如下:
-----------------------
消息 1087,级别 15,状态 2,过程 P_GetPaged_List_2000,第 19 行
必须声明表变量 "@tableName"。
消息 1087,级别 15,状态 2,过程 P_GetPaged_List_2000,第 25 行
必须声明表变量 "@tableName"。
-----------------------
我修改的如下:麻烦有朋友帮我调试下。谢谢。
create procedure [dbo].[P_GetPaged_List_2000]
(
@startIndex int,
@endIndex int,
@count int output,
@tableName varchar(100),
@strWhere varchar(500)
)
as
declare @indextable table(id int identity(1,1),nid int)
declare @TmpSelect NVarchar(600)
set rowcount @endIndex
insert into @indextable(nid) select ID from @tableName where 1=1 @strWhere order by ID desc
select O.* from @tableName O
inner join @indextable t on
O.ID=t.nid
where 1=1 and t.id between @startIndex and @endIndex order by t.id
set @count=(select count(1) from @tableName)
select @count
CREATE PROCEDURE [dbo].[GetRecordFromSingleTableByPage1]
@tblName nvarchar(255), -- 表名
@priKeyName nvarchar(50), -- 主键列或标示列
@fldNames nvarchar(1000), -- 字段名,多个字段通过逗号分割
@PageSize int = 0, -- 页尺寸
@PageIndex int = 1 output, -- 页码
@OrderType nvarchar(200) = '', -- 设置排序,'':没有排序要求 0:主键升序 1:主键降序 字符串:用户自定义排序规则
@strWhere nvarchar(2000) = '', -- 查询条件 (注意: 不要加 where)
@TotalItem int output,
@TotalPage int output
AS
declare @strByPage nvarchar(4000) -- 分页查询语句
declare @strNoPage nvarchar(1500) -- 不分页查询语句,获得所有符合条件的记录
declare @strTotal nvarchar(1000) -- 统计符合条件的纪录数
declare @strNonResult nvarchar(1500) -- 返回空记录的语句
declare @strTmp nvarchar(100)
declare @strOrder nvarchar(200)
if @OrderType is null or @OrderType=''
begin
set @strOrder = ''
set @strTmp=''
end
else if @OrderType = '0' -- 降序
begin
set @strTmp = @priKeyName + '>(select max([' + @priKeyName + ']) from '
set @strOrder = ' order by ' + @priKeyName + ' asc'
end
else if @OrderType = '1' -- 降序
begin
set @strTmp = @priKeyName + '<(select min([' + @priKeyName + ']) from '
set @strOrder = ' order by ' + @priKeyName + ' desc'
end
else -- 用户自定义排序规则
begin
set @strTmp = ''
set @strOrder = ' order by ' + @OrderType
end
set @strNonResult = 'select ' + @fldNames + ' from ' + @tblName + ' where 1=2'
if @strWhere is null or @strWhere = '' -- 如果没有额外的查询条件
begin
set @strTotal = N'select @TotalItem = count(*) from ' + @tblname
set @strNoPage = N'select ' + @fldNames + ' from ' + @tblName + @strOrder
end
else
begin
set @strTotal = N'select @TotalItem = count(*) from ' + @tblname + ' where ' + @strWhere
set @strNoPage = N'select ' + @fldNames + ' from ' + @tblName + ' where ' + @strWhere + @strOrder
end
-- 取得所有符合查询条件的记录数
--exec sp_executeSql @strTotal,N'@TotalItem int output',@TotalItem output
set @TotalItem=100
-- 如果没有适合条件的记录时,提供一个空的记录集并退出查询
if @TotalItem = 0
begin
set @TotalPage = 0
exec sp_executeSql @strNonResult
return 0
end
-- 执行查询,此时记录集不为空
if @PageSize = 0 -- 当不需要分页时
begin
set nocount off
set @TotalPage = 0
exec sp_executeSql @strNoPage
return
end
else -- 当需要分页时
begin
set nocount on
-- 得到记录的页数,并调整页号,分页从1开始
set @TotalPage=CEILING(cast(@TotalItem as float)/@PageSize)
if(@PageIndex>@TotalPage)
set @PageIndex=@TotalPage
if(@PageIndex <1)
set @PageIndex=1
if @PageIndex =1 -- 如果是第一页
begin
if @strWhere=''
set @strByPage = N'select top ' + cast(@PageSize as varchar) + ' ' + @fldNames + ' from ' + @tblName + @strOrder
else
set @strByPage = N'select top ' + cast(@PageSize as varchar) + ' ' + @fldNames + ' from ' + @tblName + ' where ' + @strWhere + @strOrder
end
else -- 以后页
begin
if (@OrderType='0' or @OrderType='1') -- 按主键升序或降序
begin
if @strWhere=''
set @strByPage = N'select top ' + cast(@PageSize as varchar) + ' ' + @fldnames
+ ' from ' + @tblName
+ ' where ' + @strTmp
+ ' (select top ' + cast((@PageIndex-1) * @PageSize as varchar) + ' ' + @priKeyName
+ ' from ' + @tblName + @strOrder + ') as tmptbl)'
+ @strOrder
else
set @strByPage = N'select top ' + cast(@PageSize as varchar) + ' ' + @fldnames
+ ' from ' + @tblName
+ ' where ' + @strTmp
+ ' (select top '+ cast((@PageIndex-1) * @PageSize as varchar) + ' ' + @priKeyName
+ ' from ' + @tblName + ' where ' + @strWhere + @strOrder + ') as tmptbl)'
+ ' and ' + @strWhere
+ @strOrder
end
else -- 没有排序规则或者用户自定义规则
begin
if @strWhere=''
set @strByPage = N'select top ' + cast(@PageSize as varchar) + ' ' + @fldnames
+ ' from ' + @tblName
+ ' where not exists (select * from '
+ ' (select top ' + cast((@PageIndex-1) * @PageSize as varchar) + ' * from '
+ @tblName + @strorder + ') as tmpTable '
+ ' where tmpTable.' + @priKeyName + ' = ' + @tblName +'.' + @priKeyName + ')'
+ @strorder
else
set @strByPage = N'select top ' + cast(@PageSize as varchar) + ' ' + @fldnames
+ ' from ' + @tblName
+ ' where not exists (select * from '
+ ' (select top ' + cast((@PageIndex-1) * @PageSize as varchar) + ' * from '
+ @tblName + ' where ' + @strWhere + @strorder + ') as tmpTable '
+ ' where tmpTable.' + @priKeyName + ' = ' + @tblName + '.' + @priKeyName + ')'
+ ' and ' + @strWhere
+ @strorder
end
end
end
print @strByPage
--exec sp_executeSql @strByPage
return
GO
select ID from @tableName
这句得改成
Exec('select ID from '+@tableName)的形式
@indextable 这个在这里得改成临时表#T才能
Exec('insert into #T(nid) select ID from '+@tableName)
在这里用表变量不行的
下面这个有我自己写的各种分页:
http://hi.baidu.com/84ww/blog/item/6fd646cb0fc7a11abe09e6c7.html