【问题标题】:Please tell me why this code is throwing an error请告诉我为什么这段代码会引发错误
【发布时间】:2010-08-16 14:48:03
【问题描述】:

我一生都无法弄清楚这段代码有什么问题:

IF NOT EXISTS(SELECT * FROM sys.columns WHERE name = 'Report_Date' 
                       AND object_id = OBJECT_ID('TempTable3'))
    ALTER TABLE TempTable3 ADD Report_Date datetime

--Set "ReportDate" field to the date the data was extracted less one.
UPDATE TempTable3
SET Report_Date = '20100815'

我不断收到此错误:

"Msg 207, Level 16, State 1, Line 51
Invalid column name 'Report_Date'."

【问题讨论】:

    标签: sql sql-server-2005 sql-update alter-table


    【解决方案1】:

    在alter table之后放一个GO

    ALTER TABLE TempTable3 ADD Report_Date datetime
    GO
    

    【讨论】:

    • 因为 GO 是一个批处理终止器,告诉 SQL Server 立即执行到该点为止。更新和插入不需要它,但每当您更改元数据时,它就需要它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 2016-07-10
    • 2016-02-03
    • 2022-01-25
    相关资源
    最近更新 更多