大家来帮一下吧!晕死了!

JadyWang 2003-10-19 09:56:23
declare @LastRecord int

/*
declare @TableName varchar(20)
declare @IndexCol char(10)
declare @CurrentPage int
declare @PageCount int
set @TableName='QQ_Person_Online'
set @IndexCol='IndexCol'
set @CurrentPage=1
set @PageCount=2
exec('set @LastRecord=(select top 100 '+@IndexCol+' from '+@TableName+' where '+@IndexCol+'='+@CurrentPage+'*'+@PageCount+' order by '+@IndexCol+')')
*/

/*
set @LastRecord=(select top 100 IndexCol from QQ_Person_Online where IndexCol=1*2 order by IndexCol)
*/
select @LastRecord
问题如下:
若执行第一部分注释中的句子,则报错:”服务器: 消息 137,级别 15,状态 1,行 1
必须声明变量 '@LastRecord'。

(所影响的行数为 1 行)

若执行第二部分注释中的句子则正确,百思不得解?!





...全文
45 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
JadyWang 2003-10-19
  • 打赏
  • 举报
回复
是吗?哦,我是看book online 上说的
newdongkui 2003-10-19
  • 打赏
  • 举报
回复
没有啊,好用这呢。
JadyWang 2003-10-19
  • 打赏
  • 举报
回复
回”老乌鸦“:
好象在sql-2000中sp_sqlexec被弃用了
JadyWang 2003-10-19
  • 打赏
  • 举报
回复
回“平平淡淡”:
(1)看似不对,但我的IndexCol是identity的,select top 100 IndexCol from QQ_Person_Online where IndexCol=1*2 order by IndexCol的返回值其实是唯一的!而top 100
只是为了后面的order by IndexCol(好像是sql的规定,否则报错)
(2)declare @IndexCol char(10)只是作为参数给出列名,而IndexCol列的数据为int identity(1,1)
JadyWang 2003-10-19
  • 打赏
  • 举报
回复
哦!呵呵。
我原意是将set @LastRecord=(select top 100 IndexCol from QQ_Person_Online where IndexCol=1*2 order by IndexCol)
写成:
exec('set '+@LastRecord+'=(select top 100 '+@IndexCol+'from '+@TableName+'where '+@IndexCol+'='+@CurrentPage+'*'+@PageCount+'order by '+@IndexCol+')')
因为在存储过程里面上一句不能执行。可是报错:
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: '=' 附近有语法错误。


welyngj 2003-10-19
  • 打赏
  • 举报
回复
老兄啊,先不说你的问题,我发现你的脚本错误之处:
@LastRecord变量,存储单个值
而select top 100 IndexCol from QQ_Person_Online where IndexCol=1*2 order by IndexCol的结果是个集合。
把集合赋给一个单个值,行吗?
declare @LastRecord int
declare @IndexCol char(10)
数值类型又不相同。
newdongkui 2003-10-19
  • 打赏
  • 举报
回复
看看 sp_sqlexec 这个,别用exec
Ouyangyifeiczy 2003-10-19
  • 打赏
  • 举报
回复
@LastRecord 变量需在EXEC('declare @LastRecord ....')中声明。

22,209

社区成员

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

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