"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