我需要一个select语句输出作为列的别名我们可以使用select查询输出列别名
是有可能
例子
select columnname as (select value from tablename) from tablename
输出
值
我需要一个select语句输出作为列的别名我们可以使用select查询输出列别名
是有可能
例子
select columnname as (select value from tablename) from tablename
输出
值
我认为你必须辞职去AS在TSQL的话,我的意思是:
select column name (select value from tablename) from tablename
我不认为它会工作:( – 2011-04-13 12:55:03
是的,我犯了一个错误,是就像Eon之前说过的,我在几分钟前试过了... – elvenbyte 2011-04-13 13:25:10
没有你,看到:
declare @a table (rowvalue varchar(20))
insert @a values ('aaa')
insert @a values ('bbb')
insert @a values ('ccc')
insert @a values ('ddd')
declare @x table (id int,rowvalue varchar(20))
insert @x values (1, 'wowwee')
insert @x values (2, 'noooo!')
select a.rowvalue as (select rowvalue from @x where id=1)
from @a a
OUTPUT:
Msg 102, Level 15, State 1, Line 11
Incorrect syntax near '('.
Msg 156, Level 15, State 1, Line 12
Incorrect syntax near the keyword 'from'.
你”将不得不使用动态sql来做到这一点。
编辑显示动态SQL:
declare @SQL varchar(500)
,@ColumnName varchar(20)
create table #a (rowvalue varchar(20))
insert #a values ('aaa')
insert #a values ('bbb')
insert #a values ('ccc')
insert #a values ('ddd')
declare @x table (id int,rowvalue varchar(20))
insert @x values (1, 'wowwee')
insert @x values (2, 'noooo!')
SELECT @ColumnName=rowvalue FROM @x where id=1
set @SQL='select a.rowvalue as '[email protected]+' from #a a'
exec(@SQL)
OUTPUT:
wowwee
--------------------
aaa
bbb
ccc
ddd
(4 row(s) affected)
动态sql就像??给我一些例子 – 2011-04-13 12:54:19
编辑答案以显示动态sql注意我必须将局部变量表@a更改为临时表#a,因为本地变量超出了EXEC()命令的范围,如果你使用的是常规永久表,那么这应该不是问题,我只是使用局部变量表,这样任何人都可以运行示例代码,而不需要向它们添加表系统。 – RacerX 2011-04-13 13:02:58
OP说, “有没有可能”。你有没有试图在SSMS中运行它?或者是真正的问题如何? – RacerX 2011-04-13 12:52:17
我只是想知道是否有可能或没有.am在SSMS中运行,并得到错误 – 2011-04-13 12:53:49