DBAsupport.com Forums - Powered by vBulletin
Results 1 to 6 of 6

Thread: How to convert this proc into Merge command

  1. #1
    Join Date
    Jul 2003
    Location
    Cincinnati
    Posts
    68

    How to convert this proc into Merge command

    create or replace procedure mtg_load as
    v_osuser mtg_sessions.osuser%type;
    v_username mtg_sessions.username%type;
    v_program mtg_sessions.program%type;
    v_machine mtg_sessions.machine%type;
    V_N NUMBER;

    cursor c1 is
    select osuser, username, program, machine
    from v$session;
    --where rownum < 20;

    begin
    open c1;
    loop
    fetch c1 into v_osuser, v_username, v_program, v_machine;
    exit when c1%notfound;
    dbms_output.put_line(v_osuser||';'||v_machine);

    begin
    select 1 into v_n
    from mtg_sessions
    where osuser=v_osuser and machine=v_machine;

    update mtg_sessions
    set username = v_username,
    program = v_program
    where osuser = v_osuser
    and machine = v_machine;

    exception
    when no_data_found then
    insert into mtg_sessions values (v_osuser, v_username, v_program, v_machine);
    commit;
    when others then null;
    end;

    end loop;
    close c1;

    end;
    /

    show err
    Last edited by st2003; 11-11-2004 at 10:56 AM.

  2. #2
    Join Date
    Nov 2000
    Location
    Israel
    Posts
    268
    Code:
    MERGE INTO g5mtg_sessions g_ses
         USING v$session v_ses
            ON (    g_ses.osuser = v_ses.osuser 
                AND g_ses.machine = v_ses.machine)
       WHEN MATCHED THEN
          UPDATE
             SET username = v_ses.username, program = v_ses.program
       WHEN NOT MATCHED THEN
          INSERT (osuser, machine, program, username)
          VALUES (v_ses.osuser, v_ses.machine, v_ses.program, v_ses.username)
    /
    Hope this helps...
    It is better to ask and appear ignorant, than to remain silent and remain ignorant.

    Oracle OCP DBA 9i,
    C++, Java developer

  3. #3
    Join Date
    Nov 2002
    Location
    Geneva Switzerland
    Posts
    3,142
    Firstly, does this actually work? I don't think the UPDATE will give a NO_DATA_FOUND exception. Test on SQL%ROWCOUNT = 0, 1 and >1

    Secondly, did you try a MERGE?


    (P.S. rotem_fo must be having a better day than I am . . . )
    Last edited by DaPi; 11-11-2004 at 11:15 AM.

  4. #4
    Join Date
    Nov 2000
    Location
    Pittsburgh, PA
    Posts
    4,166
    Take your pick.

    Code:
    CREATE OR REPLACE PROCEDURE mtg_load
    AS
       CURSOR c_v_session IS
          SELECT osuser, username, program, machine
            FROM v$session;
    BEGIN
       FOR v_row IN c_v_session
       LOOP
          BEGIN
             INSERT INTO mtg_sessions
             VALUES ( v_row.osuser,
                      v_row.username,
                      v_row.program,
                      v_row.machine );
          EXCEPTION
             WHEN DUP_VAL_ON_INDEX
             THEN
                UPDATE mtg_sessions
                   SET username = v_row.username,
                       program  = v_row.program
                 WHERE osuser   = v_row.osuser   AND
                       machine  = v_row.machine;
             WHEN OTHERS
             THEN
                RAISE;
          END;
    
       END LOOP;
    
       COMMIT;
    END;
    /
    Code:
     
    MERGE INTO mtg_sessions mtg
    USING ( SELECT osuser, username, program, machine FROM v$session ) vsess
       ON ( mtg.osuser   = vsess.osuser   AND
            mtg.machine  = vsess.machine )
     WHEN MATCHED THEN UPDATE SET   mtg.username = vsess.username,
                                    mtg.program  = vsess.program
     WHEN NOT MATCHED THEN INSERT ( mtg.osuser,
                                    mtg.username,
                                    mtg.program,
                                    mtg.machine)
                           VALUES ( vsess.osuser,
                                    vsess.username,
                                    vsess.program,
                                    vsess.machine);
    Last edited by gandolf989; 11-11-2004 at 11:18 AM.

  5. #5
    Join Date
    Nov 2000
    Location
    Pittsburgh, PA
    Posts
    4,166
    Originally posted by DaPi
    Firstly, does this actually work? I don't think the UPDATE will give a NO_DATA_FOUND exception. Test on SQL%ROWCOUNT = 0, 1 and >1

    Secondly, did you try a MERGE?


    (P.S. rotem_fo must be having a better day than I am . . . )
    Sorry dude, the select into should generate a no data found. Second, he wanted someone to give him the syntax for the merge.

  6. #6
    Join Date
    Nov 2002
    Location
    Geneva Switzerland
    Posts
    3,142
    Originally posted by gandolf989
    Sorry dude, the select into should generate a no data found.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width