我有一个包含两not null
列Created
和Updated
.
我写了相应的触发器
ALTER TRIGGER [dbo].[tr_category_inserted] ON [dbo].[Category]
AFTER INSERT
AS
BEGIN
UPDATE Category
SET Created = GETDATE(), Updated = GETDATE()
FROM inserted
WHERE Category.ID = inserted.ID;
END
和
ALTER TRIGGER [dbo].[tr_category_updated] ON [dbo].[Category]
AFTER UPDATE
AS
BEGIN
UPDATE Category
SET Updated = GETDATE()
FROM inserted
inner join [dbo].[Category] c on c.ID = inserted.ID
END
但是如果我要插入一个新行,我会收到一个错误
无法将值 NULL 插入到列“已创建”、表“类别”中;列不允许空值。插入失败。
插入命令:
INSERT INTO [Category]([Name], [ShowInMenu], [Deleted])
VALUES ('category1', 0, 0)
如何在不设置这些列以允许 null 的情况下编写此类触发器?