all groups > sql server mseq > february 2005 >
You're in the

sql server mseq

group:

Creating an alias



Creating an alias Bruce Parker
2/15/2005 7:25:06 AM
sql server mseq: 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?
Re: Creating an alias Hugo Kornelis
2/15/2005 4:53:36 PM
[quoted text, click to view]

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
--

AddThis Social Bookmark Button