05的分页存储过程改成2000的,我老改不对,希望指点

新亿 2011-06-20 10:17:08


CREATE proc [dbo].[UP_GetRecordByPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 排序字段(支持多字段,建议建索引) 比如 id asc ,addtime desc
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
--@IsReCount bit = 0, -- 返回记录总数, 非 0 值则返回
@TotalPages int output ,--输出参数,返回总页数
--@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) = '', -- 查询条件 (注意: 不要加 where)
@DataCount int output
AS
BEGIN
declare @strSql nvarchar(4000)
declare @rowsCount int
if Ltrim(Rtrim(@strWhere))=''
SET @strWhere=' 1=1 '
--obtain RowsCount
SET @strSql='select @TotalRecords=COUNT(*) from '+@tblName+' where '+@strWhere
execute SP_executesql @strSql,N'@TotalRecords int output ',@rowsCount output
select @DataCount=@rowsCount
select @TotalPages=CEILING((@rowsCount+0.0)/@PageSize)
if @PageIndex<=0
SET @PageIndex=1
if @PageIndex > @TotalPages
SET @PageIndex=@TotalPages
SET @strSql='select * from (select top (@PageIndex*@PageSize) * ,ROW_NUMBER() OVER(order by '+@fldName+') as rowNumber from '+@tblName+' where '+@strWhere+' ) t where t.rowNumber>=((@PageIndex-1)*@PageSize+1)'
print @strSql
exec sp_executesql @strSql,N'@PageIndex int, @PageSize int',@PageIndex,@PageSize
END



GO




保持传入和传出参数不变啊,因为我不想去改程序代码了, 执行存储过程的就这样,解决的给满分
...全文
132 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
新亿 2011-06-22
  • 打赏
  • 举报
回复

[Quote=引用 18 楼 acherat 的回复:]
SQL code

CREATE proc [dbo].[UP_GetRecordByPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 排序字段(支持多字段,建议建索引) 比如 id asc ,addtime desc
@PageSize ……
[/Quote]

终于ok了,不报错了
就是你能把 参数和 05传入的一样么 增加个 @zhujian
我还必须传进来值,然后程序方面还要改动 和判断了。。
AcHerat 2011-06-22
  • 打赏
  • 举报
回复

CREATE proc [dbo].[UP_GetRecordByPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 排序字段(支持多字段,建议建索引) 比如 id asc ,addtime desc
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@zhujian varchar(255), -- 主键列
--@IsReCount bit = 0, -- 返回记录总数, 非 0 值则返回
@TotalPages int output ,--输出参数,返回总页数
--@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) = '', -- 查询条件 (注意: 不要加 where)
@DataCount int output
AS
BEGIN
declare @strSql nvarchar(4000)
declare @rowsCount int
if Ltrim(Rtrim(@strWhere))=''
SET @strWhere=' 1=1 '
--obtain RowsCount
SET @strSql='select @TotalRecords=COUNT(*) from '+@tblName+' where '+@strWhere
execute SP_executesql @strSql,N'@TotalRecords int output ',@rowsCount output
select @DataCount=@rowsCount
select @TotalPages=CEILING((@rowsCount+0.0)/@PageSize)
if @PageIndex<=0
SET @PageIndex=1
if @PageIndex > @TotalPages
SET @PageIndex=@TotalPages
SET @strSql='select top '+ltrim(@PageSize)+' * from '+@tblName
+' where '+@strWhere+' and '+@zhujian+' not in (select top ' +ltrim((@PageIndex-1)*@PageSize+1)+' '
+@zhujian+' from '+@tblName+' where '+@strWhere+' order by ' + @fldName + ')'
+' order by ' + @fldName --by后加空格
print @strSql
exec(@strSql)
END
GO

新亿 2011-06-22
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 acherat 的回复:]
SQL code

