有没有办法阻止在列中指定显式值? identity属性阻止指定显式值(只要IDENTITY_INSERT关闭),这是我想要的行为类型。
create table testguid (
ID uniqueidentifier default newsequentialid() not null primary key
,somedate datetime
)
[约束或触发?]
insert into testguid (somedate) values (getdate()) -- this is ok
insert into testguid (ID, somedate) values (newid(), getdate()) -- this is not ok
我希望数据库插入值,但我想阻止任何指定它们的方法。
答案 0 :(得分:1)
您可以使用INSTEAD OF TRIGGER基本上重写INSERT语句,而不会删除ID列。
CREATE TRIGGER dbo.testguid_beforeinsert
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON;
INSERT dbo.testguid(somedate --, other columns
) SELECT GETDATE() --, other columns
FROM inserted;
END
GO
也可能想要类似UPDATE的东西。