日期:2014-05-17 浏览次数:20446 次
exec('select '+@getavg+' = avg('+@a+') from buguding')
declare @getavg decimal(18,4),@a sysname --@a賦值略 declare @s nvarchar(4000) set @s='select @getavg = avg('+@a+') from buguding' exec sp_executesql @s,N'@getavg decimal(18,4) output',@getavg output select @getavg
------解决方案--------------------
create table test0722(id int) insert into test0722 select 10 union all select 20 declare @getavg varchar(20) set @getavg='newcolname' declare @a varchar(20) set @a='id' exec('select '+@getavg+' = avg('+@a+') from test0722') /* newcolname ----------- 15 */
------解决方案--------------------
楼主是要得到变量值输出,不是要传入别名,那就需要用sp_executesql了
declare @a int; declare @sql nvarchar(4000); set @a=1; SET @sql='select @b set @b=@b+1 select @b'; exec sp_executesql @sql, N'@b int output', @b=@a;
------解决方案--------------------
declare @aa int
exec sp_executesql N'select @a=SUM(d) from table_1',N'@a int out',@aa out
print @aa
按照这个模式来。。。