Re: Creating an alias

From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 02/15/05


Date: Tue, 15 Feb 2005 16:53:36 +0100

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)


Relevant Pages

  • Re: Query doesnt always sort
    ... If the query was "within" SQL Server, then alias ... gets treated differently than in a Jet query. ...
    (microsoft.public.access.queries)
  • Trying to find a maximum value amongst 3 columns in a dB row
    ... The column prefix 'a1' does not match with a table name or alias name ... used in the query. ... Either the table is not specified in the FROM clause ...
    (comp.databases.sybase)
  • Re: Forcing alias in columnNames
    ... the query is quite complex and Access says so. ... All works quite well, the queries are automatically created, the glueing is ... >> is there a way to force Access to keep an alias in the column Name even ... >> duplicated ColumnName. ...
    (microsoft.public.access.queries)
  • Re: Correlated subquery fails when using a Table on Linked Server
    ... you may consider opening a support incident. ... >The column prefix 'a' does not match with a table name or alias name used in the query. ...
    (microsoft.public.sqlserver.server)
  • Re: Export Data to Excel Problems
    ... The export process uses the field name (or the alias for the field name) ... So use an alias in the query that has the desired ... an excel spreadsheet, which is then emailed to a supervisory group. ... The headings that come across are the call ...
    (microsoft.public.access.externaldata)