473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Table Name in Variable - Create Statement

Is it possible to have part of a table name used in a CREATE statement
contained in a variable? Here's what I'd like to do, although
obviously the syntax of this isn't quite right or I wouldn't be here
asking:

DECLARE @TblPrefix char (3)
SET @TblPrefix = 'tst'
CREATE TABLE @TblPrefix + TestTable (col1 int)

The point there is to have a table named tstTestTable

The reason I need to do this is that my ISP will only give me one
database to work with and I'd like to have two copies of the
application I'm developing running at the same time. So I'd like to
run the sql script that creates the tables with TblPrefix set to "dev"
and then run it again with TblPrefix set to "liv"

thanks
eric
Jul 20 '05 #1
3 10367

"eric" <en***@m-net.arbornet.org> wrote in message
news:cc*************************@posting.google.co m...
Is it possible to have part of a table name used in a CREATE statement
contained in a variable? Here's what I'd like to do, although
obviously the syntax of this isn't quite right or I wouldn't be here
asking:

DECLARE @TblPrefix char (3)
SET @TblPrefix = 'tst'
CREATE TABLE @TblPrefix + TestTable (col1 int)

The point there is to have a table named tstTestTable

The reason I need to do this is that my ISP will only give me one
database to work with and I'd like to have two copies of the
application I'm developing running at the same time. So I'd like to
run the sql script that creates the tables with TblPrefix set to "dev"
and then run it again with TblPrefix set to "liv"

thanks
eric


If you check the syntax in Books Online, you'll see that CREATE TABLE
doesn't accept variables for the table name, so you need to use dynamic SQL:

exec('create table ' + @tblprefix + 'TestTable (col1 int)')

What you're doing is a reasonable case for a dynamic solution, but beware of
overusing it. See here for some more discussion:

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

You might also want to consider creating your objects with SQLDMO and
something like Perl, VB, C# etc., as it's often easier to set a .Name
property on the fly using a client language than it is to construct numerous
dynamic SQL statements.

Simon
Jul 20 '05 #2
eric wrote:
Is it possible to have part of a table name used in a CREATE statement
contained in a variable?


Nope. You'll have to build your SQL statement in a varchar variable and then
execute it by calling exec.

declare @TblPrefix char (3)
set @TblPrefix = 'tst'
exec('create table ' + @TblPrefix + 'TestTable (col1 int)')

--
Steve Troxell
Jul 20 '05 #3
"Simon Hayes" <sq*@hayes.ch> wrote in message news:<41**********@news.bluewin.ch>...
"eric" <en***@m-net.arbornet.org> wrote in message
news:cc*************************@posting.google.co m...
Is it possible to have part of a table name used in a CREATE statement
contained in a variable?


If you check the syntax in Books Online, you'll see that CREATE TABLE
doesn't accept variables for the table name, so you need to use dynamic SQL:


That's what I was afraid of. Dynamic SQL is going to be harder to
read and maintain so I think I'd better keep my original scripts
laying around.

BTW, of course I checked the books online. I'm not sure why you'd
even mention that.
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Perre Van Wilrijk | last post by:
Hello, I have 2 ways of updating data I'm using often 1) via a cursor on TABLE1 update fields in TABLE2 2) via an some of variables ... SELECT @var1=FLD1, @var2=FLD2 FROM TABLE1 WHERE...
4
by: maricel | last post by:
I have the following base table structure - DDL: CREATE TABLE "ADMINISTRATOR"."T1" ( "C1" INTEGER NOT NULL ) IN "TEST_TS" ; ALTER TABLE "ADMINISTRATOR"."T1" ADD PRIMARY KEY
2
by: claus.hirth | last post by:
I wrote a stored procedure that uses a prepared INSERT INTO statement in order to play with the PREPARE and EXECUTE keywords. In transcript 1 below the call to that stored procedure does not...
14
by: deko | last post by:
Can the DROP TABLE statement be used with a select or where statement? DROP TABLE SELECT * FROM tblTablesImported WHERE Import_ID Not In (SELECT FROM tblTablesInternal); Or do I have to...
7
by: Serge Rielau | last post by:
Hi all, Following Ian's passionate postings on problems with ALTOBJ and the alter table wizard in the control center I'll try to explain how to use ALTOBJ with this thread. I'm not going to get...
1
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and...
3
by: Andrea Raimondi | last post by:
Hello peers! I'm working on this application and I'm in need for some thoughtful advice :-p I have an SQLDataSource with params, select, etc. One of my params is the table name, which can be...
0
by: timber910 | last post by:
Ok, I have a button on a form that I'm using to create another table I will need later in my form. I have created a reference table called Ref_Table (holds my table names - using this as the tables...
5
by: timber910 | last post by:
I posted this in the VB forum but I think its in the wrong place. So I thought I would try here. _________________________________________ Ok, I have a button on a form that I'm using to create...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.