简体   繁体   中英

How to compare two tables with same columns in plsql?

I got two tables : aanvr_omzetten (requests) and klant (customers)

The structure of the tables:

AANVR_OMZETTEN

VOORNAAM ACHTERNAAM GESLACHT GEBOORTEDATUM EMAIL
A        John       M        07-01-1990    v1@gmail.com
B        Jaxk       V        01-04-1965    v2@gmail.com


KLANT

NAAM ACHTERNAAM GESLACHT GEBOORTEDATUM EMAIL        NATION BANKNR STUDY
A    John       M        07-01-1990    v1@gmail.com DUTCH  12     YES
B    Jack       M        01-04-1965    v2@gmail.com DUTCH  15     YES

Here is my code:

DECLARE
v_klantnummer number;
v_rekeningnummer number;

BEGIN
    -- get klantnummer and rekeningid
    BEGIN
        select klantnummer, rekeningid into v_klantnummer, v_rekeningid from rekening where rekeningnummer = :P501_REKENINGNR and rekeningtype = 23;
    EXCEPTION
        when no_data_found then
            raise_application_error(-20000, 'Rekeningnummer is onbekend');
    END;

    -- get the information of klant X with klantnummer v_klantnummer

    for i in (select voornaam, achternaam, geslacht, geboortedatum, email, postcode, huisnummer, straat, plaats, nationaliteit, burgerservicenummer
             from klant where klantnummer = v_klantnummer)
             loop

             -- compare with other table?? 
             -- 

     for i in (select voornaam, achternaam, geslacht, geboortedatum, email, postcode, huisnummer, straat, plaats, nationaliteit, burgerservicenummer
             from aanvr_omzetten where aanvr_omzetten = :P501_aanvr_omzettennr)

             -- both tables loaded but how can i compare them?

             -- ONLY if the selected attributes match 

             INSERT etc.

I don't need to compare all columns, just those in table in aanvr_omzetten because table klant has all those columns + extra.

Primary key of row in KLANT : v_klantnummer
Primary key of row in AANVR_OMZETTEN : : P501_AANVR_OMZETTENNR

I want to do an insert only if those columns match else it should raise an error.

Any idea how I can do this?

So you want to insert into some (third?) table values which exist in both named tables? You can do this with a SQL INSERT statement, using the INTERSECT operator to identify the matching rows.

insert into whatever
select VOORNAAM, ACHTERNAAM, GESLACHT, GEBOORTEDATUM, EMAIL 
from AANVR_OMZETTEN
intersect 
select NAAM, ACHTERNAAM, GESLACHT, GEBOORTEDATUM, EMAIL   
from KLANT
;     

This may not be a complete solution (for instance, it ignores the matter of primary keys). But your question doesn't give any clues as to how you want to handle such things. If you need further assistance please clarify your question.

  • -- Create a type for presenting only common columns

    -- Pop the array of these type of tables for the of the table

    -- Iterate with one loop for example for KLANT
    -- foreach row check if it exists in AANVR_OMZETTEN table array.
    -- if not; insert; if yes continue next.

https://www.tutorialspoint.com/plsql/plsql_arrays.htm

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