SQL Server触发器:使用另一个表中的COUNT更新表

时间:2017-08-31 03:40:01

标签: sql sql-server triggers

我是SQL新手。尝试使用另一个表中的ID和COUNT更新表。我需要从Table1 ID,COUNT的ID和最近的Date获得。然后使用ID,SUM,Date更新Table2。 表1(ID,VID,日期)PK = ID& VID 表2(ID,SUM,日期)

CREATE TRIGGER Trigger1
  ON dbo.Table1
  AFTER INSERT, UPDATE, DELETE
AS
  UPDATE dbo.Table2
  SET SUM = (
     SELECT COUNT(ID)
     FROM dbo.Table1
);

我知道这是非常不完整的,不包括ID和日期。任何帮助,将不胜感激!提前谢谢。

2 个答案:

答案 0 :(得分:0)

我猜你正在寻找类似的东西......

USE tempdb;
GO

-- Start by creating a couple of tables in tempdb...
CREATE TABLE dbo.OrderHeader (
    OrderID INT NOT NULL,
    DetailCount INT NOT NULL
        CONSTRAINT df_OrderHeader_DetailCount DEFAULT (0),
    CONSTRAINT pk_OrderHeader_OrderID PRIMARY KEY CLUSTERED (OrderID)
    );
GO

INSERT dbo.OrderHeader(OrderID) VALUES 
    (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),
    (11),(12),(13),(14),(15),(16),(17),(18),(19),(20),
    (21),(22),(23),(24),(25),(26),(27),(28),(29),(30);
GO

CREATE TABLE dbo.OrderDetails (
    OrderDetailID INT NOT NULL IDENTITY(1,1),
    OrderID INT NOT NULL 
        CONSTRAINT fk_OrderHeader_OrderID FOREIGN KEY REFERENCES dbo.OrderHeader (OrderID)
            ON UPDATE CASCADE ON DELETE CASCADE,
    Quantity INT NOT NULL 
        CONSTRAINT df_OrderDetails_Quantity DEFAULT (1)
        CONSTRAINT ck_OrderDetails_Quantity CHECK (Quantity > 0)
    CONSTRAINT pk_OrderDetails_OrderID PRIMARY KEY CLUSTERED (OrderDetailID)
    );
GO

-- create a nonclustered index on dbo.OrderDetails.OrderID so that the trigger has the ability to do a seek operation.
CREATE NONCLUSTERED INDEX ix_OrderDetails_OrderID ON dbo.OrderDetails (OrderID);

-- Create an AFTER trigger on dbo.OrderDetails that will update dbo.OrderHeader after ever insert.
CREATE TRIGGER tr_UpdateDetailCount ON dbo.OrderDetails
AFTER INSERT 
AS 
BEGIN 
    UPDATE oh SET
        oh.DetailCount = odx.DetailCount
    FROM
        dbo.OrderHeader oh
        JOIN Inserted i
            ON oh.OrderID = i.OrderID
        CROSS APPLY (
                    SELECT 
                        DetailCount = COUNT(1)
                    FROM
                        dbo.OrderDetails od
                    WHERE 
                        oh.OrderID = od.OrderID
                    ) odx;
END;
GO 

-- Insert some values into dbo.OrderDetails...
INSERT dbo.OrderDetails(OrderID, Quantity)
SELECT TOP 10000
    ABS(CHECKSUM(NEWID())) % 30 + 1,
    ABS(CHECKSUM(NEWID())) % 5 + 1
FROM
    dbo.tfn_Tally(200, 1) t

--======================================

-- Check to see that the trigger has worked as expected...
SELECT * FROM dbo.OrderHeader oh;
SELECT * FROM dbo.OrderDetails od;

--======================================

-- And cleanup...
DROP TABLE dbo.OrderDetails;
DROP TABLE dbo.OrderHeader;
插入dbo.OrderDetails后的

dbo.OrderHeader

OrderID     DetailCount
----------- -----------
1           5
2           10
3           5
4           15
5           4
6           6
7           7
8           7
9           9
10          11
11          6
12          6
13          3
14          6
15          2
16          7
17          8
18          6
19          5
20          8
21          6
22          6
23          6
24          11
25          12
26          7
27          4
28          5
29          1
30          6

答案 1 :(得分:0)

试试这个 -

CREATE TRIGGER Trigger1
      ON dbo.Table1
      AFTER INSERT, UPDATE, DELETE
    AS
    begin
    DECLARE @id int
    DECLARE @date datetime
    SET NOCOUNT ON
    select  @id =  id  FROM INSERTED
    set @date =  select max(date)  FROM table1

      UPDATE dbo.Table2
      SET SUM =  a.sum
          from
         (SELECT COUNT(ID) as sum,id
         FROM dbo.Table1
         group by id) a
         where a.id = table2.id
         and  Table2.date = @date
         where a.id = table2.id;
     end