Groups | Blog | Home
all groups > sql server (alternate) > august 2003 >

sql server (alternate) : ALTER table command



sql NO[at]SPAM hayes.ch
8/27/2003 4:37:47 AM
[quoted text, click to view]

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.

oj
8/27/2003 7:09:26 AM
You would need to use dynamic query...

e.g.
exec('alter table '+@tb+' add blah')


--
-oj
RAC v2.2 & QALite!
http://www.rac4sql.net



[quoted text, click to view]

Paul Sampson
8/27/2003 4:08:02 PM
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

Paul Sampson
8/28/2003 9:03:51 AM
Thanks Simon - a common suggestion and one that I'll be sure to remember.

[quoted text, click to view]

AddThis Social Bookmark Button