存储过程局部变量赋值问题,请教一下!

xiaoding1 2006-03-25 09:37:49
这是一个用于AspNetPager的分页存储过程。我想把它写成通用的,就是给局部变量@RecordCount 赋值的时候该怎么写,诚心请教高手指点

CREATE procedure GetList
(@tablename varchar (255),--表名
@strGetFields varchar(1000) = '*',--查询字段
@fldName varchar(255)='', -- 排序的字段名
@key varchar(255), --主键
@strWhere varchar(1500) = '', -- 查询条件
@pagesize int,
@pageindex int,
@docount bit)
as
set nocount on
declare @RecordCount Int
exec('select '+@RecordCount+' = count(*) from '+@tablename+' where '+@strWhere+'')--这儿怎么给局部变量@RecordCount 赋值呀??
if(@docount=1)
select @RecordCount
else
begin
if(@pageindex=1)
exec('select top '+@pagesize+' '+@strGetFields+' from '+@tablename+' where '+@strWhere+' order by '+@key+' desc')
else
begin
declare @PageUpperBound int
declare @endrecords int
set @PageUpperBound=@pageindex*@pagesize
if(@PageUpperBound-@pagesize)>=@RecordCount
select ''
else if(@RecordCount-(@PageUpperBound-@pagesize)<=@pagesize)
begin
set @endrecords=@RecordCount-(@PageUpperBound-@pagesize)
exec('select * from (select top '+@endrecords+' '+@strGetFields+' from '+@tablename+' where '+@strWhere+' order by '+@key+')A order by '+@key+' desc')
end
else
exec('select * from (select top '+@pagesize+'* from (select top '+@PageUpperBound+' '+@strGetFields+' from '+@tablename+' where '+@strWhere+' order by '+@key+' desc)A order by '+@key+')B order by '+@key+' desc')
end
end
set nocount off
GO
...全文
347 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mschen 2006-03-25
  • 打赏
  • 举报
回复
--这样就应该没问题了.

declare @RecordCount int
declare @str nvarchar(1000)
set @str=N'select @RecordCount=count(*) from '+@tablename+' where '+@strWhere+''
exec sp_executesql @str,
N'@RecordCount int output',
@RecordCount output
select @RecordCount
xiaoding1 2006-03-25
  • 打赏
  • 举报
回复
还是不行呀。看来好要在这方面下大功夫呀
OracleRoob 2006-03-25
  • 打赏
  • 举报
回复

--------------------------------------------------------------------------------

动态sql语句基本语法
1 :普通SQL语句可以用Exec执行

eg: Select * from tableName
Exec('select * from tableName')
Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N

2:字段名,表名,数据库名之类作为变量时,必须用动态SQL

eg:
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。
Exec('select ' + @fname + ' from tableName') -- 请注意 加号前后的 单引号的边上加空格

当然将字符串改成变量的形式也可
declare @fname varchar(20)
set @fname = 'FiledName' --设置字段名

declare @s varchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句会报错



declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句正确

3. 输出参数
declare @num int,
@sqls nvarchar(4000)
set @sqls='select count(*) from tableName'
exec(@sqls)
--如何将exec执行结果放入变量中?

declare @num int,
--------------------------------------------------------------------------------

动态sql语句基本语法
1 :普通SQL语句可以用Exec执行

eg: Select * from tableName
Exec('select * from tableName')
Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N

2:字段名,表名,数据库名之类作为变量时,必须用动态SQL

eg:
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。
Exec('select ' + @fname + ' from tableName') -- 请注意 加号前后的 单引号的边上加空格

当然将字符串改成变量的形式也可
declare @fname varchar(20)
set @fname = 'FiledName' --设置字段名

declare @s varchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句会报错



declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句正确

3. 输出参数
declare @num int,
@sqls nvarchar(4000)
set @sqls='select count(*) from tableName'
exec(@sqls)
--如何将exec执行结果放入变量中?

declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from tableName '
exec sp_executesql @sqls,N'@a int output',@num output
select @num


@sqls nvarchar(4000)
set @sqls='select @a=count(*) from tableName '
exec sp_executesql @sqls,N'@a int output',@num output
select @num

mschen 2006-03-25
  • 打赏
  • 举报
回复
--上边的有点错误.这个就OK了.

declare @RecordCount int
exec sp_executesql N'select @RecordCount=count(*) from '+@tablename+' where '+@strWhere+'',
N'@RecordCount int output',
@RecordCount output
select @RecordCount
mschen 2006-03-25
  • 打赏
  • 举报
回复
--使用sp_executesql来实现.

sp_executesql N'select @RecordCount=count(*) from '+@tablename+' where '+@strWhere+'',
N'@RecordCount int output',
@RecordCount

22,209

社区成员

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

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