简体   繁体   English

将源表成功插入到目标表后,将其更新为源表?

[英]UPDATE source table upon succesfull INSERT into target table?

I need to udpate source table (cut out a string from a field) upon a succesful insert into a target table. 成功插入目标表后,我需要对源表进行udpate(从字段中切出字符串)。 I found some examples of similar tasks, but not exactly this. 我找到了一些类似任务的例子,但并非完全如此。 I am looking for something like this: 我正在寻找这样的东西:

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

Performance is not an issue, this is only a data migration script. 性能不是问题,这只是数据迁移脚本。

Credits to Adwaenyth, as I followed his comment. 当我遵循他的评论时,对Adwaenyth表示感谢。

I took a wild guess and it seems to work as desired: 我做出了一个疯狂的猜测,它似乎可以按需工作:

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 ---

在目标表中使用“插入触发器后”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM