Subquery inside Merge Into under 11g

create or replace
PROCEDURE SP_IN_CURRENCY
IS
CURSOR csr IS SELECT currency_code FROM in_currency ORDER BY currency_code;
v_hkd_rate NUMBER(15,9);
BEGIN
SELECT spot_rate INTO v_hkd_rate FROM in_currency WHERE in_currency.currency_code = 'HKD';
MERGE INTO currency USING
(SELECT currency_code, spot_rate FROM in_currency WHERE in_currency.currency_code = 'HKD') rec_in_currency
ON (currency.currency_code = rec_in_currency.currency_code)
WHEN MATCHED THEN
UPDATE
SET currency.CURRENCY_NAME = NVL(
(SELECT value
FROM tb_resources
WHERE PACKAGE = 'currency'
AND locale = 'en_US'
AND KEY = rec_in_currency.currency_code
), currency.CURRENCY_NAME),
currency.spot_rate = (v_hkd_rate/rec_in_currency.spot_rate),
currency.SPOT_RATE_IN_USD = rec_in_currency.spot_rate
WHEN NOT MATCHED THEN
INSERT
currency_code,
CURRENCY_NAME,
spot_rate,
SPOT_RATE_IN_USD
VALUES
rec_in_currency.currency_code,
(SELECT value FROM tb_resources WHERE PACKAGE = 'currency' AND locale = 'en_US' AND KEY = rec_in_currency.currency_code),
(v_hkd_rate/rec_in_currency.spot_rate),
rec_in_currency.spot_rate
COMMIT;
END SP_IN_CURRENCY;
The above stored procedure created successfully in 9i but retrun the following error when create on 11g. Anyone has idea? is it the limitation of 11g? The problem was caused by this subquery:
(SELECT value FROM tb_resources WHERE PACKAGE = 'currency' AND locale = 'en_US' AND KEY = rec_in_currency.currency_code),
Error(38,99): PL/SQL: ORA-00904: "REC_IN_CURRENCY"."CURRENCY_CODE": invalid identifier

    value assign in variable equals value return by subquery in USING clause
    so   (V_HKD_RATE / REC_IN_CURRENCY.SPOT_RATE) = 1
    and  CURRENCY_CODE = 'HKD'
    key = rec_in_currency.currency_code is always 'HKD'
    let's say
    SPOT_RATE  = 999
    v_hkd_rate = 999
-- BEFORE:
select  spot_rate
into    v_hkd_rate
from    in_currency
where   in_currency.currency_code = 'HKD';
merge into currency using
    select  currency_code, spot_rate
    from    in_currency
    where   in_currency.currency_code = 'HKD'
) rec_in_currency
on
    currency.currency_code = rec_in_currency.currency_code
when matched then update set
    currency.currency_name = nvl
                                    select  value
                                    from    tb_resources
                                    where   package = 'currency'
                                    and     locale = 'en_US'
                                    and     key = rec_in_currency.currency_code
                            ,   currency.currency_name
,currency.spot_rate           = (v_hkd_rate / rec_in_currency.spot_rate)
,currency.spot_rate_in_usd    = rec_in_currency.spot_rate
when not matched then insert
    currency_code
,   currency_name
,   spot_rate
,   spot_rate_in_usd
values
    rec_in_currency.currency_code
        select  value
        from    tb_resources
        where   package = 'currency'
        and     locale  = 'en_US'
        and     key     = rec_in_currency.currency_code
,    (v_hkd_rate/rec_in_currency.spot_rate)
,    rec_in_currency.spot_rate
-- AFTER:
merge into currency using
    select  currency_code, spot_rate
     -- 'HKD', 999
    from    in_currency
    where   in_currency.currency_code = 'HKD'
) rec_in_currency
on
    currency.currency_code = 'HKD'
when matched then update set
    /* If value IS NULL update with current value? I think you should change it... */
    currency.currency_name = nvl
                                    select  value
                                    from    tb_resources
                                    where   package = 'currency'
                                    and     locale  = 'en_US'
                                    and     key     ='HKD'
                            ,   currency.currency_name
,    currency.spot_rate        = 999/999
,    currency.spot_rate_in_usd = 999
when not matched then insert
    currency_code
,   currency_name
,   spot_rate
,   spot_rate_in_usd
values
    'HKD'
        select  value
        from    tb_resources
        where   package = 'currency'
        and     locale  = 'en_US'
        and     key     = 'HKD'
,   999/999
,   999
Can anybody check it?
*/

