2016-12-27 78 views
0

我需要創建觸發器,它插入到dbo.Log表值: - 在ACTION_TYPE:如何對多條記錄更新,刪除或插入的 - 在datetime_of_action ofcourse行動的時間戳日誌表使用觸發器

現在我有這樣的:

DROP TABLE dbo.Log 

CREATE TABLE dbo.Log (
logid  INT   NOT NULL IDENTITY, 
action_type NVARCHAR(50) NOT NULL, 
datetime_of_action DATETIME  NOT NULL, 
CONSTRAINT PK_Log PRIMARY KEY(logid)); 

CREATE TRIGGER trig2 
ON Sales.Customers 
FOR UPDATE , DELETE, INSERT 
AS 
BEGIN 
...................... 
END 

SELECT * FROM dbo.Log 

這是Sales.Customers表的腳本:

CREATE TABLE [Sales].[Customers](
    [custid] [int] IDENTITY(1,1) NOT NULL, 
    [companyname] [nvarchar](40) NOT NULL, 
    [contactname] [nvarchar](30) NOT NULL, 
    [contacttitle] [nvarchar](30) NOT NULL, 
    [address] [nvarchar](60) NOT NULL, 
    [city] [nvarchar](15) NOT NULL, 
    [region] [nvarchar](15) NULL, 
    [postalcode] [nvarchar](10) NULL, 
    [country] [nvarchar](15) NOT NULL, 
    [phone] [nvarchar](24) NOT NULL, 
    [fax] [nvarchar](24) NULL, 
CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED 
(
    [custid] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

GO 

如果有人知道怎麼做這是我的建議。

+0

你有什麼研究,嘗試,發現不符合你正在嘗試做什麼? – dfundako

回答

0

使用此查詢:

CREATE TRIGGER trig2 
    ON Sales.Customers 
    AFTER UPDATE , DELETE, INSERT 
    AS 
    BEGIN 

    IF EXISTS (select * From inserted) 
     BEGIN 
     IF EXISTS (select * From deleted) 
     BEGIN 
      INSERT INTO dbo.Log (action_typ, datetime_of_action) 
      VALUES(CAST(SELECT COUNT(*) FROM deleted) as VARCHAR(50) + ' records have been updated',GETDATE()) 
     END 
     ELSE 
     BEGIN 
      INSERT INTO dbo.Log (action_typ, TOTAL, datetime_of_action) 
      VALUES(CAST(SELECT COUNT(*) FROM inserted) as VARCHAR(50) + ' records have been iniserted',GETDATE()) 
     END 
    ELSE 
     BEGIN 
     INSERT INTO dbo.Log (action_typ, TOTAL, datetime_of_action) 
     VALUES(CAST(SELECT COUNT(*) FROM deleted) as VARCHAR(50) + ' records have been deleted',GETDATE()) 
    END 
    END 
+0

對您的真實案例進行任何措詞更改 – LONG

+0

不能解決捕獲多少記錄受到影響的問題。 – dfundako

+0

@dfundako,請在發表評論之前查看'log'表架構 – LONG

0
CREATE TRIGGER trig2 
ON Sales.Customers 
FOR UPDATE , DELETE, INSERT 
AS 
BEGIN 
    SET NOCOUNT ON; 

-- First determine the action 
DECLARE @Action NVARCHAR(50); 
SET @Action = (CASE WHEN EXISTS(SELECT * FROM INSERTED) AND EXISTS(SELECT * FROM DELETED) 
        THEN N'Update: ' + CAST((SELECT COUNT(*) FROM INSERTED) AS NVARCHAR(10)) 
        WHEN EXISTS(SELECT * FROM INSERTED) AND NOT EXISTS(SELECT * FROM DELETED) 
        THEN N'Insert: ' + CAST((SELECT COUNT(*) FROM INSERTED) AS NVARCHAR(10)) 
        WHEN EXISTS(SELECT * FROM DELETED) AND NOT EXISTS(SELECT * FROM INSERTED) 
        THEN N'Delete: ' + CAST((SELECT COUNT(*) FROM DELETED) AS NVARCHAR(10)) 
        ELSE NULL -- Skip. It may have been a "failed delete". 
       END) 

    INSERT INTO dbo.[Log] (action_type, datetime_of_action) 
    VALUES (@Action , GETDATE()); 

END