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
 General SQL Server Forums
 New to SQL Server Programming
 Can the name of a table be a variable?

Author  Topic 

dmilam
Posting Yak Master

185 Posts

Posted - 2010-03-23 : 18:32:58
Not to discuss table variables, but to discuss whether the name of a table can be declared as a string for substitution throughout a script

declare @tablename as varchar(10)
set @tablename = 'tablename';

create table @tablename (id int not null)
insert into @tablename
...

--obviously a table is not the same data type as varchar, but CAST doesn't seem to work for this kind of conversion

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2010-03-23 : 18:47:10
It can. You would need to use dynamic SQL and execute it.
Read up on dynamic SQL.

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-03-23 : 18:55:35
Be warned that it is not good practice to use code like that for performance and security reasons.

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

Subscribe to my blog
Go to Top of Page

dmilam
Posting Yak Master

185 Posts

Posted - 2010-03-23 : 19:06:13
Thanks, good to know!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-25 : 02:58:45
But make sure you read this article fully
www.sommarskog.se/dynamic_sql.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

dmilam
Posting Yak Master

185 Posts

Posted - 2010-08-02 : 17:12:33
Creating a synonym is perfect for this. Exciting!

http://www.sommarskog.se/dynamic_sql.html#Dyn_DB

http://msdn.microsoft.com/en-us/library/ms177544.aspx
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2010-08-02 : 18:43:55
In a properly designed schema, a table models a set of ONE AND ONLY ONE KIND entities. Switching the names in and out as parameter would be like doing the same thing to automobiles, squids, etc. A magical generic makes no sense.

If you really want to write bad code, you can use dynamic SQL.

--CELKO--
Joe Celko, SQL Guru
Go to Top of Page

dmilam
Posting Yak Master

185 Posts

Posted - 2010-08-03 : 13:19:59
quote:
In a properly designed schema, a table models a set of ONE AND ONLY ONE KIND entities. Switching the names in and out as parameter would be like doing the same thing to automobiles, squids, etc. A magical generic makes no sense.

If you really want to write bad code, you can use dynamic SQL.



Should a table name never be the name of a required string used elsewhere in the query?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-03 : 13:26:53
dmilam, I'm not clear on your question. Could you post a sample?

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

Subscribe to my blog
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2010-08-03 : 19:45:33
>>Should a table name never be the name of a required string used elsewhere in the query? <<

A table name cannot be a reserved word.

A table name should be a collective or plural noun for the set of entities it models. For example:

Employee = bad
Employees = better
Personnel = best

But the bad news is that a table name can be the same a column name or variable. It is a flaw in the language. Do not write code that way.




--CELKO--
Joe Celko, SQL Guru
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-04 : 10:57:40
A table name can be a reserved word, but you've got to put square brackets around it when querying it. I've got one such table, and I hate it! I'm not the one who named it, so don't blame me. I'd rename it right now if I could.

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

Subscribe to my blog
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-08-04 : 11:42:39
quote:
Originally posted by jcelko

>>Should a table name never be the name of a required string used elsewhere in the query? <<

A table name cannot be a reserved word.

A table name should be a collective or plural noun for the set of entities it models. For example:

Employee = bad
Employees = better
Personnel = best

But the bad news is that a table name can be the same a column name or variable. It is a flaw in the language. Do not write code that way.

Joe, I don't want to get into the Singular versus Plural table names. As I have my own opinion and I think people that have a very solid relation background are, mostly, of the same opinion.

However, I've seen you toss around the 11179 ISO standard for naming and I can only find them naming Classes, which I assume to be the equivalent of an Entity and they do them as singular names. So, why would you give out bad information about how to name Entities? When I say "bad" I'm referring to Emploees being better than Employee.
Go to Top of Page

dmilam
Posting Yak Master

185 Posts

Posted - 2010-08-06 : 00:21:33
Example:


-- Set name of current shipment
DECLARE @ShipmentName AS VARCHAR(25);
SET @ShipmentName = 'Shipment1876';

-- Create a synonym for the table for the current calling user
DECLARE @Sql NVARCHAR(200)
SET @Sql = '
DROP SYNONYM TBL;
CREATE SYNONYM TBL
FOR T.['+(SELECT USER_NAME())+'].'+@ShipmentName
EXEC sp_executesql @Sql


Here the shipment name will vary each time the code needs to be run. (In a few weeks, the shipment name will be different). @ShipmentName appears elsewhere in the query because it will become a column name in a spreadsheet. It is not a column name in any table.
Go to Top of Page
   

- Advertisement -