成功INSERT到目标表后UPDATE源表?

时间:2017-07-20 09:58:28

标签: sql sql-server

我需要在成功插入目标表后对源表进行udpate(从字段中剪切出一个字符串)。我找到了一些类似任务的例子,但不完全是这样。我正在寻找这样的东西:

INSERT INTO [dbo].[ConditionsAsignments]
       ([ConditionID]
       ,[SRCtableID])
SELECT
        8 as ConditionID   
       ,SRC.ID as SRCtableID   
FROM SRCtable as SRC 
WHERE SRC.BusinessConditionText LIKE '%Prices are ex VAT.%'

OUTPUT INSERTED.*
UPDATE SRC.BusinessConditionText = SUBSTRING(...cut out the searched text...) 
WHERE SRC.ID = SRCtableID

性能不是问题,这只是一个数据迁移脚本。

2 个答案:

答案 0 :(得分:0)

当我跟随他的评论时,对Adwaenyth的信用。

我采取了疯狂的猜测,似乎按预期工作:

CREATE TABLE #IDins(ID INT)

INSERT INTO [dbo].[ConditionsAsignments]
       ([ConditionID]
       ,[SRCtableID])
OUTPUT INSERTED.SRCtableID INTO #IDins   -- outputing to a TEMP table
SELECT
        8 as ConditionID   
       ,SRC.ID as SRCtableID   
FROM SRCtable as SRC 
WHERE SRC.BusinessConditionText LIKE '%Prices are ex VAT.%'

UPDATE SRCtable SET BusinessConditionText = CONCAT(SUBSTRING(...cut out the searched text...)) 
WHERE ID IN (SELECT ID FROM #IDins)     -- modifiying only lines, which have inserts

GO

-- can be repeated with a different string ---

答案 1 :(得分:-1)

使用"插入触发后#34;为你的目标表。