使用李天平代码生成器中分页存储过程的问题
2008-09-05 11:20
656 查看
今天在使用李天平的代码生成器时发现了自带的分页存储过程的缺陷,当查询条件@strWhere中超过@strTmp varchar(100)的长度时会出现“关键字order附件有语法错误”,@strWhere被截断为100的长度,语法结构也就遭到了破坏,下面标注红色的部分为修改后的代码,其中UP_GetRecordByPageOrder存储过程也会出现相同的情况,修改方式也相同。
------------------------------------
--用途:分页存储过程(对有主键的表效率极高)
--说明:
------------------------------------
CREATE PROCEDURE UP_GetRecordByPage
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 主键字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@IsReCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(6000) -- 主语句
declare @strTmp varchar(1000) -- 临时变量
declare @strOrder varchar(400) -- 排序类型
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
if @PageIndex = 1
begin
set @strTmp =''
if @strWhere != ''
set @strTmp = ' where ' + @strWhere
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + ']' + @strTmp + ' ' + @strOrder
end
if @IsReCount != 0
set @strSQL = 'select count(*) as Total from [' + @tblName + ']'+' where ' + @strWhere
exec (@strSQL)
GO
------------------------------------
--用途:分页存储过程(对有主键的表效率极高)
--说明:
------------------------------------
CREATE PROCEDURE UP_GetRecordByPage
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 主键字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@IsReCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(6000) -- 主语句
declare @strTmp varchar(1000) -- 临时变量
declare @strOrder varchar(400) -- 排序类型
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
if @PageIndex = 1
begin
set @strTmp =''
if @strWhere != ''
set @strTmp = ' where ' + @strWhere
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + ']' + @strTmp + ' ' + @strOrder
end
if @IsReCount != 0
set @strSQL = 'select count(*) as Total from [' + @tblName + ']'+' where ' + @strWhere
exec (@strSQL)
GO
相关文章推荐
- oracle ora-02020 存储过程中dblink使用个数问题
- C#中使用存储过程,C#分页存储过程
- 存储过程使用:分页存储
- sqlserver 通用存储过程分页代码(附使用ROW_NUMBER()和不使用ROW_NUMBER()两种情况性能分析)
- 折腾Oracle问题小菜记[分页存储过程/查询所有表、视图、存储过程/查询表、视图、存储过程字段结构与参数](三)
- 带输出参数的存储过程的使用及在C#中调用问题
- mysql存储过程中两个CURSOR嵌套使用遇到的有关问题
- osx中使用navicat连接sqlserver查看functions中的存储过程的内容时无法显示的问题
- 在存储过程中使用事务处理 sql server 删除多表操作一致性的问题
- 存储过程里使用select要注意的一个问题!
- 存储过程和分页控件的使用
- 使用AspNetPager分页控件、分页存储过程及用户控件基类实现的完美分页功能
- 使用SQL存储过程要特别注意的问题-注意顺序读取
- 原创:SQL Server的通用分页存储过程,未使用游标,速度更快!
- 使用ROW_NUMBER()的分页存储过程
- asp.net之DataList的使用方法,及分页(存储过程创建),编辑,更新,删除
- 使用AspNetPager分页控件、分页存储过程及用户控件基类实现的完美分页功能
- MSSQL根据表名动态分页的存储过程以及C#.net调用使用
- 折腾Oracle问题小菜记[分页存储过程/查询所有表、视图、存储过程/查询表、视图、存储过程字段结构与参数](三)