简体   繁体   中英

Comparing two columns in an IF statement in a stored procedure in Oracle

I have two different tables in SQL Developer, TB_Temp and TB_Main . I want to compare just two columns from these tables, so eg. I want to compare the user_id column in tb_temp and the user_payment column in tb_main .

Both these tables have been imported from Excel files. If there is a match, I want it to for instance, output just a debug using dbms_output logging.

So far I have; Its the comparison part I struggle with the IF statement! The two tables contain values which will constantly be uploaded with new information, therefore what I am getting at is I cant actually declare the values that are stored in the table, as they will always be changing if that makes sense? I just need to know if the columns match, then there needs to be an output!

create PROCEDURE PR_TEST AS

user_payment number;

CURSOR c is
SELECT user_payment 
FROM tb_main;

CURSOR tmp is

SELECT * 
FROM tb_temp
WHERE user_ref is null or user_ref = user_payment

cTemp%ROWTYPE

BEGIN

--loop through cursor---

 cTemp_COUNT:=0;

 FOR I IN cTemp LOOP

 end loop

The SET based solution should work for you.

SELECT t.* 
FROM tb_temp t INNER JOIN tb_main m on 
WHERE t.user_ref = m.user_payment
UNION
SELECT * FROM tb_temp WHERE user_ref IS NULL

Do you know what joins are and ref_Cursors for your output? Also note: when playing with imported data, you may have non-display characters such as space. So if a user_Payment was '123' and a user ID was '123 ' no match would be found.

Shows all details from both tables where the user_payment matches user_ID

SELECT * 
FROM tb_Main M
INNER JOIN tb_temp T
 on M.user_payment = T.User_ID

Shows all records from both tables and when matches exist shows the records aligned (on same row) with eachother if multiple matches occur, multiple rows will be produced.

SELECT * 
FROM tb_Main M
FULL OUTER JOIN tb_temp T
 on M.user_payment = T.User_ID

Shows all records from tb_main and only details if it matches from tb_temp

SELECT * 
FROM tb_Main M
LEFT JOIN tb_temp T
 on M.user_payment = T.User_ID

Shows all records from tb_temp and only details if it matches from tb_main

SELECT * 
FROM tb_Main M
RIGHT JOIN tb_temp T
 on M.user_payment = T.User_ID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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