あなたの構文は少しずれています:
CREATE TRIGGER dbo.trUpdate
ON dbo.Tablename
FOR INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON;
UPDATE Tablename
SET Datecolumn = GETDATE()
WHERE ID IN (SELECT id FROM inserted);
END
GO
NOT NULL
ここで、列を に変更し、デフォルトをに変更する必要がありますGETDATE()
。次に、更新時にこのトリガーを起動するだけです。
-- first clean up any remaining NULL values:
UPDATE dbo.Tablename
SET DateColumn = '19000101'
WHERE DateColumn IS NULL;
-- now add the default constraint:
ALTER TABLE dbo.Tablename ADD CONSTRAINT
DF_Tablename_DateColumn DEFAULT(GETDATE()) FOR DateColumn;
-- now add the NOT NULL constraint:
ALTER TABLE dbo.Tablename ALTER COLUMN DateColumn
DATETIME NOT NULL;
-- now you can update the trigger so that it only fires on update:
ALTER TRIGGER dbo.trUpdate
ON dbo.Tablename
FOR UPDATE
AS
BEGIN
SET NOCOUNT ON;
UPDATE Tablename
SET Datecolumn = GETDATE()
WHERE ID IN (SELECT id FROM inserted);
END
GO