필자는 긴 스크립트를 가지고 있으며 필자는 필요할 때 전체 파일을 실행할 수 있기를 원합니다.이 파일의 일부가 이미 실행 되었더라도 걱정할 필요가 없습니다. 그러나 아래의 스크립트는 나에게 문제를주고있다. 어떤 이유로 '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'.
열이 존재하지 않습니까? -'IF' 위의'select' 문을 그대로 두어 어떤 것이 반환되는지 확인하십시오. –
은 어떤 열도없는 경우에만 false를 반환합니다. 그래서 하나 또는 다른 사람이 ... –
다른 스키마에있는'Notes'라는 테이블이있을 수 있습니까? 'TABLE_SCHEMA'도 지정해야합니다. 또는 더 나은 아직,'sys.columns'을 사용하십시오 (이것을 읽으십시오 : http://sqlblog.com/blogs/aaron_bertrand/archive/2011/11/03/the-case-against-information-schema-views.aspx) –