简体   繁体   中英

Invalid column name sql error when adding and updating at same time

I am trying to add and update column at same time, below is my query.I don't want to use NOT NULL DEFAULT constraint.

IF NOT EXISTS(SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE 
TABLE_NAME = 'table1' AND COLUMN_NAME = 'new col')
BEGIN 
ALTER TABLE table1 ADD "new col" varchar(MAX)
EXEC ('UPDATE table1 SET "new col" = '''' ');
END

But its throwing me an error Invalid column name 'new col'.

Thanks in advance.

if not exists(Select name from sys.columns where name = 'IncOHSRepEvent' and

object_id = object_id('Inc01_CA')) ALTER TABLE [Inc01_CA] ADD [IncOHSRepEvent]

varchar(200) NULL

Please try above with your values. Also in above query Column name = IncOHSRepEvent' Table Name = 'Inc01_CA'

ALTER TABLE table1 ADD [new col] varchar(MAX)
EXEC ('UPDATE table1 SET [new col] = '''' ');

We cannot use "Column" for table names and columns

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM