Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7940831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:29:21+00:00 2026-06-03T23:29:21+00:00

I have made a procedure in PL/SQL which inserts data from one table to

  • 0

I have made a procedure in PL/SQL which inserts data from one table to another on basis of primary key. My procedure is working fine but i can’t figure out how will i update column CODE_NUMBER of my table MAIN if primary key already exists.
Actually i want rows of MAIN table to get UPDATED when its has primary key and insert data from REGIONS when primary key does not exists.

DECLARE
    variable number;
    id number;
    description varchar2 (100);

    CURSOR C1 IS
        select regions.REGION_ID variable
        from regions;

BEGIN
      FOR R_C1 IN C1 LOOP
            BEGIN
                select regions.REGION_ID,regions.REGION_NAME
                into id,description
                from regions
                where regions.REGION_ID = R_C1.variable; 
                ----If exists then update otherwise insert
                INSERT INTO MAIN( ID, CODE_NUMBER) VALUES( id,description);
                dbms_output.put_line( id ||' '|| 'Already Exists');
            EXCEPTION
                WHEN DUP_VAL_ON_INDEX THEN
                dbms_output.put_line( R_C1.variable);
            END;
      END LOOP;
END; 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:29:23+00:00Added an answer on June 3, 2026 at 11:29 pm

    There’s no need to do this with PL/SQL and cursors. What you really want to do is something like this:

    MERGE INTO MAIN dst
    USING (
      SELECT regions.REGION_ID id,
             regions.REGION_NAME description
      FROM   regions
    ) src
    ON src.id = dst.id
    WHEN MATCHED THEN UPDATE 
      SET dst.code_number = src.description
    WHEN NOT MATCHED THEN INSERT (id, code_number)
      VALUES (src.id, src.description)
    

    Read more about the SQL MERGE statement in the documentation

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using MS SQL Server .I have made a Stored Procedure named SP_Get_CallsLogged . I
I have two processes that work with data in the same table. One process
I have made a game which is nearly identical to Popcap's Atomica. ( http://www.popcap.com/gamepopup.php?theGame=atomica
I have made a basic ascx control which is just a panel with a
I have inherited a SQL Server 2008 database to which calling applications have access
What I have: LINQ to SQL auto generated class, which corresponds to the shape
I want to return all rows from a SQL Server 2008 database table into
I have a service broker which calls a stores procedure sp_dequeue queue --> ACTIVATION
I made a query to SQL Server to get some data via a Stored
I have this script(dtsx file) on a sql 2005 server (made by some other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.