我需要根据场景命名列别名
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是否可以做到这一点?
我需要根据场景命名列别名
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是否可以做到这一点?
不,除非您使用动态 SQL,否则您无法根据值更改别名的名称。
选择列时,每列只能有一个名称/别名。
如果你想要不同的列名,那么你可以使用类似下面的一些使用不同的选择语句:
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
这是我写的达到目标的东西,但这不是我做过的最优雅的作品。
不同的客户希望显示与其资源关联的属性的不同值。因此,存在一个通用表,允许每个客户为每个资源分配不同的值。
让我们首先创建结构并填充它们。没什么太花哨的:
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 !!!
我将把它留在这里http://www.sommarskog.se/dynamic_sql.html#columnalias
您首先将数据放入临时表中,然后使用 sp_rename 根据需要重命名列。(您需要使用 tempdb 限定 sp_rename 以使其在该数据库中运行。)
如果您经常处理动态 SQL,请阅读他的网站,如果您不小心的话,有很多方法会让您自责...