Need help with some sql syntax.
Posted on 2016-08-30
I wrote a sql query (MS Sql Server) which is used to add a new column to an existing table in the database. The query almost works,
however something is wrong in my sql syntax, and I can't figure it out. look at the sql between the BEGIN and END below.
I am trying to create a new column with a data type of bit. The error displayed says :
Msg 207, Level 16, State 1, Line 16
Invalid column name bit
I am stuck. Can someone tell me how to fix the sql below?
DECLARE @TableName VARCHAR(25)
DECLARE @newColumnName VARCHAR(25)
DECLARE @dbName = VARCHAR(25)
DECLARE @ddl VARCHAR(255)
SET @newColumnName = 'CustAltId'
SET @TableName = 'Customer'
SET @dbName = 'WebDock'
IF NOT EXISTS (
SELECT * from information_schema.COLUMNS
WHERE column_name = @newColumnName
and table_name = @TableName
SET @ddl = 'ALTER TABLE ' + @dbName + '.' + @TableName +
' ADD COLUMN ' + @newColumnName + ' ' + bit