合并SQL多个插入语句不是一个选项

时间:2013-01-22 10:05:35

标签: sql oracle insert merge-statement

所以,另一个问题的继续......

我合并了哪个任务是从现有表创建联结表行,该表只能表示实体之间的1-1连接(PROJECT)和表示N-1 connecton(worker)的表,联结表是(PROJECT-WORKER),每个项目都应该有一个专门的领导者,每个工人都应该有一个项目,但现在我应该有一个nn连接...这个合并将完成这项工作:

但不幸的是,multiple matched then insert分支不是sql中的“功能”,我该如何解决这个问题。原始查询---)

MERGE INTO WORKERPROJECT TARGET
USING (SELECT distinct
               w.worker_id,
               w.worker_type,
               w.project_id worker_project_id, 
               p.project_id project_project_id,
               p.dedicated_project_leader,
               p.dedicated_lead_developer,
               p.dedicated_lead_consultant,
               p.dedicated_supervisor
       from WORKER w 
       join PROJECT p on w.project_id = p.project_id
      ) SOURCE
   ON (SOURCE.worker_type is null)
 WHEN NOT MATCHED THEN INSERT (TARGET.FK_WORKER_ID, TARGET.FK_PROJECT_ID,TARGET.IS_ACTIVE,POSITION) 
  VALUES (SOURCE.WORKER_ID,SOURCE.worker_project_id,'ACTIVE',SOURCE.worker_type);
 WHEN NOT MATCHED THEN INSERT (FK_WORKER_ID, FK_PROJECT_ID,IS_ACTIVE,POSITION)
  VALUES (SOURCE.dedicated_project_leader,SOURCE.project_project_id,'ACTIVE','PROJECTVEZETŐ');
 WHEN NOT MATCHED THEN INSERT (FK_WORKER_ID, FK_PROJECT_ID,IS_ACTIVE,POSITION)  
  INSERT (FK_WORKER_ID, FK_PROJECT_ID,IS_ACTIVE,POSITION) VALUES (SOURCE.dedicated_lead_developer,SOURCE.project_project_id,'ACTIVE','FEJLESZTŐVEZETŐ');
 WHEN NOT MATCHED THEN INSERT (FK_WORKER_ID, FK_PROJECT_ID,IS_ACTIVE,POSITION)
  INSERT (FK_WORKER_ID, FK_PROJECT_ID,IS_ACTIVE,POSITION) VALUES (SOURCE.dedicated_lead_consultant,SOURCE.project_project_id,'ACTIVE','KONZULENSVEZETŐ');
 WHEN NOT MATCHED THEN INSERT (FK_WORKER_ID, FK_PROJECT_ID,IS_ACTIVE,POSITION)
  INSERT (FK_WORKER_ID, FK_PROJECT_ID,IS_ACTIVE,POSITION) VALUES (SOURCE.dedicated_supervisor,SOURCE.project_project_id,'ACTIVE','SUPERVISOR');

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分:0)

如果需要使用merge执行多个插入,请将不同表中的列添加到初始选择中。像这样:

_jobs.html