我有以下 SQL:
IF EXISTS (SELECT 1 FROM sys.columns WHERE name='RequireNamespaceClaim' AND object_id = OBJECT_ID('DefaultBaseUrl'))
BEGIN
UPDATE DefaultBaseUrl SET AuthenticationTypeId = at.AuthenticationTypeId
FROM DefaultBaseUrl dbu
JOIN (
SELECT AuthenticationTypeId, CASE CodeName WHEN 'NATIVE' THEN 0 ELSE 1 END RequireNamespaceClaim
FROM AuthenticationType
) at ON dbu.RequireNamespaceClaim = at.RequireNamespaceClaim
END
运行它会打印:
Msg 207, Level 16, State 1, Line 8
Invalid column name 'RequireNamespaceClaim'.
然而,运行
SELECT 1 FROM sys.columns WHERE name='RequireNamespaceClaim' AND object_id = OBJECT_ID('DefaultBaseUrl')
表明确实不存在这样的列。
因此,该IF EXISTS
语句是FALSE
,因此 if 语句的主体不会运行。但是,仍然会打印错误。
到底是怎么回事?
我该如何解决?