asp.net如何创建存储过程分页tb_name、pageindex、pagesize作参数?
我以前用的AspNetPager第三方控件和index,size两个参数是对的,现在想把表名做成动态,却一直报错,说未定义@tb_name求大神指点?ALTERPROCE...
我以前用的AspNetPager第三方控件和index,size两个参数是对的,现在想把表名做成动态,却一直报错,说未定义@tb_name
求大神指点?
ALTER PROCEDURE dbo.Getpage
@pageIndex int,@pageSize int,@biaoming varchar(255)
AS
declare @min int;
declare @max int;
declare @tb_name varchar(255);
set @min=@pageSize*(@pageIndex-1)+1;
set @max=@pageSize*@pageIndex;
set @tb_name=@biaoming;
with myTable as
(select *,Row_Number() over (order by YongHuMing) as rownum from @tb_name)
select * from myTable where rownum between @min and @max
RETURN 展开
求大神指点?
ALTER PROCEDURE dbo.Getpage
@pageIndex int,@pageSize int,@biaoming varchar(255)
AS
declare @min int;
declare @max int;
declare @tb_name varchar(255);
set @min=@pageSize*(@pageIndex-1)+1;
set @max=@pageSize*@pageIndex;
set @tb_name=@biaoming;
with myTable as
(select *,Row_Number() over (order by YongHuMing) as rownum from @tb_name)
select * from myTable where rownum between @min and @max
RETURN 展开
展开全部
这个得测试 具体不好说 可能是传的值问题 下面是我写的分页过程 可以借鉴下
ALTER proc [dbo].[GetRecordFromPage]
@tblName varchar(255), -- 表名
@RetColumns varchar(1000) = '*', -- 需要返回的列,默认为全部
@Orderfld varchar(255), -- 排序字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@IsCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType varchar(50) = 'asc', -- 设置排序类型, 非 asc 值则降序
@strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(1000) -- 主语句
declare @strOrder varchar(400) -- 排序类型
if @IsCount != 0 --执行总数统计
begin
if @strWhere != ''
set @strSQL= 'select COUNT(*) as Total from @tblName from ['+@tblName+'] where '+@strWhere
else
set @strSQL='select count(*) as Total from @tblName from ['+@tblName+']'
end
else
if @Orderfld != ''
begin
if @OrderType != 'asc'
begin
set @strOrder = ' order by [' + @Orderfld +'] desc'
end
else
begin
set @strOrder = ' order by [' + @Orderfld +'] asc'
end
end
if @strWhere != ''
begin
set @strSQL = 'select * from
(
select ROW_NUMBER() over(order by '+@Orderfld+' desc) as num,* from ['+@tblName+'] as n1 where '+@strWhere+'
)
as n2 where '+@strWhere+' and n2.num between ('+Convert(nvarchar(10),@PageIndex)+'-1)*'+Convert(nvarchar(10),@PageSize)+'+1 and '+Convert(nvarchar(10),@PageIndex)+'*'+Convert(nvarchar(10),@PageSize)+' '
end
else
begin
set @strSQL = 'select * from
(
select ROW_NUMBER() over(order by '+@Orderfld+' desc) as num,* from ['+@tblName+'] as n1
)
as n2 where n2.num between ('+Convert(nvarchar(10),@PageIndex)+'-1)*'+Convert(nvarchar(10),@PageSize)+'+1 and '+Convert(nvarchar(10),@PageIndex)+'*'+Convert(nvarchar(10),@PageSize)+' '
end
exec (@strSQL)
ALTER proc [dbo].[GetRecordFromPage]
@tblName varchar(255), -- 表名
@RetColumns varchar(1000) = '*', -- 需要返回的列,默认为全部
@Orderfld varchar(255), -- 排序字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@IsCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType varchar(50) = 'asc', -- 设置排序类型, 非 asc 值则降序
@strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(1000) -- 主语句
declare @strOrder varchar(400) -- 排序类型
if @IsCount != 0 --执行总数统计
begin
if @strWhere != ''
set @strSQL= 'select COUNT(*) as Total from @tblName from ['+@tblName+'] where '+@strWhere
else
set @strSQL='select count(*) as Total from @tblName from ['+@tblName+']'
end
else
if @Orderfld != ''
begin
if @OrderType != 'asc'
begin
set @strOrder = ' order by [' + @Orderfld +'] desc'
end
else
begin
set @strOrder = ' order by [' + @Orderfld +'] asc'
end
end
if @strWhere != ''
begin
set @strSQL = 'select * from
(
select ROW_NUMBER() over(order by '+@Orderfld+' desc) as num,* from ['+@tblName+'] as n1 where '+@strWhere+'
)
as n2 where '+@strWhere+' and n2.num between ('+Convert(nvarchar(10),@PageIndex)+'-1)*'+Convert(nvarchar(10),@PageSize)+'+1 and '+Convert(nvarchar(10),@PageIndex)+'*'+Convert(nvarchar(10),@PageSize)+' '
end
else
begin
set @strSQL = 'select * from
(
select ROW_NUMBER() over(order by '+@Orderfld+' desc) as num,* from ['+@tblName+'] as n1
)
as n2 where n2.num between ('+Convert(nvarchar(10),@PageIndex)+'-1)*'+Convert(nvarchar(10),@PageSize)+'+1 and '+Convert(nvarchar(10),@PageIndex)+'*'+Convert(nvarchar(10),@PageSize)+' '
end
exec (@strSQL)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询