私は2つのテーブルUser &ユーザーログを持っています。ユーザログテーブルは基本的に、ユーザテーブルに対して行われたすべての変更(挿入/更新/削除)を記録します。挿入/更新または削除は、ユーザ・テーブルに対して行われるユーザ・ログテーブル内SQL Server 2005 - トリガーが起動しない
ALTER TRIGGER [dbo].[TRG_UserLog]
ON [dbo].[Users]
FOR INSERT,UPDATE,DELETE
AS
-- Declare variables here
DECLARE @UserName VARCHAR(50)
DECLARE @FirstName VARCHAR(50)
DECLARE @LastName VARCHAR(50)
DECLARE @Email VARCHAR(50)
DECLARE @RoleID INT
DECLARE @UpdatedBy VARCHAR(50)
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for trigger here
IF @@ROWCOUNT = 0
BEGIN
RETURN
END
IF EXISTS(SELECT * FROM INSERTED)
--INSERTED/UPDATED
BEGIN
SET @UserName = (SELECT UserName FROM INSERTED)
SET @FirstName = (SELECT FirstName FROM INSERTED)
SET @LastName = (SELECT LastName FROM INSERTED)
SET @Email = (SELECT Email FROM INSERTED)
SET @RoleID = (SELECT RoleID FROM INSERTED)
SET @UpdatedBy = (SELECT ModifiedBy FROM INSERTED)
INSERT INTO UserLog(UserName,FirstName,LastName,Email,RoleID,[DateTime],UpdatedBy)
VALUES (@UserName,@FirstName,@LastName,@Email,@RoleID,GETDATE(),@UpdatedBy)
END
ELSE
-- DELETED
BEGIN
SET @UserName = (SELECT UserName FROM DELETED)
SET @FirstName = (SELECT FirstName FROM DELETED)
SET @LastName = (SELECT LastName FROM DELETED)
SET @Email = (SELECT Email FROM DELETED)
SET @RoleID = (SELECT RoleID FROM DELETED)
SET @UpdatedBy = (SELECT ModifiedBy FROM DELETED)
INSERT INTO UserLog(UserName,FirstName,LastName,Email,RoleID,[DateTime],UpdatedBy)
VALUES (@UserName,@FirstName,@LastName,@Email,@RoleID,GETDATE(),@UpdatedBy)
END
END
上記triiggerが挿入されていないデータ:
Iは以下の通りであるユーザテーブルにトリガを有します。コードに何か問題はありますか?