导航
  • 主页
  • 基础类
  • 应用实例
  • 新技术前沿

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

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这样出错,请问怎么办,谢谢!
...全文
99 点赞 收藏 13
写回复
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
看老龟给你答案吧 哈哈
回复
发动态
发帖子
MS-SQL Server
创建于2007-09-28

3.2w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
社区公告
暂无公告