简体   繁体   English

如何在plsql中比较具有相同列的两个表?

[英]How to compare two tables with same columns in plsql?

I got two tables : aanvr_omzetten (requests) and klant (customers) 我有两个表: aanvr_omzetten (请求)和klant (客户)

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. 我不需要比较所有列,只需比较aanvr_omzetten klant中的那些列,因为表klant具有所有这些列和其他内容。

Primary key of row in KLANT : v_klantnummer KLANT v_klantnummer主键: v_klantnummer
Primary key of row in AANVR_OMZETTEN : : P501_AANVR_OMZETTENNR 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. 您可以使用INTERSECT运算符通过SQL INSERT语句来执行此操作,以标识匹配的行。

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 -以一个循环进行迭代,例如KLANT
    -- foreach row check if it exists in AANVR_OMZETTEN table array. -foreach行检查它是否存在于AANVR_OMZETTEN表数组中。
    -- if not; - 如果不; insert; 插入; if yes continue next. 如果是,请继续。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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