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

  • SEARCH
  • Home
  • 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 7741345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:00:11+00:00 2026-06-01T09:00:11+00:00

I am writing a PL/SQL Procedure that performs a select based on input variables

  • 0

I am writing a PL/SQL Procedure that performs a select based on input variables and then inserts a row for each result in the select. I am having trouble debugging what is wrong with my query due my newness to PL/SQL. I know this must be easy, but I am stuck here for some reason. Thanks for your help!

CREATE OR REPLACE PROCEDURE setup_name_map(ranking_id IN NUMBER, class_string IN VARCHAR2) 
IS
BEGIN

    FOR rec IN (SELECT NAME_ID FROM PRODUCT_NAMES WHERE NAME = class_string)
    LOOP
        EXECUTE IMMEDIATE 'INSERT INTO NAME_RANKING (NAME_ID, RANKING_ID) VALUES (' || rec.NAME_ID || ', ' || ranking_id || ')';
    END LOOP;
END;

According to the Oracle Developer Compiler… ‘NAME_ID’ is an invalid identifier. I’ve tried putting it in quotes but no dice. It also complains that loop index variables ‘REC’ use is invalid. Any help is much appreciated.

  • 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-01T09:00:13+00:00Added an answer on June 1, 2026 at 9:00 am

    There is no need for dynamic SQL here:

    BEGIN
    
        FOR rec IN (SELECT NAME_ID FROM PRODUCT_NAMES
                    WHERE NAME = class_string)
        LOOP
            INSERT INTO NAME_RANKING (NAME_ID, RANKING_ID)
            VALUES (rec.NAME_ID, ranking_id);
        END LOOP;
    END;
    

    Better still you can avoid a slow row-by-row cursor approach like this:

    BEGIN
        INSERT INTO NAME_RANKING (NAME_ID, RANKING_ID)
        SELECT NAME_ID, ranking_id FROM PRODUCT_NAMES
        WHERE NAME = class_string;
    END;
    

    If you really did need the dynamic SQL you should not be concatenating values into it, but using bind variables:

    BEGIN
    
        FOR rec IN (SELECT NAME_ID FROM PRODUCT_NAMES
                    WHERE NAME = class_string)
        LOOP
            EXECUTE IMMEDIATE 'INSERT INTO NAME_RANKING 
                               (NAME_ID, RANKING_ID) VALUES (:b1, :b2)
            USING rec.NAME_ID, ranking_id;
        END LOOP;
    END;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having problems with a stored procedure that I am writing for SQL
I'm writing a stored procedure on SQL Server 2000. I've written a complicated select
I am writing a query for a sql input statement, i noticed that when
I am writing a PL/SQL procedure that needs to to dynamically generate some queries,
I am writing a stored procedure that works with a heavy select statement. The
I am writing a stored procedure that needs to execute a .sql source file.
I am writing a stored procedure in SQL Server 2008. Here I have a
I'm writing SQL (for Oracle) like: INSERT INTO Schema1.tableA SELECT * FROM Schema2.tableA; where
I always have this notion that writing SQL queries in the code behind is
Okay so I'm writing a SQL Server 2008 Stored Procedure (maintenance script). In doing

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.