CREATE proc [dbo].[UP_GetRecordByPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 排序字段(支持多字段,建议建索引) 比如 id asc ,addtime desc
@PageSize ……
[/Quote]

报错提示
select top 10 * from xyc_Article where 1=1 and art_ID not in (select top 1 art_ID from tb where 1=1 order by art_ID desc) order by art_ID desc
消息 208,级别 16,状态 1,第 1 行
对象名 'tb' 无效。

(1 行受影响)

(1 行受影响)
AcHerat 2011-06-21
  • 打赏
  • 举报
回复

CREATE proc [dbo].[UP_GetRecordByPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 排序字段(支持多字段,建议建索引) 比如 id asc ,addtime desc
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@zhujian varchar(255), -- 主键列
--@IsReCount bit = 0, -- 返回记录总数, 非 0 值则返回
@TotalPages int output ,--输出参数,返回总页数
--@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) = '', -- 查询条件 (注意: 不要加 where)
@DataCount int output
AS
BEGIN
declare @strSql nvarchar(4000)
declare @rowsCount int
if Ltrim(Rtrim(@strWhere))=''
SET @strWhere=' 1=1 '
--obtain RowsCount
SET @strSql='select @TotalRecords=COUNT(*) from '+@tblName+' where '+@strWhere
execute SP_executesql @strSql,N'@TotalRecords int output ',@rowsCount output
select @DataCount=@rowsCount
select @TotalPages=CEILING((@rowsCount+0.0)/@PageSize)
if @PageIndex<=0
SET @PageIndex=1
if @PageIndex > @TotalPages
SET @PageIndex=@TotalPages
SET @strSql='select top '+ltrim(@PageSize)+' * from '+@tblName
+' where '+@strWhere+' and '+@zhujian+' not in (select top ' +ltrim((@PageIndex-1)*@PageSize+1)+' '
+@zhujian+' from tb where '+@strWhere+' order by ' + @fldName + ')'
+' order by ' + @fldName --by后加空格
print @strSql
exec(@strSql)
END
GO
--小F-- 2011-06-20
  • 打赏
  • 举报
回复
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go





ALTER PROC [dbo].[PROCE_PageView2000]
(
@tbname nvarchar(100), --要分页显示的表名
@FieldKey nvarchar(1000), --用于定位记录的主键(惟一键)字段,可以是逗号分隔的多个字段
@PageCurrent int=1, --要显示的页码
@PageSize int=10, --每页的大小(记录数)
@FieldShow nvarchar(1000)='', --以逗号分隔的要显示的字段列表,如果不指定,则显示所有字段
@FieldOrder nvarchar(1000)='', --以逗号分隔的排序字段列表,可以指定在字段后面指定DESC/ASC
@WhereString nvarchar(1000)=N'', --查询条件
@RecordCount int OUTPUT --总记录数
)
AS
SET NOCOUNT ON
--检查对象是否有效
--IF OBJECT_ID(@tbname) IS NULL
--BEGIN
-- RAISERROR(N'对象"%s"不存在',1,16,@tbname)
-- RETURN
--END
--IF OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTable')=0
-- AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsView')=0
-- AND OBJECTPROPERTY(OBJECT_ID(@tbname),N'IsTableFunction')=0
--BEGIN
-- RAISERROR(N'"%s"不是表、视图或者表值函数',1,16,@tbname)
-- RETURN
--END

--分页字段检查
IF ISNULL(@FieldKey,N'')=''
BEGIN
RAISERROR(N'分页处理需要主键(或者惟一键)',1,16)
RETURN
END

--其他参数检查及规范
IF ISNULL(@PageCurrent,0)<1 SET @PageCurrent=1
IF ISNULL(@PageSize,0)<1 SET @PageSize=10
IF ISNULL(@FieldShow,N'')=N'' SET @FieldShow=N'*'
IF ISNULL(@FieldOrder,N'')=N''
SET @FieldOrder=N''
ELSE
SET @FieldOrder=N'ORDER BY '+LTRIM(@FieldOrder)
IF ISNULL(@WhereString,N'')=N''
SET @WhereString=N''
ELSE
SET @WhereString=N'WHERE ('+@WhereString+N')'

--如果@RecordCount为NULL值,则计算总页数(这样设计可以只在第一次计算总页数,以后调用时,把总页数传回给存储过程,避免再次计算总页数,对于不想计算总页数的处理而言,可以给@RecordCount赋值)
IF @RecordCount IS NULL
BEGIN
DECLARE @sql nvarchar(4000)
SET @sql=N'SELECT @RecordCount=COUNT(*)'
+N' FROM '+@tbname
+N' '+@WhereString
EXEC sp_executesql @sql,N'@RecordCount int OUTPUT',@RecordCount OUTPUT
END

--计算分页显示的TOPN值
DECLARE @TopN varchar(20),@TopN1 varchar(20)
SELECT @TopN=@PageSize,
@TopN1=(@PageCurrent-1)*@PageSize

--第一页直接显示
IF @PageCurrent=1
EXEC(N'SELECT TOP '+@TopN
+N' '+@FieldShow
+N' FROM '+@tbname
+N' '+@WhereString
+N' '+@FieldOrder)
ELSE
BEGIN
--处理别名
IF @FieldShow=N'*'
SET @FieldShow=N'a.*'

--生成主键(惟一键)处理条件
DECLARE @Where1 nvarchar(4000),@Where2 nvarchar(4000),
@s nvarchar(1000),@Field sysname
SELECT @Where1=N'',@Where2=N'',@s=@FieldKey
WHILE CHARINDEX(N',',@s)>0
SELECT @Field=LEFT(@s,CHARINDEX(N',',@s)-1),
@s=STUFF(@s,1,CHARINDEX(N',',@s),N''),
@Where1=@Where1+N' AND a.'+@Field+N'=b.'+@Field,
@Where2=@Where2+N' AND b.'+@Field+N' IS NULL',
@WhereString=REPLACE(@WhereString,@Field,N'a.'+@Field),
@FieldOrder=REPLACE(@FieldOrder,@Field,N'a.'+@Field),
@FieldShow=REPLACE(@FieldShow,@Field,N'a.'+@Field)
SELECT @WhereString=REPLACE(@WhereString,@s,N'a.'+@s),
@FieldOrder=REPLACE(@FieldOrder,@s,N'a.'+@s),
@FieldShow=REPLACE(@FieldShow,@s,N'a.'+@s),
@Where1=STUFF(@Where1+N' AND a.'+@s+N'=b.'+@s,1,5,N''),
@Where2=CASE
WHEN @WhereString='' THEN N'WHERE ('
ELSE @WhereString+N' AND ('
END+N'b.'+@s+N' IS NULL'+@Where2+N')'

--执行查询
EXEC(N'SELECT TOP '+@TopN
+N' '+@FieldShow
+N' FROM '+@tbname
+N' a LEFT JOIN(SELECT TOP '+@TopN1
+N' '+@FieldKey
+N' FROM '+@tbname
+N' a '+@WhereString
+N' '+@FieldOrder
+N')b ON '+@Where1
+N' '+@Where2
+N' '+@FieldOrder)
END
唐诗三百首 2011-06-20
  • 打赏
  • 举报
回复
手头没有SQL2000环境,无法测试, 但SQL2000与SQL2005语法差别不很大吧?
直接执行create proc UP_GetRecordByPage ... 报什么错误?
AcHerat 2011-06-20
  • 打赏
  • 举报
回复

CREATE proc [dbo].[UP_GetRecordByPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 排序字段(支持多字段,建议建索引) 比如 id asc ,addtime desc
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@zhujian varchar(255), -- 主键列
--@IsReCount bit = 0, -- 返回记录总数, 非 0 值则返回
@TotalPages int output ,--输出参数,返回总页数
--@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) = '', -- 查询条件 (注意: 不要加 where)
@DataCount int output
AS
BEGIN
declare @strSql nvarchar(4000)
declare @rowsCount int
if Ltrim(Rtrim(@strWhere))=''
SET @strWhere=' 1=1 '
--obtain RowsCount
SET @strSql='select @TotalRecords=COUNT(*) from '+@tblName+' where '+@strWhere
execute SP_executesql @strSql,N'@TotalRecords int output ',@rowsCount output
select @DataCount=@rowsCount
select @TotalPages=CEILING((@rowsCount+0.0)/@PageSize)
if @PageIndex<=0
SET @PageIndex=1
if @PageIndex > @TotalPages
SET @PageIndex=@TotalPages
SET @strSql='(select top (@PageSize) * from '+@tblName
+' where '+@strWhere+' and '+@zhujian+' not in (select top ((@PageIndex-1)*@PageSize+1) '
+@zhujian+' from tb where '+@strWhere+' order by ' + @fldName + ')'
+' order by' + @fldName
print @strSql
exec sp_executesql @strSql,N'@PageIndex int, @PageSize int',@PageIndex,@PageSize
END
GO
AcHerat 2011-06-20
  • 打赏
  • 举报
回复

CREATE proc [dbo].[UP_GetRecordByPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 排序字段(支持多字段,建议建索引) 比如 id asc ,addtime desc
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@zhujian varchar(255), -- 主键列
--@IsReCount bit = 0, -- 返回记录总数, 非 0 值则返回
@TotalPages int output ,--输出参数,返回总页数
--@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) = '', -- 查询条件 (注意: 不要加 where)
@DataCount int output
AS
BEGIN
declare @strSql nvarchar(4000)
declare @rowsCount int
if Ltrim(Rtrim(@strWhere))=''
SET @strWhere=' 1=1 '
--obtain RowsCount
SET @strSql='select @TotalRecords=COUNT(*) from '+@tblName+' where '+@strWhere
execute SP_executesql @strSql,N'@TotalRecords int output ',@rowsCount output
select @DataCount=@rowsCount
select @TotalPages=CEILING((@rowsCount+0.0)/@PageSize)
if @PageIndex<=0
SET @PageIndex=1
if @PageIndex > @TotalPages
SET @PageIndex=@TotalPages
SET @strSql='(select top '+ltrim(@PageSize)+' * from '+@tblName
+' where '+@strWhere+' and '+@zhujian+' not in (select top 'ltrim((@PageIndex-1)*@PageSize+1)+' '
+@zhujian+' from tb where '+@strWhere+' order by ' + @fldName + ')'
+' order by' + @fldName
print @strSql
exec sp_executesql @strSql,N'@PageIndex int, @PageSize int',@PageIndex,@PageSize
END
GO


2000不支持ROW_NUMBER()排序函数。
新亿 2011-06-20
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 zxl2235 的回复:]
引用 13 楼 webgee 的回复:
引用 10 楼 acherat 的回复:
最后的order by 加个空格。


