合并时与目标

时间:2017-10-11 19:12:47

标签: sql sql-server sql-server-2008 merge

MERGE Table1 AS T USING Table2 AS S ON (T.1 = S.10) 
     WHEN MATCHED AND T.2 = 'testname1' THEN UPDATE SET T.4 = S.11 
     WHEN NOT MATCHED THEN INSERT (t.3, t.1, t.2, t.4) VALUES (22, S.10, 'testname1', s.11); 

MERGE Table1 AS T USING Table2 AS S ON (T.1 = S.10) 
     WHEN MATCHED AND T.2 = 'testname2' THEN UPDATE SET T.4 = S.11 
     WHEN NOT MATCHED THEN INSERT (t.3, t.1, t.2, t.4) VALUES (22, S.10, 'testname2', s.11); 

我需要每次运行5次更改T.2的数据。更新语句可以正常运行5次,但插入仅适用于第一次运行,因为t.1现在退出表中,并且您不能在没有匹配时和对目标列的引用。我想要的是"WHEN NOT MATCHED AND T.2 <> 'testname2' THEN INSERT .... t.1是你可以加入的唯一列,当完成每个t.1应该有5行数据,t.2和t.4更新为s10和s.11的数据。 Testname是文本,不存储在任何表格中。

completed data   
22, 1234, testname 1, text 11  
22, 1234, testname 2, text 12  
22, 1234, testname 3, text 13  
22, 1234, testname 4, text 14  
22, 1234, testname 5, text 15  

我怎样才能让它发挥作用?

0 个答案:

没有答案