Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 table name as variable

Author  Topic 

KlausEngel
Yak Posting Veteran

85 Posts

Posted - 2010-07-20 : 18:02:49
I have a legacy database to deal with with very long table name. All table names star out with "[Department Form Name$" and then the specification of the table for example "Order Header]".
Is there a way I can set a variable for te first part of the table name so I don't have to write out the full name? Just a matter of convenience.
Thanks.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-20 : 18:06:56
You can do that with dynamic SQL, however it will not perform as well and will not be as secure.

Using dynamic SQL will not make your code easier. In fact it'll make it more complicated.

Alternatively, you could choose to wrap the tables with views that have a shorter name.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2010-07-20 : 19:09:29
Or use synonyms (SQL 2005 or higher) with shorter names.
Go to Top of Page
   

- Advertisement -