请问如何在表名后加变量?

tired_bird 2007-12-18 04:08:55
请问如何在表名后加变量?

例如
declare @i int
declare @v_n varchar(100) ,@fname varchar(100)
set @i = 1
while @i<= 10
begin
@fname = 'column1' + cast(@i as varchar)
update a
a.@fname = b.total
from dm a , dn b
where a.yyear=b.yyear and a.mmonth=1
end

但a.@fname这样出错,请问怎么办,谢谢!
...全文
147 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
tired_bird 2007-12-23
  • 打赏
  • 举报
回复
谢谢,问题按大家的办法得到解决了
tired_bird 2007-12-22
  • 打赏
  • 举报
回复
谢谢,己调通!
cxmcxm 2007-12-20
  • 打赏
  • 举报
回复
只能用动态执行
declare   @i   int   
declare @v_n varchar(100) ,@fname varchar(100)
declare @str1 varchar(500)
set @i = 1
while @i <= 10
begin
@fname = 'column1' + cast(@i as varchar)
set @str1='update a set '+@fname
+' = b.total
from dm a , dn b
where a.yyear=b.yyear and a.mmonth=1 '
exec (@str1)
end
tired_bird 2007-12-20
  • 打赏
  • 举报
回复
各位大侠帮帮忙
这几天我用潇洒老乌龟的
declare @i int
declare @v_n varchar(100)
declare @fname varchar(100)
declare @sql as varchar(100)
set @i = 1
while @i <= 10
begin
@fname = 'column1' + cast(@i as varchar)
set @sql = 'update a set ' + @fname + ' = b.total from dm a , dn b where a.yyear=b.yyear and a.mmonth=1'
exec(@sql)
set @i = @i + 1
end

和无枪狙击手的declare @i int
declare @v_n varchar(100) ,@fname varchar(100)
set @i = 1
while @i <= 10
begin
set @fname = 'column1' + cast(@i as varchar)
exec ('update a
set a.'+@fname+' = b.total
from dm a , dn b
where a.yyear=b.yyear and a.mmonth=1 ')
set @i=@i+1
end

方法都提示错误
难道是exec不能和update一起用吗?
请大家号号脉
等待中
areswang 2007-12-18
  • 打赏
  • 举报
回复
标记下来看看!
-狙击手- 2007-12-18
  • 打赏
  • 举报
回复
上面的回答都少了一个update后的 set
-狙击手- 2007-12-18
  • 打赏
  • 举报
回复
declare   @i   int   
declare @v_n varchar(100) ,@fname varchar(100)
set @i = 1
while @i <= 10
begin
set @fname = 'column1' + cast(@i as varchar)
exec ('update a
set a.'+@fname+' = b.total
from dm a , dn b
where a.yyear=b.yyear and a.mmonth=1 ')
set @i=@i+1
end
-狙击手- 2007-12-18
  • 打赏
  • 举报
回复
declare @i int
declare @v_n varchar(100) ,@fname varchar(100)
set @i = 1
while @i <= 10
begin
set @fname = 'column1' + cast(@i as varchar)
exec('update a '+
a.@fname +' = b.total
from dm a , dn b
where a.yyear=b.yyear and a.mmonth=1 ')
end
jinjazz 2007-12-18
  • 打赏
  • 举报
回复
declare   @i   int   
declare @v_n varchar(100) ,@fname varchar(100)
set @i = 1
while @i <= 10
begin
set @fname = 'column1' + cast(@i as varchar)
exec ('update a
a.'+@fname+' = b.total
from dm a , dn b
where a.yyear=b.yyear and a.mmonth=1 ')
set @i=@i+1
end
dawugui 2007-12-18
  • 打赏
  • 举报
回复
动态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
dawugui 2007-12-18
  • 打赏
  • 举报
回复
--要使用动态SQL.
declare @i int
declare @v_n varchar(100)
declare @fname varchar(100)
declare @sql as varchar(100)
set @i = 1
while @i <= 10
begin
@fname = 'column1' + cast(@i as varchar)
set @sql = 'update a set ' + @fname + ' = b.total from dm a dn b where a.yyear=b.yyear and a.mmonth=1'
exec(@sql)
set @i = @i + 1
end
wzy_love_sly 2007-12-18
  • 打赏
  • 举报
回复
要用动态语句,等老龟发,我也留一份
wzy_love_sly 2007-12-18
  • 打赏
  • 举报
回复
看老龟给你答案吧 哈哈

34,838

社区成员

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

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