我有一个这样定义的表
CREATE TABLE [dbo].[ObjectRelationClauses] ( [Id] INT NOT NULL PRIMARY KEY IDENTITY, [RelationId] INT NOT NULL, [OperatorType] NVARCHAR(3) NOT NULL, [LocalPropertyId] INT NOT NULL, [ForeignPropertyId] INT NULL, [ForeignValue] VARCHAR(255) NULL, [ParentClauseId] INT NULL )
如果ForeignPropertyId和ForeignValue列的值都为,我需要能够引发一个错误null,否则我想执行该操作。
ForeignPropertyId
ForeignValue
null
这是我尝试过的
CREATE TRIGGER [dbo].[Trigger_ObjectRelationClauses] ON [dbo].[ObjectRelationClauses] FOR INSERT, UPDATE AS BEGIN SET NoCount ON IF(ForeignPropertyId IS NULL AND ForeignValue IS NULL) BEGIN RAISERROR('Either ForeignPropertyId or ForeignValue must be provided to perform this action!') END END
但这给了我一个语法错误。也许,我使用的方式RAISERROR是错误的。
RAISERROR
我怎样才能正确地添加触发器来验证的数据INSERT和UPDATE?
INSERT
UPDATE
这看起来像是检查约束的工作,而不是触发器:
ALTER TABLE [dbo].[ObjectRelationClauses] ADD CONSTRAINT foreign_chk CHECK ([ForeignPropertyId] IS NOT NULL OR [ForeignValue] IS NOT NULL);