SQL code

CREATE proc [dbo].[UP_GetRecordByPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 排序字段(支持多字段,建议建索引) 比如 id as……
[/Quote]

还是等作者测试下看看,你那边报错了么?
zxl2235 2011-06-20
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 webgee 的回复:]
引用 10 楼 acherat 的回复:
最后的order by 加个空格。


SQL code

CREATE proc [dbo].[UP_GetRecordByPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 排序字段(支持多字段,建议建索引) 比如 id asc ,addtime d……



……
[/Quote]
你再在最后加个括号或第一个去掉试试,怎么感觉select前面的括号没结尾的了。
新亿 2011-06-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 acherat 的回复:]
最后的order by 加个空格。


SQL code

CREATE proc [dbo].[UP_GetRecordByPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 排序字段(支持多字段,建议建索引) 比如 id asc ,addtime d……
[/Quote]


(select top 10 * from xyc_Article where 1=1 and art_ID not in (select top 1 art_ID from tb where 1=1 order by art_ID desc) order by art_ID desc
消息 156,级别 15,状态 1,第 1 行
在关键字 'order' 附近有语法错误。

(1 行受影响)

(1 行受影响)


报错啦,还是报错呢 对了,能不能不要zhujian 这个传入变量
zxl2235 2011-06-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 webgee 的回复:]
报错呢

SQL code


(select top 10 * from xyc_Article where 1=1 and art_ID not in (select top 1 art_ID from tb where 1=1 order by art_ID) order byart_ID
消息 156,级别 15,状态 1,第 1 行
在关键字 'order' 附近有语法错……
[/Quote]

怎么感觉第一个括号没最后少了半边。
快溜 2011-06-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 acherat 的回复:]
SQL code

CREATE proc [dbo].[UP_GetRecordByPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 排序字段(支持多字段,建议建索引) 比如 id asc ,addtime desc
@PageSize ……
[/Quote].
AcHerat 2011-06-20
  • 打赏
  • 举报
回复
最后的order by 加个空格。


CREATE proc [dbo].[UP_GetRecordByPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 排序字段(支持多字段,建议建索引) 比如 id asc ,addtime desc
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@zhujian varchar(255), -- 主键列
--@IsReCount bit = 0, -- 返回记录总数, 非 0 值则返回
@TotalPages int output ,--输出参数,返回总页数
--@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) = '', -- 查询条件 (注意: 不要加 where)
@DataCount int output
AS
BEGIN
declare @strSql nvarchar(4000)
declare @rowsCount int
if Ltrim(Rtrim(@strWhere))=''
SET @strWhere=' 1=1 '
--obtain RowsCount
SET @strSql='select @TotalRecords=COUNT(*) from '+@tblName+' where '+@strWhere
execute SP_executesql @strSql,N'@TotalRecords int output ',@rowsCount output
select @DataCount=@rowsCount
select @TotalPages=CEILING((@rowsCount+0.0)/@PageSize)
if @PageIndex<=0
SET @PageIndex=1
if @PageIndex > @TotalPages
SET @PageIndex=@TotalPages
SET @strSql='(select top '+ltrim(@PageSize)+' * from '+@tblName
+' where '+@strWhere+' and '+@zhujian+' not in (select top ' +ltrim((@PageIndex-1)*@PageSize+1)+' '
+@zhujian+' from tb where '+@strWhere+' order by ' + @fldName + ')'
+' order by ' + @fldName --by后加空格
print @strSql
exec(@strSql)
END
GO

新亿 2011-06-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 acherat 的回复:]
SQL code

CREATE proc [dbo].[UP_GetRecordByPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 排序字段(支持多字段,建议建索引) 比如 id asc ,addtime desc
@PageSize ……
[/Quote]
还是有点报错呢
新亿 2011-06-20
  • 打赏
  • 举报
回复
报错呢

(select top 10 * from xyc_Article where 1=1 and art_ID not in (select top 1 art_ID from tb where 1=1 order by art_ID) order byart_ID
消息 156,级别 15,状态 1,第 1 行
在关键字 'order' 附近有语法错误。

(1 行受影响)

(1 行受影响)
AcHerat 2011-06-20
  • 打赏
  • 举报
回复

CREATE proc [dbo].[UP_GetRecordByPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 排序字段(支持多字段,建议建索引) 比如 id asc ,addtime desc
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@zhujian varchar(255), -- 主键列
--@IsReCount bit = 0, -- 返回记录总数, 非 0 值则返回
@TotalPages int output ,--输出参数,返回总页数
--@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) = '', -- 查询条件 (注意: 不要加 where)
@DataCount int output
AS
BEGIN
declare @strSql nvarchar(4000)
declare @rowsCount int
if Ltrim(Rtrim(@strWhere))=''
SET @strWhere=' 1=1 '
--obtain RowsCount
SET @strSql='select @TotalRecords=COUNT(*) from '+@tblName+' where '+@strWhere
execute SP_executesql @strSql,N'@TotalRecords int output ',@rowsCount output
select @DataCount=@rowsCount
select @TotalPages=CEILING((@rowsCount+0.0)/@PageSize)
if @PageIndex<=0
SET @PageIndex=1
if @PageIndex > @TotalPages
SET @PageIndex=@TotalPages
SET @strSql='(select top '+ltrim(@PageSize)+' * from '+@tblName
+' where '+@strWhere+' and '+@zhujian+' not in (select top ' +ltrim((@PageIndex-1)*@PageSize+1)+' '
+@zhujian+' from tb where '+@strWhere+' order by ' + @fldName + ')'
+' order by' + @fldName
print @strSql
exec(@strSql)
END
GO
新亿 2011-06-20
  • 打赏
  • 举报
回复
将zhujian 赋值过去,怎么会报错了呢,我写的是 那个表的 ID

(select top (@PageSize) * from xyc_Article where 1=1 and art_ID not in (select top ((@PageIndex-1)*@PageSize+1) art_ID from tb where 1=1 order by art_ID) order byart_ID
消息 170,级别 15,状态 1,第 1 行
第 1 行: '(' 附近有语法错误。
消息 170,级别 15,状态 1,第 1 行
第 1 行: '(' 附近有语法错误。

(1 行受影响)

(1 行受影响)
]
新亿 2011-06-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 acherat 的回复:]
SQL code

CREATE proc [dbo].[UP_GetRecordByPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 排序字段(支持多字段,建议建索引) 比如 id asc ,addtime desc
@PageSize ……
[/Quote]

@zhujian 必须要传入么

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