我有四个表不断截断,然后每五分钟从另一个系统插入和更新数据到SQL Server。所以我需要创建一个更新并插入触发器来使用if exists或merge更新SQL Server中的另一个。
Select
[Barcode]
,[TradingPartnerOrderNo]
,[ProductNo]
,[BarcodeCreatedDateTime]
,[BarcodeScheduledDateTime]
,[CancelledIndicator]
,[CancelledDateTime]
,[SiteIndicator]
,[InterfaceTimeStamp]
,[OperationDate]
,[Operation]
from [dbo].[BookingView]
答案 0 :(得分:0)
如果您使用的是SQL Server 2008+,则可以使用MERGE
来执行此操作。像这样:
MERGE INTO table2 AS TGT
USING
(
Select
[Barcode]
,[TradingPartnerOrderNo]
,[ProductNo]
,[BarcodeCreatedDateTime]
,[BarcodeScheduledDateTime]
,[CancelledIndicator]
,[CancelledDateTime]
,[SiteIndicator]
,[InterfaceTimeStamp]
,[OperationDate]
,[Operation]
FROM [dbo].[BookingView]
) AS SRC ON SRC.Barcode = TGT.BarCode AND
WHEN NOT MATCHED THEN
INSERT (Barcode, TradingPartnerOrderNo, ...)
VALUES (SRC.Barcode, SRC.TradingPartnerOrderNo, ...);
答案 1 :(得分:0)
对我而言,你的需求并不是很清楚,但这是一个触发器示例:
CREATE TRIGGER [dbo].[up_update_mytable]
ON [dbo].[MyTable] FOR UPDATE
AS
DECLARE @ID VARCHAR(50)
SELECT @ID = ID FROM INSERTED --Use INSERTED keyword for insert and update triggers to get informations of the inserted or updated record.
UPDATE MyOtherTable SET
Column1 = 'test value',
Column2 = 'test value 2'
WHERE OtherTableID = @ID