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
Asked
Active
Viewed 69 times
0
-
1Please edit your question and format your code so it is readable. – JJF Dec 10 '15 at 12:26
-
1You should not expect people to reverse engineer your table schema from the code you are posting. Please include all details (source and destination tables schema). – Filburt Dec 10 '15 at 12:28
-
ok, i had formatted it already.. thanks – jason Dec 10 '15 at 12:29
-
@Filburt, i just want to know the method on how to combine the two query, because if i run the above query in oracle, it always display the result of second query only.. thanks – jason Dec 10 '15 at 12:32
1 Answers
2
To combine results of separate queries into the same table you have several options available to you:
- Combine both queries into one by using a
Union
or aUnion all
depending on whether you want to exclude duplicates. - Use a temp table to break apart your queries into 2 separate
Insert
statements and then return the results of the temp table.

Community
- 1
- 1
-
since both of my queries have different column number, so i cannot use union or union all. Do u have other suggestion? thanks – jason Dec 10 '15 at 13:15
-
1I'm confused then... how do you expect to insert both query results into the same table if they follow a different structure? Ultimately, finding a consistent result pattern needs to be the first problem you tackle before trying to combine your queries. – Dec 10 '15 at 13:21
-
or can u show me how to do it based on my code? i had ran the above code and the result only show the second query.. thanks – jason Dec 10 '15 at 13:21
-
actually the records will based on second query and only get from table1, but got one column, 'type' need to compare with another table to get its record – jason Dec 10 '15 at 13:27
-
You have three "open" statements. The first two are conditional on the count at the top, but the third gets executed regardless, so I would be surprised if you see anything other than that last result set. btw you have more declare/begin/ends than you need. And for some reason all your code has gone uppercase. – William Robertson Dec 10 '15 at 18:51
-
@WilliamRobertson: thanks, you had got my idea correctly. Now my problem, the third will execute regardless, but i want the results from the first two conditional statement combine with the third one.. Can you teach me how to do it? – jason Dec 11 '15 at 01:11