游标使用问题

DG_1 2005-05-07 10:03:38
declare @zdhm varchar(50)
set @zdhm='zd00047' --更改单引号中的制单号码
declare tb cursor for
Select name from sysobjects Where xtype='U'
declare @name sysname
open tb
fetch next from tb into @name
while @@fetch_status=0
begin
delete from @name where zdhm=@zdhm
fetch next from tb into @name
end
close tb
deallocate tb

错误提示如下
服务器: 消息 137,级别 15,状态 2,行 10
必须声明变量 '@name'。

我不是已声明变量了么,请问怎样解决,谢谢!
...全文
124 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
DG_1 2005-05-07
  • 打赏
  • 举报
回复
:)多谢各位!!!
orcale 2005-05-07
  • 打赏
  • 举报
回复
delete from @name where zdhm=@zdhm=>
要寫成動態sql
exec('delete from '+@name +' where zdhm='+''''+@zdhm+'''')
lengxiaowei 2005-05-07
  • 打赏
  • 举报
回复
变量不能直接放SQL里面使用,
要通过字符串的形式来使用,

支持邹键大哥的
zjcxc 元老 2005-05-07
  • 打赏
  • 举报
回复
declare @zdhm varchar(50)
set @zdhm='zd00047' --更改单引号中的制单号码
declare tb cursor for
Select name from sysobjects Where xtype='U'
declare @name sysname
open tb
fetch next from tb into @name
while @@fetch_status=0
begin
exec('delete from '+@name+' where zdhm='''+@zdhm+'''')
fetch next from tb into @name
end
close tb
deallocate tb
jsldhy 2005-05-07
  • 打赏
  • 举报
回复
delete from @name where zdhm=@zdhm
这一句是个不小的错误噢!
delete ?
from @name ???
我也是一个小小的菜鸟,但是好象declare 是定义变量的,定义临时表以#开始?!
我建议:去掉declare @name sysname这一句
然后把@name都改为#name。OK!

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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