T-SQL动态别名,不使用动态SQL

时间:2013-02-25 17:04:06

标签: sql-server-2008 tsql

我需要根据场景命名列别名

declare @testing as varchar(max)
set @testing = 'choice'

select 1 as case when @testing = 'choice' then 'chose' else 'didntChoose' end

因此,如果@testing ='choice',结果将如下所示:

chose
1

否则:

didntChoose
1

是否可以执行此操作没有动态SQL

3 个答案:

答案 0 :(得分:3)

不,除非使用动态SQL,否则无法根据值更改别名的名称。

选择列时,每列只能有一个名称/别名。

如果你想要不同的列名,那么你可以使用类似下面的一些使用不同的select语句:

IF @testing = 'choice'
    select 1 as 'Chose'
ELSE 
    select 1 as 'didntChoose'   

或者您可以返回两个单独的列:

select 
    case when @testing = 'choice' then 1 else 0 end Chose,
    case when @testing <> 'choice' then 1 else 0 end DidNotChose

答案 1 :(得分:2)

这是我写的那种实现目标的东西,但它不是我做过的最优雅的工作。

各种客户希望为与其资源相关联的属性显示不同的值。因此,存在一个通用表,允许每个客户为每个资源分配不同的值。

让我们先创建结构并填充它们。没什么太花哨的:

create table CustResource (
CustId int,
Attr1 varchar(50),
Attr2 varchar(50),
Attr3 varchar(50),
Attr4 varchar(50),
Attr5 varchar(50))

insert into CustResource (CustId, attr1, attr2, attr3, attr4) values (1, 'Div','Dept','Machine Type','Main Usage')

/* What just happened above is that the customer assigned display values to the first 4 attributes only */

create table PortalResource (
ResourceId int,
custId     int,
ResourceName varchar(50),
Attr1 varchar(50),
Attr2 varchar(50),
Attr3 varchar(50),
Attr4 varchar(50),
Attr5 varchar(50))


insert into PortalResource (ResourceId, CustId, ResourceName, attr1, attr2, attr3, attr4) 
   values (10,1,'abcd1234','Local Government','State Emergency Services','File Server','Production')
insert into PortalResource (ResourceId, CustId, ResourceName, attr1, attr2, attr3, attr4) 
   values (11,1,'bcde2345','Local Government','State Emergency Services','Database Server','Production')
insert into PortalResource (ResourceId, CustId, ResourceName, attr1, attr2, attr3, attr4) 
   values (12,1,'bcde2346','Local Government','Department of Education','Domain Controller','Production')

/* Notice in the above that attr5 is not populated. This is deliberate! */

/* OK, now we want to accept the customer Id (I have hard-coded it here for quick reference, but you get the point) */

declare @SQLString varchar(1000)
    , @attr1 varchar (50)
    , @attr2 varchar(50)
    , @attr3 varchar(50)
    , @attr4 varchar(50)
    , @attr5 varchar(50)
    , @CustId varchar(10)

set @CustId = 1
select @attr1 = upper(attr1)
     , @attr2 = upper(attr2)
     , @attr3 = upper(attr3)
     , @attr4 = upper(attr4 )
     , @attr5 = UPPER(attr5)
     , @CustId = convert(varchar,custId)
from CustResource where custid = @CustId

set @SQLString = 'Select '  + @CustId + 'as CustomerID'
If @attr1  is not null set @SQLString = @SQLString  + 
              ' , attr1 as '   + '"' + @attr1 + '"' 
If @attr2  is not null set @SQLString = @SQLString  + 
              ' , attr2 as ' + '"' + @attr2 + '"' 
If @attr3  is not null set @SQLString = @SQLString  + 
              ' , attr3 as ' + '"' + @attr3 + '"' 
If @attr4  is not null set @SQLString = @SQLString  + 
              ' , attr4 as ' + '"' + @attr4 + '"' 
If @attr5  is not null set @SQLString = @SQLString  + 
              ' , attr5 as ' + '"' + @attr5 + '"' 
Set @SQLString = @SQLString + ' from PortalResource where CustId = ' + @CustId 

print @SQLString

exec (@SQLString)

这很有魅力,但它超级ugleeeeee !!!!

答案 2 :(得分:1)

我将在此留下http://www.sommarskog.se/dynamic_sql.html#columnalias

  

首先将数据放入临时表,然后使用sp_rename   根据您的需要重命名列。 (您需要限定sp_rename   使用tempdb使其在该数据库中运行。)

如果你在使用动态SQL进行大量处理时,请阅读他的网站,如果你不小心的话,有很多方法可以自己拍摄...