1

有没有办法防止在列中指定显式值?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

我希望数据库插入值,但我想阻止以任何方式指定它们。

4

1 回答 1

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 类似的东西。

于 2012-06-05T15:39:55.923 回答