Thursday, March 29, 2012

Creating an alias

I have an alias that can get very long because it maintains a information of
where it came from. Is there a way to alias an alias?
My first thought was:
Declare @.T1 varchar(128)
Set T1 = 'MyAlias'
...
Inner Join Table1 as @.T1
This of course does not work. Is there way?
On Tue, 15 Feb 2005 07:25:06 -0800, Bruce Parker wrote:

>I have an alias that can get very long because it maintains a information of
>where it came from. Is there a way to alias an alias?
>My first thought was:
>Declare @.T1 varchar(128)
>Set T1 = 'MyAlias'
>...
>Inner Join Table1 as @.T1
>This of course does not work. Is there way?
Hi Bruce,
Not without using dynamic SQL.
But I fail to see why you would want this. A table alias is never visible
to the outside user, it's only used as part of your query.
If you choose an alias that has some added value, like indicating where
something came from, then that's good - it can make your queries eaasier
to maintain.
If such an alias gets long, then so be it. Either you keep it, paying the
price of a long alias for the gain of a self-documenting query - or you
shorten the alias, paying the price of a less self-documenting query for
the gain of a shorter alias.
But I don't see how using a variable to store the alias would gain you any
benefit. I don't see how query #1 below would be easier to understand that
query #2.
Query #1 (syntactically not correct!!)
DECLARE @.T1 varchar(128)
SET @.T1 = 'MyAlias'
SELECT @.T1.Column1, @.T2.Column2
FROM Table1 AS @.T1
WHERE @.T1.Column3 > @.T1.Column4
Query #2
SELECT T1.Column1, T2.Column2
FROM Table1 AS T1-- MyAlias
WHERE T1.Column3 > T1.Column4
Query #3
SELECT MyAlias.Column1, MyAlias.Column2
FROM Table1 AS MyAlias
WHERE MyAlias.Column3 > MyAlias.Column4
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)

No comments:

Post a Comment