473,508 Members | 2,236 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ALTER table command

Hi,

I'm trying to run the ALTER TABLE command using a dynamic string for the
table, like so:

DECLARE @TableName CHAR
SET @TableName = 'Customers'
ALTER TABLE @TableName
ADD ...blah

Is this possible? We know this works:

ALTER TABLE Customers ADD ...blah

It looks like I need a way to convert the CHAR value to a literal or perhaps
even a table ID?

Thanks in advance,
Paul
Jul 20 '05 #1
3 10564
oj
You would need to use dynamic query...

e.g.
exec('alter table '+@tb+' add blah')
--
-oj
RAC v2.2 & QALite!
http://www.rac4sql.net

"Paul Sampson" <ps******@uecomm.com.au> wrote in message
news:10***************@proxy.uecomm.net.au...
Hi,

I'm trying to run the ALTER TABLE command using a dynamic string for the
table, like so:

DECLARE @TableName CHAR
SET @TableName = 'Customers'
ALTER TABLE @TableName
ADD ...blah

Is this possible? We know this works:

ALTER TABLE Customers ADD ...blah

It looks like I need a way to convert the CHAR value to a literal or perhaps even a table ID?

Thanks in advance,
Paul

Jul 20 '05 #2
"Paul Sampson" <ps******@uecomm.com.au> wrote in message news:<10***************@proxy.uecomm.net.au>...
Hi,

I'm trying to run the ALTER TABLE command using a dynamic string for the
table, like so:

DECLARE @TableName CHAR
SET @TableName = 'Customers'
ALTER TABLE @TableName
ADD ...blah

Is this possible? We know this works:

ALTER TABLE Customers ADD ...blah

It looks like I need a way to convert the CHAR value to a literal or perhaps
even a table ID?

Thanks in advance,
Paul


You can use dynamic SQL:

declare @tablename sysname
set @tablename = 'Customers'
exec('alter table dbo.' + @tablename + ' add ...')

See here for more information on dynamic SQL:

http://www.algonet.se/~sommar/dynamic_sql.html

By the way, if you declare a variable as CHAR without a length, it
will default to CHAR(1). For object names, sysname is a better choice.

Simon
Jul 20 '05 #3
Thanks Simon - a common suggestion and one that I'll be sure to remember.

"Simon Hayes" <sq*@hayes.ch> wrote in message
news:60**************************@posting.google.c om...
"Paul Sampson" <ps******@uecomm.com.au> wrote in message

news:<10***************@proxy.uecomm.net.au>...
Hi,

I'm trying to run the ALTER TABLE command using a dynamic string for the
table, like so:

DECLARE @TableName CHAR
SET @TableName = 'Customers'
ALTER TABLE @TableName
ADD ...blah

Is this possible? We know this works:

ALTER TABLE Customers ADD ...blah

It looks like I need a way to convert the CHAR value to a literal or perhaps even a table ID?

Thanks in advance,
Paul


You can use dynamic SQL:

declare @tablename sysname
set @tablename = 'Customers'
exec('alter table dbo.' + @tablename + ' add ...')

See here for more information on dynamic SQL:

http://www.algonet.se/~sommar/dynamic_sql.html

By the way, if you declare a variable as CHAR without a length, it
will default to CHAR(1). For object names, sysname is a better choice.

Simon

Jul 20 '05 #4

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

Similar topics

10
160506
by: Jane | last post by:
Does any one know why this statement is failing? db2 => ALTER TABLE ELMT_T ALTER COLUMN CDTY_CD SET DATA TYPE VARCHAR(51) DB21034E The command was processed as an SQL statement because it was...
10
26040
by: BuddhaBuddy | last post by:
Platform is DB2/NT 7.2.9 The table was created like this: CREATE TABLE MYTEST ( MYTESTOID bigint not null primary key, FK_OTHEROID bigint not null references other, FK_ANOTHEROID bigint not...
5
5284
by: minjie | last post by:
Is it possible to run a simple script to alter a table column in Access database from an interger to a double? I have been writing C++ programs every time we need to upgrade (modify) the Access...
3
2565
by: Michael Charney | last post by:
I am trying to alter a table that I imported data to. I can import the data just fine but when I run the following command: ALTER TABLE tblimport ADD COLUMN admit_date SmallDateTime It gives...
2
31435
by: RamaKrishna Narla | last post by:
In MS SQL Server, I have the following tables with some data in it. create table table1 ( column1 varchar(32), column2 int not null, column10 varchar(255), ..... primary key (column1,...
1
3474
by: vasilip | last post by:
I'm testing out db2 for a project I'm starting that requires proper xml support and I can't seem to get both xml and spatial data to work well in the same table. Once having created a table...
3
9199
by: Gregor KovaĨ | last post by:
Hi! I have a table with a column of BIGINT and I want to change the type to SMALLINT. Command ALTER TABLE TABLE1 ALTER COLUMN COL1 SET DATA TYPE SMALLINT does not work since I cannot specify...
3
6661
by: Rahul B | last post by:
Hi, I have a user UCLDEV1 which is a part of staff and a group(db2schemagrp1) to which i have not given any permissions. The authorizations of that user are shown as db2 =get authorizations...
0
2088
by: ccoq | last post by:
I received the below error message when I ran this command ALTER TABLE WAC.SUPPRESSED_CUSTOMER ALTER COLUMN CUSTOMER_ID SET DATA TYPE VARCHAR(28) DB21034E The command was processed as an SQL...
0
7227
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
7127
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
7331
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,...
0
7501
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...
0
5633
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,...
1
5056
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...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
424
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...

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.