如何将两个SQL查询结果合并到一个表中?

时间:2015-12-10 12:23:40

标签: sql oracle plsql

create or replace PROCEDURE SearchResult
(     V_ID IN VARCHAR2
      , V_REFCUR OUT SYS_REFCURSOR
)
IS

BEGIN
  DECLARE CNT INT;

  BEGIN

  SELECT COUNT(*) INTO CNT 
  FROM TABLE1 T1
  INNER JOIN TABLE2 T2 ON T1.CODE = T2.CODE
  WHERE T1.ID = V_ID;

  IF (CNT > 0) THEN
  OPEN V_REFCUR FOR  
  SELECT T1.TYPE
  FROM TABLE T1
  INNER JOIN TABLE2 T2 ON T1.CODE = T2.CODE
  WHERE T1.ID = V_ID AND T1.TYPE IS NOT NULL;


  ELSE 
  OPEN V_REFCUR FOR  
  SELECT 
     T3.NAME||T1.TYPE
     FROM TABLE3 T3
     INNER JOIN TABLE2 T2 ON T2.ID= T3.ID
     INNER JOIN TABLE1 T1 ON T1.CODE = T2.CODE
     AND TI.TYPE IS NOT NULL AND T1.ID = V_ID;

  END IF;
  END;

   BEGIN
   OPEN V_REFCUR FOR
    SELECT
    T1.NAME
    , T1.ADDRESS
    FROM TABLE1 T1
    WHERE T1.ID = V_ID

 END;
END SearchResult

1 个答案:

答案 0 :(得分:2)

要将单独查询的结果合并到同一个表格中,您可以使用以下几个选项:

  • 根据您是否要Union all,使用exclude duplicatesInsert将两个查询合并为一个。
  • 使用临时表将查询拆分为2个单独的The text code generated语句,然后返回临时表的结果。