0

我正在编写一个查询,将模式从旧表结构更新为新表结构。我不明白为什么这个查询:

if exists (
    select * 
    from information_schema.columns 
    where table_name = 'old_table' and column_name = 'old_col')
    begin
    update [dbo].[new_table] 
        set [new_table].[new_col] = [old_table].[old_col] 
        from [dbo].[new_table] 
        inner join [dbo].[old_table] on [new_table].[old_id] = [old_table].[old_id];
    --yada yada yada...
    alter table [dbo].[old_table] drop column [old_col];
    print 'old_table successfully migrated to new_table';
    end
else
    print 'no need to migrate old_table to new_table';

抛出此异常:

Msg 207, Level 16, State 1, Line 7, 
Invalid column name 'old_col'.

old_col列不再退出时old_table,我知道这是因为:

select * 
    from information_schema.columns 
    where table_name = 'old_table' and column_name = 'old_col'

不返回任何行,我在Object Explorer.

我在这里想念什么?为什么条件语句中的查询甚至会运行?

4

0 回答 0