Add a column, if it does not already exist column error

axr492tv  于 2023-02-28  发布在  其他
关注(0)|答案(1)|浏览(152)

I got invalid column error
Here is simple code for your reference..

create or alter procedure proc_testing 
as
begin

drop table testing
create table testing ([name] varchar(50))

insert into testing values ('testing')

IF NOT EXISTS (SELECT 1
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE upper(TABLE_NAME) = 'testing'
        AND upper(COLUMN_NAME) = 'age')
BEGIN
    ALTER TABLE [dbo].[Person] ADD age int 
END

update testing set age=1

End
gwo2fgha

gwo2fgha1#

Try this:

create or alter procedure proc_testing 
as
begin

drop table if exists testing
create table testing ([name] varchar(50))

insert into testing values ('testing')

IF NOT EXISTS (SELECT 1
       FROM INFORMATION_SCHEMA.COLUMNS
       WHERE upper(TABLE_NAME) = 'testing'
       AND upper(COLUMN_NAME) = 'age')
BEGIN
   ALTER TABLE [dbo].[testing] ADD age int 
END

EXEC('
update testing set age=1
')

End

相关问题