June 14, 2012 at 9:27 am
I was surprised to not find this in BOL, and am having a hard time finding this online, so I'm wondering if this is possible . . .
Simple (I hope!) question: what is the syntax to programmatically create a table alias in T-SQL?
+--------------------------------------------------------------------------------------+
Check out my blog at https://pianorayk.wordpress.com/
June 14, 2012 at 9:33 am
There is no such object In SQL Server as Table Alias, so you can not create it by any means.
Could you please provide a bit more details about what are you trying to achieve?
June 14, 2012 at 9:33 am
table alias meaning synonym? or table alias in the middle of a SQL statement?
Lowell
June 14, 2012 at 9:36 am
Lowell (6/14/2012)
table alias meaning synonym? or table alias in the middle of a SQL statement?
Synonym.
For example: let's say I have an existing table called "myTable."
I instead want to refer to it as "AltTable."
So, my SELECT statement would be: select * from AltTable
where AltTable is pointing to (aliasing) myTable.
+--------------------------------------------------------------------------------------+
Check out my blog at https://pianorayk.wordpress.com/
June 14, 2012 at 9:37 am
I do need to create the table alias as a database object. I might not be using the right terminology.
+--------------------------------------------------------------------------------------+
Check out my blog at https://pianorayk.wordpress.com/
June 14, 2012 at 9:39 am
easy peasy:
CREATE SYNONYM MailView FOR msdb.dbo.sysmail_allitems
select * from MailView
* note a synonym must point to an object.
so you cannot try to make it part of a name, like getting it to replace LinkedServer.Databasename.Schema.
it has to point to an object, like table, view, etc that you'd find in sys.objects.
Lowell
June 14, 2012 at 9:42 am
Ahhh, no wonder why I couldn't find it in BOL!
Thanks, as always!
+--------------------------------------------------------------------------------------+
Check out my blog at https://pianorayk.wordpress.com/
June 14, 2012 at 7:13 pm
Lowell (6/14/2012)
easy peasy:
CREATE SYNONYM MailView FOR msdb.dbo.sysmail_allitems
select * from MailView
* note a synonym must point to an object.
so you cannot try to make it part of a name, like getting it to replace LinkedServer.Databasename.Schema.
it has to point to an object, like table, view, etc that you'd find in sys.objects.
Lowell - Another nice learning experience from you. That's now 2 in as many days!
I see this was introduced in SQL 2005. Any idea if it can be used in SQL 2005 running at compatibility level 80?
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
June 15, 2012 at 4:41 am
dwain.c (6/14/2012)
Lowell - Another nice learning experience from you. That's now 2 in as many days!
I see this was introduced in SQL 2005. Any idea if it can be used in SQL 2005 running at compatibility level 80?
Dwain i just checked, and it one of those commands that are not filtered based on compatibility level:
even if the database is set for 80, you can create or drop the synonym.
Lowell
June 15, 2012 at 5:59 am
Lowell (6/15/2012)
dwain.c (6/14/2012)
Lowell - Another nice learning experience from you. That's now 2 in as many days!
I see this was introduced in SQL 2005. Any idea if it can be used in SQL 2005 running at compatibility level 80?
Dwain i just checked, and it one of those commands that are not filtered based on compatibility level:
even if the database is set for 80, you can create or drop the synonym.
Well that's cool!
Both that it works and you were able to get back with an answer so quickly. I have a use for it but I was too busy today to check it out for myself.
Thanks.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
June 15, 2012 at 6:51 am
If you're truly stuck in a pre-2005 environment, you can also create a "pass through" view which will act as if it were a table of a different name (ie. alias).
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply