SQL IF语句被忽略

问题描述:

我有一个很长的脚本,我希望能够在需要时运行整个文件,而不必担心它的部分是否已经运行。但是下面的脚本给我带来了问题。由于某些原因,即使列'EntityID'和'EntityType'不存在,它也会越过IF语句,在这种情况下,它不应该通过IF语句。有人能告诉我什么是错的吗?SQL IF语句被忽略

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Notes' AND COLUMN_NAME IN ('EntityId', 'EntityType')) 
BEGIN 
    BEGIN TRANSACTION 
     --Delete notes where EntityType and EntityID are both NULL 
     DELETE FROM [dbo].[Notes] 
     WHERE [EntityId] = NULL 
     AND [EntityType] = NULL 
     --Delete notes where the corresponding contact or account has been deleted. 
     OR [ID] IN (9788, 10684, 10393, 10718, 10719) 

     --Populate new columns with all existing data 
     UPDATE [dbo].[Notes] 
     SET [AccountId] = [EntityId] 
     WHERE [EntityType] = 1 

     UPDATE [dbo].[Notes] 
     SET [ContactId] = [EntityId] 
     WHERE [EntityType] = 2 

     --Delete EntityId and EntityType columns from the Notes table 
     ALTER TABLE [dbo].[Notes] 
     DROP COLUMN [EntityId], [EntityType] 
    COMMIT 
END 
GO 

的脚本表

CREATE TABLE [dbo].[Notes](
    [ID] [int] IDENTITY(1,1) NOT NULL, 
    [AnnotationID] [uniqueidentifier] NULL, 
    [CreatedBy] [int] NULL, 
    [CreatedDate] [datetime] NULL, 
    [NoteText] [ntext] NULL, 
    [OriginalAnnotationID] [uniqueidentifier] NULL, 
    [Active] [bit] NULL, 
    [ContactId] [int] NULL, 
    [AccountId] [int] NULL, 
CONSTRAINT [PK_Notes] PRIMARY KEY CLUSTERED 
(
    [ID] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] 

GO 

ALTER TABLE [dbo].[Notes] WITH CHECK ADD CONSTRAINT [FK_Account_ID] FOREIGN KEY([AccountId]) 
REFERENCES [dbo].[Account] ([ID]) 
GO 

ALTER TABLE [dbo].[Notes] CHECK CONSTRAINT [FK_Account_ID] 
GO 

ALTER TABLE [dbo].[Notes] WITH CHECK ADD CONSTRAINT [FK_ContactId_ID] FOREIGN KEY([ContactId]) 
REFERENCES [dbo].[Contact] ([ID]) 
GO 

ALTER TABLE [dbo].[Notes] CHECK CONSTRAINT [FK_ContactId_ID] 
GO 

的错误:

Msg 207, Level 16, State 1, Line 6 
Invalid column name 'EntityId'. 
Msg 207, Level 16, State 1, Line 7 
Invalid column name 'EntityType'. 
Msg 207, Level 16, State 1, Line 12 
Invalid column name 'EntityType'. 
Msg 207, Level 16, State 1, Line 16 
Invalid column name 'EntityType'. 
+1

你确定这些列不存在吗? - 尝试在'IF'上面粘贴'select'语句,看看它返回什么(如果有的话) –

+0

只有当没有列存在时才返回false。所以,如果其中一个... –

+0

是否有可能有一个名为'Notes'的表在不同的模式?你也应该指定'TABLE_SCHEMA'。或者更好的是,使用'sys.columns'(阅读:http://sqlblog.com/blogs/aaron_bertrand/archive/2011/11/03/the-case-against-information-schema-views.aspx) –

马丁绝对是一些东西。解析器在解析时正在处理IF中的内容,并忽略您的IF是否会被移出。这是你不能做同样的原因:

IF 1 = 1 
    CREATE TABLE #x(a INT); 
ELSE 
    CREATE TABLE #x(b INT); 

一种解决方法是使用动态SQL:

IF EXISTS ... 
BEGIN 
    BEGIN TRANSACTION; 

    DECLARE @sql NVARCHAR(MAX); 

    SET @sql = N' 
     DELETE FROM [dbo].[Notes] 
     WHERE [EntityId] IS NULL 
     AND [EntityType] IS NULL 
     --Delete notes where the corresponding contact or account has been deleted. 
     OR [ID] IN (9788, 10684, 10393, 10718, 10719) 

     --Populate new columns with all existing data 
     UPDATE [dbo].[Notes] 
     SET [AccountId] = [EntityId] 
     WHERE [EntityType] = 1 

     UPDATE [dbo].[Notes] 
     SET [ContactId] = [EntityId] 
     WHERE [EntityType] = 2 

     --Delete EntityId and EntityType columns from the Notes table 
     ALTER TABLE [dbo].[Notes] 
     DROP COLUMN [EntityId], [EntityType]'; 

    EXEC sp_executesql @sql; 

    COMMIT TRANSACTION; 
END 

但你还是应该确保两个列在那里。

+0

是的,这是有效的。谢谢 – Jmh2013

我怀疑的问题是,其中一列存在,但不能同时使用。请尝试以下操作:

IF 2 = (SELECT count(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Notes' AND COLUMN_NAME IN ('EntityId', 'EntityType')) 
+0

现在两个列都不存在。我尝试了你的建议,并得到了同样的确切错误:'无效列名'EntityId'' – Jmh2013

+0

是否有可能在另一个具有相同名称(注释)的架构中有一个视图? –