存储过程 分页 Where 出错.

worldhj1 2009-03-16 08:35:02
程序中副值就会出错
da.SelectCommand.Parameters.Add("@SelectOrder", SqlDbType.VarChar, 200).Value = "Where 1=1";
提示Where出错.
但写在里面的却可以.
@SelectWhere Varchar(500)='Where 1=1'
不知道为何啊..


CREATE proc getdataset
@TableList Varchar(200)='*',--搜索表的字段,比如:’id,datatime,job‘,用逗号隔开
@TableName Varchar(30)='Product_Table', --搜索的表名
@SelectWhere Varchar(500)='Where 1=1',--搜索条件,这里不用写where,比如:job=’teacher‘and class='2'
@SelectOrderId Varchar(20)='ProductID',--表主键字段名。比如:id
@SelectOrder Varchar(200)='', --排序,可以使用多字段排序但主键字段必需在最前面.也可以不写,比如:order by class asc
@intPageNo int=1, --页号
@intPageSize int=10 ,--每页显示数
@RecordCount int OUTPUT --总记录数(存储过程输出参数)
as

declare @TmpSelect NVarchar(600)
declare @Tmp NVarchar(600)

set nocount on--关闭计数

set @TmpSelect = 'select @RecordCount = count(*) from '+@TableName+' '+@SelectWhere

execute sp_executesql
@TmpSelect, --执行上面的sql语句
N'@RecordCount int OUTPUT' , --执行输出数据的sql语句,output出总记录数
@RecordCount OUTPUT

if (@RecordCount = 0) --如果没有贴子,则返回零
return 0

/*判断页数是否正确*/
if (@intPageNo - 1) * @intPageSize > @RecordCount --页号大于总页数,返回错误
return (-1)
set nocount off--打开计数
if @SelectWhere != ''
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectWhere +' '+@SelectOrder+') and '+@SelectWhere +' '+@SelectOrder
end
else
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectOrder+') '+@SelectOrder
end
execute sp_executesql @TmpSelect
return(@@rowcount)
GO

...全文
77 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
worldhj1 2009-03-16
  • 打赏
  • 举报
回复
还想知道print @TmpSelect
怎么检查的???谢谢了。
worldhj1 2009-03-16
  • 打赏
  • 举报
回复
饿。。。下面漏了Where....谢谢了。
tyb1222 2009-03-16
  • 打赏
  • 举报
回复
关注
worldhj1 2009-03-16
  • 打赏
  • 举报
回复
加了这句怎么使用? 运行程序还是显示错误。
wuyq11 2009-03-16
  • 打赏
  • 举报
回复
print @TmpSelect
执行看看
worldhj1 2009-03-16
  • 打赏
  • 举报
回复
不好意思。我是写的da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 200).Value = "Where 1=1";
帖错了。
worldhj1 2009-03-16
  • 打赏
  • 举报
回复
好象是下面这里有问题

if @SelectWhere != ''
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectWhere +' '+@SelectOrder+') and '+@SelectWhere +' '+@SelectOrder
end
wuyq11 2009-03-16
  • 打赏
  • 举报
回复
da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 200).Value = "Where 1=1";
worldhj1 2009-03-16
  • 打赏
  • 举报
回复
原来里面写Where 1=1 也错。。大家看下这个分页Where哪有问题。。
wenblue7 2009-03-16
  • 打赏
  • 举报
回复
关注

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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