473,776 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10391

"eric" <en***@m-net.arbornet.or g> wrote in message
news:cc******** *************** **@posting.goog le.com...
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.bluewi n.ch>...
"eric" <en***@m-net.arbornet.or g> wrote in message
news:cc******** *************** **@posting.goog le.com...
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
3186
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 FLD-ID = @inputVAR UPDATE TABLE2 SET FLDx = @var1, FLDy = @var2
4
15839
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
3854
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 work. Yet transcript 2 shows that the stored procedure does its job, if only the table name is provided directly in the statement text string, instead of as a host variable to a prepared statement like in 'EXECUTE stmt USING tabName, fldValue'. --...
14
9117
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 supply a parameter like: DROP TABLE Sheet1
7
6992
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 into the GUI because it is hard to describe in text. First of all what is the purpose of ALTOBJ()? This procedure was created mostly for ISVs who need to do produce change scripts to upgrade application from release to release, but it can also
1
7552
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 having the sql statement dynamically built according to the input provided by the user. I have used the method described here hundreds of times it is quick and adaptive. I generally use a frames page for the search, in this way the search is maintained...
3
2846
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 programmatically set, this is necessary because I may have a simple table name or a union, hence I got to pick the correct one! Unfortunately, I can't bind this parameter to a control, session or
0
1759
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 with the orginal data changes from project to project), I have another reference table called Ref_Fields (this holds the fields I will need to pull out of the tables they are in.) I know there is a better way to code some of this so if you happen...
5
2113
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 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 with the orginal data changes from project to project), I have another reference table called Ref_Fields...
0
9459
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10056
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9920
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8948
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7467
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5489
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3619
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2857
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.