February 26, 2015 at 3:02 pm
Hi,
Is there a way to display a column alias as part of the result set column labels?
February 26, 2015 at 3:15 pm
I'm not sure what you're talking about. Can you be more explicit? Where do you want to display the column alias?
February 26, 2015 at 3:45 pm
select [Table Name ] = name,
[Date created]= create_date
from sys.tables
select [My current SPID]= @@spid
February 27, 2015 at 6:08 am
Instead, I'd go with a more standard approach:
select name AS [Table Name],
create_date AS [Date created]
from sys.tables;
select @@spid AS [My current SPID];
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
February 27, 2015 at 6:46 am
I don't think I explained this very well..let me try to do a better job.
A developer came to me yesterday and asked if he can reference the table/column alias he has created so that it is easier for him to discern.
So, the dev want to display the column alias name along with the actual column name.
I think I may have found something close:
select alias, t.COLUMN_name
from
(
select VC.COLUMN_NAME,
case when
ROW_NUMBER () OVER (
partition by C.COLUMN_NAME order by
CHARINDEX(',',VIEW_DEFINITION,CHARINDEX(C.COLUMN_NAME,VIEW_DEFINITION))-
CHARINDEX(VC.COLUMN_NAME,VIEW_DEFINITION)
) = 1
then 1
else 0 end
as lenDiff
,C.COLUMN_NAME as alias
,CHARINDEX(',',VIEW_DEFINITION,CHARINDEX(C.COLUMN_NAME,VIEW_DEFINITION)) diff1
, CHARINDEX(VC.COLUMN_NAME,VIEW_DEFINITION) diff2
from INFORMATION_SCHEMA.VIEW_COLUMN_USAGE VC
inner join INFORMATION_SCHEMA.VIEWS V on V.TABLE_NAME = 'v'+VC.TABLE_Name
inner join information_schema.COLUMNS C on C.TABLE_NAME = 'v'+VC.TABLE_Name
where VC.TABLE_NAME = 'My_Table'
and CHARINDEX(',',VIEW_DEFINITION,CHARINDEX(C.COLUMN_NAME,VIEW_DEFINITION))-
CHARINDEX(VC.COLUMN_NAME,VIEW_DEFINITION) >0
)
t
where lenDiff = 1
February 27, 2015 at 8:09 am
Yeah, a derived table or a Common Table Expression would allow you to do that. Sorry I didn't understand what you were asking.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
February 27, 2015 at 2:24 pm
You can also use CROSS APPLY to do that:
select VC.COLUMN_NAME,
case when
ROW_NUMBER () OVER (
partition by C.COLUMN_NAME order by
CHARINDEX(',',VIEW_DEFINITION,CHARINDEX(C.COLUMN_NAME,VIEW_DEFINITION))-
CHARINDEX(VC.COLUMN_NAME,VIEW_DEFINITION)
) = 1
then 1
else 0 end
as lenDiff
,alias
,CHARINDEX(',',VIEW_DEFINITION,CHARINDEX(C.COLUMN_NAME,VIEW_DEFINITION)) diff1
, CHARINDEX(VC.COLUMN_NAME,VIEW_DEFINITION) diff2
from INFORMATION_SCHEMA.VIEW_COLUMN_USAGE VC
inner join INFORMATION_SCHEMA.VIEWS V on V.TABLE_NAME = 'v'+VC.TABLE_Name
inner join information_schema.COLUMNS C on C.TABLE_NAME = 'v'+VC.TABLE_Name
cross apply (
select C.COLUMN_NAME as alias
) as assign_alias_names
where VC.TABLE_NAME = 'My_Table'
and CHARINDEX(',',VIEW_DEFINITION,CHARINDEX(C.COLUMN_NAME,VIEW_DEFINITION))-
CHARINDEX(VC.COLUMN_NAME,VIEW_DEFINITION) >0
Edit:
What's really uber-cool is that a subsequent CROSS APPLY can use a previous alias, i.e., you can alias based on an alias:
CROSS APPLY ( SELECT a+b AS c ) AS ca1
CROSS APPLY ( SELECT c + d AS e ) AS ca2
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
February 27, 2015 at 2:26 pm
Grant Fritchey (2/27/2015)
Yeah, a derived table or a Common Table Expression would allow you to do that. Sorry I didn't understand what you were asking.
my mistake Grant - I didn't explain very well. Thanks for trying.
February 27, 2015 at 2:27 pm
ScottPletcher (2/27/2015)
You can also use CROSS APPLY to do that:
select VC.COLUMN_NAME,
case when
ROW_NUMBER () OVER (
partition by C.COLUMN_NAME order by
CHARINDEX(',',VIEW_DEFINITION,CHARINDEX(C.COLUMN_NAME,VIEW_DEFINITION))-
CHARINDEX(VC.COLUMN_NAME,VIEW_DEFINITION)
) = 1
then 1
else 0 end
as lenDiff
,alias
,CHARINDEX(',',VIEW_DEFINITION,CHARINDEX(C.COLUMN_NAME,VIEW_DEFINITION)) diff1
, CHARINDEX(VC.COLUMN_NAME,VIEW_DEFINITION) diff2
from INFORMATION_SCHEMA.VIEW_COLUMN_USAGE VC
inner join INFORMATION_SCHEMA.VIEWS V on V.TABLE_NAME = 'v'+VC.TABLE_Name
inner join information_schema.COLUMNS C on C.TABLE_NAME = 'v'+VC.TABLE_Name
cross apply (
select C.COLUMN_NAME as alias
) as assign_alias_names
where VC.TABLE_NAME = 'My_Table'
and CHARINDEX(',',VIEW_DEFINITION,CHARINDEX(C.COLUMN_NAME,VIEW_DEFINITION))-
CHARINDEX(VC.COLUMN_NAME,VIEW_DEFINITION) >0
Edit:
What's really uber-cool is that a subsequent CROSS APPLY can use a previous alias, i.e., you can alias based on an alias:
CROSS APPLY ( SELECT a+b AS c ) AS ca1
CROSS APPLY ( SELECT c + d AS e ) AS ca2
nice thanks Scott
February 27, 2015 at 2:41 pm
Grant Fritchey (2/27/2015)
Instead, I'd go with a more standard approach:
Heh... why? Portability? 😛
--Jeff Moden
Change is inevitable... Change for the better is not.
February 28, 2015 at 11:59 am
Jeff Moden (2/27/2015)
Grant Fritchey (2/27/2015)
Instead, I'd go with a more standard approach:Heh... why? Portability? 😛
Yes Joe.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply