Sql 插入更新后触发 table 有条件
Sql Trigger After Insert Update another table with conditions
我正在创建 After Insert 触发器,它工作正常,但在执行触发器内的语句之前我有某些条件
- Based on Different CustomerId Run the trigger, I want check which CustomerId got inserted in my LoyaltyDetailsTable, say if last insert
was Customerid=2 then pass that Customerid in where condition then run
the trigger , or if Customerid = 1 then run the trigger for that Id,
so on.
- I want to check whether in PriceClaimTable the inserted CustomerId exist or not, If exists then update the details else just insert the
values in LoyaltyDetailsTable only.
触发查询
CREATE TRIGGER DetailsAfterInsert ON [dbo].[LoyaltyDetailsTable]
FOR INSERT
as
UPDATE PriceClaimTable
SET CurrentPoints =
(
(SELECT SUM(LoayaltyPointsTable.Points) AS RecentPoints FROM LoayaltyPointsTable
join LoyaltyDetailsTable ON LoayaltyPointsTable.LoyaltyPointsId
= LoyaltyDetailsTable.LoyaltyPointsId
WHERE CustomerId=1 and LoyaltyDetailsId= (SELECT MAX(LoyaltyDetailsId)
AS LoyaltyDetailsTable FROM LoyaltyDetailsTable))
+
(SELECT CurrentPoints FROM PriceClaimTable WHERE ClaimCustomerId=1 and
PriceClaimId=(SELECT max(PriceClaimId) FROM PriceClaimTable
))
)
WHERE ClaimCustomerId=1 and PriceClaimId=(SELECT max(PriceClaimId) FROM PriceClaimTable)
这是我第一次尝试写触发器,是table结构
任何帮助都会很棒。
您在这里寻找的是 inserted
table。每次发出 UPDATE
语句时,SQL 服务器都会生成两个虚拟 table,分别称为 inserted
和 deleted
,用于存储有关您正在进行的数据修改的信息.这些 table 可从您的触发器访问。有关详细信息,请参阅此处:https://msdn.microsoft.com/en-us/library/ms191300.aspx
您可以使用 inserted
来获取您要查找的 ID。所以,而不是:
WHERE ClaimCustomerId=1
您可以使用:
WHERE ClaimCustomerId=inserted.ClaimCustomerId
我正在创建 After Insert 触发器,它工作正常,但在执行触发器内的语句之前我有某些条件
- Based on Different CustomerId Run the trigger, I want check which CustomerId got inserted in my LoyaltyDetailsTable, say if last insert was Customerid=2 then pass that Customerid in where condition then run the trigger , or if Customerid = 1 then run the trigger for that Id, so on.
- I want to check whether in PriceClaimTable the inserted CustomerId exist or not, If exists then update the details else just insert the values in LoyaltyDetailsTable only.
触发查询
CREATE TRIGGER DetailsAfterInsert ON [dbo].[LoyaltyDetailsTable]
FOR INSERT
as
UPDATE PriceClaimTable
SET CurrentPoints =
(
(SELECT SUM(LoayaltyPointsTable.Points) AS RecentPoints FROM LoayaltyPointsTable
join LoyaltyDetailsTable ON LoayaltyPointsTable.LoyaltyPointsId
= LoyaltyDetailsTable.LoyaltyPointsId
WHERE CustomerId=1 and LoyaltyDetailsId= (SELECT MAX(LoyaltyDetailsId)
AS LoyaltyDetailsTable FROM LoyaltyDetailsTable))
+
(SELECT CurrentPoints FROM PriceClaimTable WHERE ClaimCustomerId=1 and
PriceClaimId=(SELECT max(PriceClaimId) FROM PriceClaimTable
))
)
WHERE ClaimCustomerId=1 and PriceClaimId=(SELECT max(PriceClaimId) FROM PriceClaimTable)
这是我第一次尝试写触发器,
任何帮助都会很棒。
您在这里寻找的是 inserted
table。每次发出 UPDATE
语句时,SQL 服务器都会生成两个虚拟 table,分别称为 inserted
和 deleted
,用于存储有关您正在进行的数据修改的信息.这些 table 可从您的触发器访问。有关详细信息,请参阅此处:https://msdn.microsoft.com/en-us/library/ms191300.aspx
您可以使用 inserted
来获取您要查找的 ID。所以,而不是:
WHERE ClaimCustomerId=1
您可以使用:
WHERE ClaimCustomerId=inserted.ClaimCustomerId