Similar Messages

  • Subquery inside INSERT INTO

    Hi !
    I have a problem to put a Subquery inside an INSERT INTO-Statement.
    I try it this way: (the select-statemnet alone is running fine) (the listing is shortened)
    INSERT INTO W_CACS_ENTRY
    location_code
    ,case_num
    ,billing_account_id
    select
    a.location_code
    ,a.case_num
    ,billing.billing_account_id
    from
    select
    max(billing_account_id) as billing_account_id
    from
    select /*+ parallel ( a 4)*/
    account_no
    ,external_id
    ) case,
    w_billing_accounts bill
    where
    case.account_no = bill.account_no
    ) billing
    w_CO_CASE_HIST a
    where .....bla...
    When I try to compile it i get the following error:
    ORA-Seperator S223:
    (S223) Expecting: statement_terminator BEGIN CASE DECLARE END IDENTIFIER IF LOOP
    Is it forbidden to use Subquerys inside an INSERT TO statement?
    Are there any other possibilities?
    thanks and greets
    bang

    yes, it's inside a PL/SQL-Package. I'm using TOAD to develop it.
    Here is the whole query:
    INSERT INTO W_CACS_ENTRY
        location_code
        ,case_num
        ,EVENT_TYPE
        ,EVENT_DATE
        ,event_id
        ,cacs_case_sequence
        ,part
        ,billing_account_id
    select
         a.location_code
        ,a.case_num
        ,'CACS IN' as EVENT_TYPE
        ,a.DATE_TIME as EVENT_DATE
        ,a.sequence_number as event_id
        ,seq_cacs_case_dwh.nextval as cacs_case_sequence
        ,substr(seq_cacs_case_dwh.currval, -1) as part  
        ,billing.billing_account_id   
        from
                select  /*+ use_hash (case bill) parallel (bill 4) */
                 case_num
                ,LOCATION_CODE
                ,max(billing_account_id) as billing_account_id
                from
                    select  /*+ parallel ( a 4)*/
                    account_no
                    ,external_id  as case_num
                    ,101010 as LOCATION_CODE
                    from
                    w_CUSTOMER_ID_ACCT_MAP ar
                    union all
                    select  /*+ parallel ( a 4)*/
                    account_no
                    ,external_id  as case_num
                    ,202020 as LOCATION_CODE
                    from
                    c_CUSTOMER_ID_ACCT_MAP ar 
                ) casec,
                w_billing_accounts  bill
                where
                casec.account_no = bill.account_no
                group by
                 case_num
                ,LOCATION_CODE
           ) billing
           w_CO_CASE_HIST a
            where a.coll_activity_code in ('EN','MS')
            and a.DATE_TIME > to_date ('01.01.2006','dd.mm.yyyy')
            and billing.case_num = a.case_num
            and billing.location_code = a.location_code
    ;

  • Question on MERGE into  inside a stored procedure?

    Hello Folks,
    I have this procedure which actually has a MERGE command inside the procedure and basically all it does is it inserts all the data from the temp table when there is no match on the Bill_ID.
    Here my requirement is on a daily basis the Bill_ID can be assigned to a different Event_ID. If for example the Bill_ID has changed its event tommorrow the procedure should update the row with the new Event_ID. But in my procedure its not updating because the Bill_ID hasnt changed and its the same(coz am using MERGE insert when not matched). How do i acheive this on a day to day basis. What is the building block of code to do this. Am kinda new to programming and am trying to learn.
    CREATE OR REPLACE PROCEDURE load_fadm_staging_area_test (
      p_data_load_date DATE
    ) IS
      v_start_date   DATE;
      v_end_date     DATE;
    BEGIN
      SELECT NVL (p_data_load_date, SYSDATE) - 7,
             NVL (p_data_load_date, SYSDATE) - 1
      INTO   v_start_date,
             v_end_date
      FROM   DUAL;
      MERGE INTO stg_fadm_hri_stage_bill_test b
      USING      (SELECT *
                  FROM   stage_bill
                  WHERE  created_date BETWEEN v_start_date AND v_end_date) a
      ON         (b.bill_id = a.bill_id,)
      WHEN NOT MATCHED THEN
        INSERT     (batch_id,
                    beginning_service_date,
                    bill_id,
                    bill_method,
                    bill_number,
                    bill_received_date,
                    bill_status,
                    bill_type,
                    change_oltp_by,
                    change_oltp_date,
                    client_datafeed_code,
                    client_id,
                    created_date,
                    date_of_incident,
                    date_paid,
                    deleted_oltp_by,
                    deleted_oltp_date,
                    duplicate_bill,
                    ending_service_date,
                    event_case_id,
                    event_id,
                    from_oltp_by,
                    oltp_bill_status,
                    review_status,
                    row_effective_date,
                    row_end_date
        VALUES     (a.batch_id,
                    a.beginning_service_date,
                    a.bill_id,
                    a.bill_method,
                    a.bill_number,
                    a.bill_received_date,
                    a.bill_status,
                    a.bill_type,
                    a.change_oltp_by,
                    a.change_oltp_date,
                    a.client_datafeed_code,
                    a.client_id,
                    a.created_date,
                    a.date_of_incident,
                    a.date_paid,
                    a.deleted_oltp_by,
                    a.deleted_oltp_date,
                    a.duplicate_bill,
                    a.ending_service_date,
                    a.event_case_id,
                    a.event_id,
                    a.from_oltp_by,
                    a.oltp_bill_status,
                    a.review_status,
                    v_start_date,
                    v_end_date
    END load_fadm_staging_area_test;Edited by: user11961230 on Jan 26, 2011 9:36 AM

    Add a when matched clause to your merge statement. Something like:
    MERGE INTO stg_fadm_hri_stage_bill_test b
      USING      (SELECT *
                  FROM   stage_bill
                  WHERE  created_date BETWEEN v_start_date AND v_end_date) a
      ON         (b.bill_id = a.bill_id,)
      WHEN NOT MATCHED THEN
        INSERT     (batch_id,
                    beginning_service_date,
                    bill_id,
                    bill_method,
                    bill_number,
                    bill_received_date,
                    bill_status,
                    bill_type,
                    change_oltp_by,
                    change_oltp_date,
                    client_datafeed_code,
                    client_id,
                    created_date,
                    date_of_incident,
                    date_paid,
                    deleted_oltp_by,
                    deleted_oltp_date,
                    duplicate_bill,
                    ending_service_date,
                    event_case_id,
                    event_id,
                    from_oltp_by,
                    oltp_bill_status,
                    review_status,
                    row_effective_date,
                    row_end_date
        VALUES     (a.batch_id,
                    a.beginning_service_date,
                    a.bill_id,
                    a.bill_method,
                    a.bill_number,
                    a.bill_received_date,
                    a.bill_status,
                    a.bill_type,
                    a.change_oltp_by,
                    a.change_oltp_date,
                    a.client_datafeed_code,
                    a.client_id,
                    a.created_date,
                    a.date_of_incident,
                    a.date_paid,
                    a.deleted_oltp_by,
                    a.deleted_oltp_date,
                    a.duplicate_bill,
                    a.ending_service_date,
                    a.event_case_id,
                    a.event_id,
                    a.from_oltp_by,
                    a.oltp_bill_status,
                    a.review_status,
                    v_start_date,
                    v_end_date
    WHEN MATCHED THEN UPDATE
       SET b.event_id = a.event_id
       WHERE b.event_id != a.event_id;If other column values can change, and you want to update them as well, the add them to the list in the when matched.
    John
    Edited by: John Spencer on Jan 26, 2011 12:43 PM
    I got there first :-), but Dom had the correct (for this forum) inequality operator. Fixed the where clause. :-(

  • Merge Into works in SQL, fails in PL/SQL

    Database is 9i (Oracle9i Enterprise Edition Release 9.2.0.5.0)<br>
    <br>
    The 9i documentation--PL/SQL User's Guide and Reference
    Release 2 (9.2)--makes it seem like this is part of the PL/SQL language, but the compiler disagrees.<br>
    <br>
    Anyone else encounter this?<br>
    <br>
    Test case:<br>
    create table kev(d date, n number);<br><br>
    Table created.<br>
    <br>
    ..it works in SQL...<br>
    MERGE INTO kev k<br>2 USING ( SELECT 1 n, sysdate d from dual ) s<br>
    3 ON ( k.n = s.n )<br>
    4 WHEN MATCHED THEN<br>
    5 UPDATE<br>
    6 SET k.d = s.d<br>
    7 WHEN NOT MATCHED THEN<br>
    8 INSERT (n,d)<br>
    9 VALUES (s.n, s.d);<br>
    <br>
    1 row merged.<br>
    <br>
    ..but not in PL/SQL...<br>
    begin<br>2 MERGE INTO kev k<br>
    3 USING ( SELECT 1 n, sysdate d from dual ) s<br>
    4 ON ( k.n = s.n )<br>
    5 WHEN MATCHED THEN<br>
    6 UPDATE<br>
    7 SET k.d = s.d<br>
    8 WHEN NOT MATCHED THEN<br>
    9 INSERT (n,d)<br>
    10 VALUES (s.n, s.d);<br>
    11 end;<br>
    12 /<br>
    MERGE INTO kev k<br>
    *<br>
    ERROR at line 2:<br>
    ORA-06550: line 2, column 7:<br>
    PLS-00103: Encountered the symbol "INTO" when expecting one of the following:
    := . ( @ % ; <a SQL statement><br>
    <br>
    Thanks.

    Answer
    There are indeed DB parameters conflicting with the MERGE statement. The DB I am working on is for Oracle Applications. Apparently, although the applications are certified to run on current databases, the underlying code base has not really been brought up, and users are forced into some backward compatibility constraints. So I am stuck until we get our application upgraded from 11.5.7 to something better.
    Hope this helps someone else.
    Kevin
    From the database initialization parameters:
    # Events
    # Events should not be set unless directed by Oracle Support,
    # or by instruction as per the Applications documentation.
    # The following events should be set for PL/SQL backward
    # compatibility.
    # These events should only be used if you are using Oracle
    # Applications release 11.5.7. For later releases (i.e. 11.5.8
    # or higher), you should remove these events.
    event="10932 trace name context level 32768"
    event="10933 trace name context level 512"
    event="10943 trace name context level 16384"

  • Playerglobal.swc Linkage Type: External or Merged Into Code?

    Hi,
    I downloaded Flash Pro CS6 with flex sdk version 11.3. It didn't contain Workers functionality, so I had to download the latest playerglobal.swc 11.8 version.
    I then used Actionscript options and pointed to the playerglobal.swc.
    When  I published the fla with playerglobal.swc linkage type set to Merged into Code, the runtime pops up this error:
    VerifyError: Error #1079: Native methods are not allowed in loaded code.
    However, when  I published the fla with playerglobal.swc linkage type set to External, the runtime pops up this error:
    VerifyError: Error #1014: Class flash.system::Worker could not be found.
    The only way to get rid of the VerifyError: Error #1079: Native methods are not allowed in loaded code is to get rid of all the code that reference Workers in  the fla.
    How can I use Workers in my project!???

    Here's a really old article but using it you can still get the general idea of how to properly update Flash Pro with a new version of Flash Player.
    http://chrisgriffith.wordpress.com/2012/07/20/updating-flash-professional-cs6-to-support-1 1-4/
    It's for Mac but if you're on Windows the paths inside the folders are the same and Flash itself can be found in (C:\Program Files (x86)\Adobe\Adobe Flash CS6) Just grab the player you choose, latest or use the archive link for older, update Flash Pro so it's an option you can select as a publish target:
    http://www.adobe.com/support/flashplayer/downloads.html
    As long as you've updated your debugger (C:\Program Files (x86)\Adobe\Adobe Flash CS6\Players\Debug) with the latest debugger, always test with the debugger (CTRL+SHIFT+ENTER, not just CTRL+ENTER). Otherwise you're using an older Flash Player. You can verify the version of the debugger you're testing with checking the Capabilities.version property,
    e.g.:
    import flash.system.Capabilities;
    trace(Capabilities.version); // should say 11,8,xxx,xxx or whatever version you installed

  • Subquery within MERGE?

    Hello,
    I'm having a bit of difficulty getting a MERGE command to work with a subquery as part of the imported data. Thoughts?
    -- Clean up from prior run, if there was one
    DROP TABLE PARENT CASCADE CONSTRAINTS;
    DROP TABLE CHILD CASCADE CONSTRAINTS;
    DROP TABLE INPUT CASCADE CONSTRAINTS;
    DROP SEQUENCE ENTERPRISE_SEQ;
    --create objects for test
    CREATE TABLE PARENT(
    PARENT_ENTERPRISE_ID NUMBER(4, 0) NOT NULL,
    PARENT_SOURCE_NAME VARCHAR2(20) NOT NULL,
    PARENT_SOURCE_ID VARCHAR2(20) NOT NULL,
    PARENT_COMMENT VARCHAR2(25) NOT NULL,
    CONSTRAINT PARENT_PK PRIMARY KEY (PARENT_ENTERPRISE_ID),
    CONSTRAINT PARENT_AK UNIQUE (PARENT_SOURCE_NAME, PARENT_SOURCE_ID)
    CREATE SEQUENCE ENTERPRISE_SEQ;
    CREATE TABLE CHILD(
    PARENT_ENTERPRISE_ID NUMBER(4, 0) NOT NULL,
    CHILD_ENTERPRISE_ID NUMBER(4, 0) NOT NULL,
    CHILD_SOURCE_NAME VARCHAR2(20) NOT NULL,
    CHILD_SOURCE_ID VARCHAR2(20) NOT NULL,
    CHILD_COMMENT VARCHAR2(25) NOT NULL,
    CONSTRAINT CHILD_PK PRIMARY KEY (CHILD_ENTERPRISE_ID),
    CONSTRAINT CHILD_AK UNIQUE (CHILD_SOURCE_NAME, CHILD_SOURCE_ID)
    ALTER TABLE CHILD
    ADD CONSTRAINT CHILD_FK_PARENT
    FOREIGN KEY (PARENT_ENTERPRISE_ID)
    REFERENCES PARENT(PARENT_ENTERPRISE_ID)
    CREATE TABLE INPUT(
    INPUT_SOURCE_NAME VARCHAR2(10) NOT NULL,
    INPUT_SOURCE_ID VARCHAR2(20) NOT NULL,
    INPUT_RECORD_LEVEL VARCHAR2(10) NOT NULL,
    INPUT_COMMENT VARCHAR2(25) NOT NULL,
    INPUT_PARENT_RECORD_ID VARCHAR2(20) NULL,
    CONSTRAINT INPUT_PK PRIMARY KEY (INPUT_SOURCE_NAME, INPUT_SOURCE_ID)
    -- insert records into the table simulating external input
    -- note that the input table has two levels of records: Parent and Child
    insert into INPUT values ('Source A', '123', 'Parent', 'Input rec 1', null);
    insert into INPUT values ('Source A', '234', 'Parent', 'Input rec 2', null);
    insert into INPUT values ('Source A', '345', 'Child', 'Input rec 3', '123');
    insert into INPUT values ('Source A', '456', 'Child', 'Input rec 4', '123');
    -- Merge into Parent table
    -- This works just fine
    merge into PARENT P
    using (select INPUT_SOURCE_NAME,
    INPUT_SOURCE_ID,
    INPUT_COMMENT
    from INPUT
    where INPUT_RECORD_LEVEL = 'Parent'
    ) I
    on (I.INPUT_SOURCE_NAME = PARENT_SOURCE_NAME and
    I.INPUT_SOURCE_ID = PARENT_SOURCE_ID
    when matched then
    update set P.PARENT_COMMENT = I.INPUT_COMMENT
    when not matched then
    insert (P.PARENT_ENTERPRISE_ID,
    P.PARENT_SOURCE_NAME,
    P.PARENT_SOURCE_ID,
    P.PARENT_COMMENT
    values
    (ENTERPRISE_SEQ.NEXTVAL,
    I.INPUT_SOURCE_NAME,
    I.INPUT_SOURCE_ID,
    I.INPUT_COMMENT
    -- Merge into Child table
    -- This fails
    merge into CHILD C
    using (select INPUT_SOURCE_NAME,
    INPUT_SOURCE_ID,
    INPUT_COMMENT
    from INPUT
    where INPUT_RECORD_LEVEL = 'Child'
    ) I
    on (I.INPUT_SOURCE_NAME = C.CHILD_SOURCE_NAME
    and
    I.INPUT_SOURCE_ID = C.CHILD_SOURCE_ID
    when matched then
    update set C.CHILD_COMMENT = I.INPUT_COMMENT
    when not matched then
    insert (C.PARENT_ENTERPRISE_ID,
    C.CHILD_ENTERPRISE_ID,
    C.CHILD_SOURCE_NAME,
    C.CHILD_SOURCE_ID,
    C.CHILD_COMMENT
    values
    ((select PARENT_ENTERPRISE_ID
    from PARENT
    where PARENT_SOURCE_NAME = I.INPUT_SOURCE_NAME
    and PARENT_SOURCE_ID = I.INPUT_SOURCE_ID
    ENTERPRISE_SEQ.NEXTVAL,
    I.INPUT_SOURCE_NAME,
    I.INPUT_SOURCE_ID,
    I.INPUT_COMMENT
    select * from PARENT;
    select * from CHILD;

    ME_XE?merge into child c
      2  using
      3  (
      4     select
      5             i.input_source_name,
      6             i.input_source_id,
      7             i.input_comment,
      8             p.parent_enterprise_id
      9     from
    10             input   i,
    11             parent  p
    12     where i.input_record_level              = 'Child'
    13     and   i.input_source_name               = p.parent_source_name
    14     and   i.input_parent_record_id          = p.parent_source_id
    15  ) i
    16  on
    17  (
    18             i.input_source_name = c.child_source_name
    19     and     i.input_source_id   = c.child_source_id
    20  )
    21  when matched then
    22     update set c.child_comment = i.input_comment
    23  when not matched then
    24  insert
    25  (
    26     c.parent_enterprise_id,
    27     c.child_enterprise_id,
    28     c.child_source_name,
    29     c.child_source_id,
    30     c.child_comment
    31  )
    32  values
    33  (
    34     i.parent_enterprise_id,
    35     enterprise_seq.nextval,
    36     i.input_source_name,
    37     i.input_source_id,
    38     i.input_comment
    39  );
    2 rows merged.
    Elapsed: 00:00:00.01
    ME_XE?
    ME_XE?
    ME_XE?select * from parent;
    PARENT_ENTERPRISE_ID PARENT_SOURCE_NAME                                           PARENT_SOURCE_ID                                             PARENT_COMMENT
                       1 Source A                                                     123                                                          Input rec 1
                       2 Source A                                                     234                                                          Input rec 2
    2 rows selected.
    Elapsed: 00:00:00.00
    ME_XE?select * from child;
    PARENT_ENTERPRISE_ID CHILD_ENTERPRISE_ID CHILD_SOURCE_NAME                                            CHILD_SOURCE_ID                                              CHILD_COMMENT
                       1                   3 Source A                                                     345                                                          Input rec 3
                       1                   4 Source A                                                     456                                                          Input rec 4
    2 rows selected.
    Elapsed: 00:00:00.01
    ME_XE?

  • Family pack aliases not merged into icloud?

    My Mobile Me family pack sub mail address is updated to icloud but I did notice sub address aliases are not merged into icloud. They are still mobile me accounts in Mail. How to merge them into icloud so I could use icloud.com too? Or will Apple delete aliases still under MM some times?
    Anyone find same issue?

    Check out these support articles.
    MobileMe: Email aliases can no longer be added, changed, or deleted
    and here >  iCloud: About email aliases

  • Error when attempting to merge into table with RLS - ORA - 28132

    hey,
    i've encountered an "ORA-28132 - Merge into syntax does not support security policies" when attempting to merge into a table with RLS defined on it.
    i looked around and found this [discussion |http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:259415261270] on ask tom.
    according to the thread - this was a bug in 9i. in 10g merge is possible only if the policy returns "null" and in 11g it was finally fixed.
    *i have been able to merge into tables with rls in 10gR2 in some cases.*
    i would like to know if anyone has some more information about this error. when can i merge into tables with RLS ?
    thanks, Haki.

    Never.
    The bug was that it did work sometimes in 10g i.e. it should always raise an error.
    It's something that I've always found a bit odd and frustrating - that MERGE and RLS policy should not be compatible.
    But bottom line is that you shouldn't be using MERGE on table with RLS policy.
    Edited by: Dom Brooks on Feb 14, 2011 10:51 AM

  • Merge into table using cursor

    Hi,
    I am trying to merge into test table using a cursor.
    If the contract number in the test table matches with the contract number from the cursor then i need to update the creation_date else i need to insert values from the cursor into the test table. I know this is wrong usage of cursor, but i would appreciate suggestions regarding this.
    FOR i IN cursortest
    MERGE INTO test
             USING cursortest
    ON (i.contract_number = test.contract_number)
             WHEN MATCHED THEN
                UPDATE
                   SET creation_date = SYSDATE
             WHEN NOT MATCHED THEN
                INSERT (customer_id, contract_number, creation_date)
                VALUES (i.customer_cpr_id, i.contract_number,SYSDATE);
    end loop;

    I know this is wrong usage of cursor, but i would appreciate suggestions regarding this.
    >
    FOR i IN cursortest
    MERGE INTO test
    USING cursortest
    ON (i.contract_number = test.contract_number)
    WHEN MATCHED THEN
    UPDATE
    SET creation_date = SYSDATE
    WHEN NOT MATCHED THEN
    INSERT (customer_id, contract_number, creation_date)
    VALUES (i.customer_cpr_id, i.contract_number,SYSDATE);
    end loop;I think you're close. MERGE is designed to do a subquery internally, pretty much what I think you are trying to do in your cursor. Put the subquery into your using clause and see how that works. You can see examples in the on-line documentation

  • MERGE INTO and CROSS JOIN

    Hello Guys,
    I have a bit of trouble understanding the MERGE I have written here.
    All the code executes without errors. As far as I see,
    the below MERGE SELECT is a Cartesian product/cross join and I remember that is not
    good. The ON (join condition) seems to be only for the merge into table (T_CLAIMS_BB) and
    the table that comes from the select (d).
    merge into T_CLAIMS_BB t
    using
    SELECT sch_vt.SCH#,vert.CNO,vers.SubCNo,regv.ReturnNo,regv.SndReturnNo,sch_vt.MainProd,sch_vt.Prod
    FROM mainschema.TCONTRACT vert, mainschema.TVERSCONTRACT vers, commonschema.VC_REGVEREINB regv, myschema.T_CLAIMS_VT sch_vt
    ) d
    ON (
    t.sch# = d.sch# AND
    SUBSTR(UPPER(t.BOOKINGTEXT),1,2) = d.SndReturnNo AND d.SubCNo IS NULL
    ) -- Join
    when matched then
    update
    set t.CNO = 'a',
    t.ReturnNo = 'a',
    t.MainProd = 'b',
    t.Prod = 'c';
    I wonder now, what is the advantage of using MERGE in my case - if there is one.
    Finally I want to demonstrate how the whole thing would look without a MERGE:
    SELECT vers.* FROM TVERSCONTRACT vers
    LEFT OUTER JOIN myschema.T_CLAIMS_BB sch ON sch.SubCNo = vers.SubCNo
    LEFT OUTER JOIN commonschema.VC_REGVEREINB regv ON sch.SCH# = regv.SCH# AND SUBSTR(UPPER(sch.BUCHTEXT),1,2) = regv.SndReturnNo AND sch.SubCNo IS NULL
    LEFT OUTER JOIN myschema.T_CLAIMS_VT sch_vt ON sch.SCH# = sch_vt.SCH#

    Hi,
    metalray wrote:
    Hello Guys,
    I have a bit of trouble understanding the MERGE I have written here.
    All the code executes without errors. As far as I see,
    the below MERGE SELECT is a Cartesian product/cross join and I remember that is not
    good.Cross joins are good for some purposes. In practice, they're useful in only 1 or 2 queries out of every 1000.
    Using the old join notation, it was very easy to get a cross join by mistake. Given how rarely cross joins are needed, the vast majority of cross joins (using old notation) were not good, but that was the fault of the programmers and the notation, not an indication that cross join itself was bad.
    The ON (join condition) seems to be only for the merge into table (T_CLAIMS_BB) and
    the table that comes from the select (d).Exactly!
    You can have separate ON conditions inside the USING sub-query, if you need them.
    >
    merge into T_CLAIMS_BB t
    using
    SELECT sch_vt.SCH#,vert.CNO,vers.SubCNo,regv.ReturnNo,regv.SndReturnNo,sch_vt.MainProd,sch_vt.Prod
    FROM mainschema.TCONTRACT vert, mainschema.TVERSCONTRACT vers, commonschema.VC_REGVEREINB regv, myschema.T_CLAIMS_VT sch_vt
    ) d
    ON (
    t.sch# = d.sch# AND
    SUBSTR(UPPER(t.BOOKINGTEXT),1,2) = d.SndReturnNo AND d.SubCNo IS NULL
    ) -- Join
    when matched then
    update
    set t.CNO = 'a',
    t.ReturnNo = 'a',
    t.MainProd = 'b',
    t.Prod = 'c';
    I wonder now, what is the advantage of using MERGE in my case - if there is one.I don't see any advantage, but I suspect the statement, as written, is not doing what it's supposed to. If someone made the mistake of using MERGE when UPDATE would be better, that might be the least of the mistakes in this statement.
    Finally I want to demonstrate how the whole thing would look without a MERGE:It looks like the MERGE statement above is equivalent to:
    UPDATE     t_claims_bb
    SET     cno          = 'a'
    ,     returnno     = 'a'
    ,     mainprod     = 'b'
    ,     prod          = 'c'
    WHERE     UPPER (SUBSTR (bookingtext, 1, 2)) IN
              SELECT  returnno
              FROM     commonschema.vc_regvereinb
    AND     EXISTS
              SELECT  1
              FROM     mainschema.tverscontract
              WHERE     subcno     IS NULL
    AND     EXISTS
              SELECT     1
              FROM     mainschema.tcontract
    AND     EXISTS
              SELECT     1
              FROM     myschema.t_claims_vt
    ;Again, I suspect that the MERGE state,ment above is not doing what it was meant to do, or may coinncidentally be getting the right results on a small sample set. Many of the tables named in the USING clause of the MERGE statement do'nt seem to have much of a role in this problem.
    SELECT vers.* FROM TVERSCONTRACT vers
    LEFT OUTER JOIN myschema.T_CLAIMS_BB sch ON sch.SubCNo = vers.SubCNo
    LEFT OUTER JOIN commonschema.VC_REGVEREINB regv ON sch.SCH# = regv.SCH# AND SUBSTR(UPPER(sch.BUCHTEXT),1,2) = regv.SndReturnNo AND sch.SubCNo IS NULL
    LEFT OUTER JOIN myschema.T_CLAIMS_VT sch_vt ON sch.SCH# = sch_vt.SCH#What is this query? Perhaps you meant to have something like this in the USING clause of the MERGE.
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all the tables involved, and the results you want from that data.
    The sample data should show what the tables are like before the MERGE (or UPDATE), and the results will be the contents of the changed table after the MERGE.
    Explain, using specific examples, how you get those results from that data.
    Always say what version of Oracle you're using.

  • Two sets of faces have merged into one. How can I separate them back into two groups or delete the bunch

    Two different sets of faces have merged into one under one name. How can I separate them back or delete the bunch?

    Look at Steve MacGuire's helpful article:  http://samsoft.org.uk/iTunes/grouping.asp  It explains how to solve this much better than I can.

  • All tracks from one artist merged into one huge album. How to separate into correct albums?

    So all the songs from one artist have merged into one huge album. All I did was select all the songs and changed the artist name. Not sure why my iPod has it under one album. On iTunes they're separated into the correct albums but on my iPod it isn't. All the info (Artist name, album name, etc) is good, i checked a million times and still can't seem to figure out why my iPod is reading it like that. Has anyone ever had this problem?

    Thanks for the great link!
    Although it doesn't allow me to cut 'n paste, like I was hoping, the answers give me a lot of insight as to how the track fields are structured, as well as full control over whether or not I want to group tracks together within the same Album.  So, in the end, this information is more useful than the cut 'n paste function, as it prevents me from losing potentially useful track information.
    John

  • How does one set up two different iTunes accounts on the same computer so that they do not merge into one?

    How does one set up two different iTunes accounts on the same computer so that they do not merge into one?

    mparker402 wrote:
    So, when the other iTunes account needs to be used, I should log out of Windows and have the other user log in under a different Windows account, correct?  
    M Parker, 
    Yes, since you are trying to keep things separate, that is exactly what you would do.

  • I have 2 icloud accounts. can they be merged into one?

    I have two iCloud accounts and want to delete one of them.  Can the two be merged into one?

    Welcome to the Apple Community.
    You can't merge accounts but there may be some things you can do, what exactly are you trying to achieve by merging accounts.

  • I have 20,00 images in my iphoto they have all been merged into one event. How do I restore them to the original events and dates? them

    I have 20,00 images in my iphoto they have all been merged into one event. How do I restore them to the original events and dates?

    Load the backup from before this happened
    LN

Maybe you are looking for

  • PDF Printing problems with networked HP Laserjet C5020N

    Hello, I'm having problems printing documents that I've created with the PDF creator of Office 2007. When I try to print only the first page, the printer correctly prints, but when I try any other job (printing the whole document, only the 2nd page,

  • Call .html file in ABAP report (se38)

    Hi, My question is, is it possible to call and execute an html file in a report? For example, you have 2 containers in the report, 1 is for the html part and the 2nd for some ALV table (which is not the problem in this discussion). The HTML part is a

  • How to include citation information into document description

    I am a chemistry professor in a small college and have a large number of journal articles as pdf's that I reference in my research.  I'd would like to include fields for the article citation such as:  Journal, Volume, Issue, page, Author 1, Author 2.

  • EDI 820

    Hi SAP Experts, Is it possible to create a abap program to generate EDI 820 format (with mapping SAP tables and fields to EDI 820) while doing the payment run with F110. I mean without mapping IDOC with EDI 820. Please advise, Regards, Sim

  • Bootcamp on Mac 10.6.8?S

    So I'm using this Macbook (not Pro), that is on version Snow Leopard 10.6.8, if I'm not wrong that is. I want to use Window programs and found out that there is the software bootcamp that allows me to do so on a Mac. I might not mind having to pay fo