请教关于存储过程的问题,谢谢~!

zeng_job 2010-03-02 03:49:06
if exists(select name from sysobjects where name = N'pageQuery')--加N表示为常量
drop proc pageQuery
go

create proc pageQuery
@areaId varchar(10)='SH',

@returnCol nvarchar(100)='*',--返回的列,默认值为*

@pageSize int,--每页数据数量

@colname nvarchar(20),--依据排序的列,实际上是根据行号排序

@tableName nvarchar(50),--表名

@pageIndex int,--页码

@isDesc int=0,--是否降序

@where nvarchar(200)='',--where条件子句

@sum int output--记录总条数



as
declare @parm nvarchar(100)--定义调用系统存储过程sp_executesql时需要用到的output参数

set @parm=N'@s int output'--给output参数赋值

declare @tempsql nvarchar(200)--定义临时sql语句

set @tempsql=N'select @s=count(*) from '+@tableName +' where manageResult is null and AreaId=@areaId' --给临时sql语句

declare @sumPage int--定义接收记录总条数的变量

exec sp_executesql @tempsql,@parm,@s=@sum output--执行系统存储过程sp_executesql,将记录总条数返回给@sum变量

set @sumPage=ceiling(@sum/@pageSize)--将@sumPage进一

declare @sql nvarchar(1000)--定义sql语句

if(@isDesc<>0)--如果需要降序查询
begin
set @colName=@colName
end

set @sql='select '+@ReturnCol+' from
(select top '+str(@pageSize)+' * from
(select row_number() over (order by '+@colName+') as num,'+@tableName+'.* from '+@tablename+')
as tempTable where num > '+str((@pageIndex-1)*@pageSize)+')
as tmp '

if(@where<>'')--如果where条件不为空
begin
set @sql=@sql+' where '+@where
end
--if(@areaId<>'')
-- begin
-- set @sql=@sql+' where '+@where + 'and AreaId= ' + @areaId
-- end

exec(@sql)--执行sql语句

return @sumPage--返回总页数
go

declare @sumPage int
declare @sum int
exec @sumPage=pageQuery
'Id,TelType,ClientId,AreaId,DeptName,OutStoreDate,TelDate,ClientPerson',--返回的列
30,--每页存放的数据数量
'Id',--照指定列排序
'Tbl_InputInfor',--对指定表操作
1,--第几页
1,--非0代表降序
'',--where条件
--'SH',
@sum output

select @sumPage as '总页数'
select @sum as '总记录'

报错如下:
消息 8114,级别 16,状态 5,过程 pageQuery,第 0 行
从数据类型 varchar 转换为 int 时出错。

(1 行受影响)
(1 行受影响)
请教各位大侠了~!
...全文
59 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lijunfeng 2010-03-02
  • 打赏
  • 举报
回复
这样你的第三个参数是 'Id' 当不能转换为int
lijunfeng 2010-03-02
  • 打赏
  • 举报
回复
下面这段里是不是少传了第一个参数
exec @sumPage=pageQuery
'Id,TelType,ClientId,AreaId,DeptName,OutStoreDate,TelDate,ClientPerson',--返回的列
30,--每页存放的数据数量
'Id',--照指定列排序
'Tbl_InputInfor',--对指定表操作
1,--第几页
1,--非0代表降序
'',--where条件
--'SH',
@sum output

22,210

社区成员

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

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