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 8519795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:21:08+00:00 2026-06-11T06:21:08+00:00

I need to create and execute a query dynamically in a stored procedure. I

  • 0

I need to create and execute a query dynamically in a stored procedure.

I have two tables users and user_updates.

Employee table has emp_id, username, division, product, region, title etc.

Employee_updates has columns like emp_id, effective_date, column_name, new_value etc.

Basically this is what I want to do.

  1. Get all employees having updates in user_udates table for a given effective date.

  2. Loop over each employee.

  3. Get all updates for each employeefor the given effective date. A employee may have one or more than one updates in employee_updates table.

  4. Create a dynamic "UPDATE" query based on those updates like

    update employee set col1 = new_val_1, col2 = new_val_2 where emp_id = ?

This is what I have done so far

    create or replace
PROCEDURE SP_RUN_EMPLOYEE_UPDATES 
(
  IN_DATE IN DATE
) 
  IS
      
    update_sql varchar2(225); 
      
employee_id BI_EMPLOYEE_UPDATE.employee_id%TYPE;   

    CURSOR  employees 
        IS   SELECT distinct(employee_id)
               FROM BI_EMPLOYEE_UPDATE 
             WHERE EFFECTIVE_DATE = to_date(IN_DATE,'dd-mm-yy') 
              AND EXECUTED = 'N' AND ACTIVITY_ID = '0'
              ;
   
    CURSOR e_updates 
    IS  SELECT * 
         FROM BI_EMPLOYEE_UPDATE 
        WHERE EFFECTIVE_DATE = to_date(IN_DATE,'dd-mm-yy') 
         AND EXECUTED = 'N' 
         AND ACTIVITY_ID = '0' 
         and employee_id = employee_id ;
      
     BEGIN
      
   OPEN employees;
   LOOP
    FETCH employees into employee_id;
        EXIT WHEN employees%NOTFOUND;
            
           FOR e_update in e_updates
         update_sql :=  'UPDATE BI_EMPLOYEE SET ';
              LOOP
                -- create dynam,ic update statment
             
                 UPDATE BI_EMPLOYEE_UPDATE
                 SET EXECUTED = 'Y'
                 WHERE EMPLOYEE_UPDATE_ID = e_update.EMPLOYEE_UPDATE_ID ;
                
                END LOOP;
            
            -- run dynamic sql 
     
              END LOOP;
 CLOSE employees;
END;

PLease help.

  • 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-11T06:21:10+00:00Added an answer on June 11, 2026 at 6:21 am

    You have a few issues here, including:

    • IN_DATE is declared as a date, so you don’t need to pass it through TO_DATE().
    • You only need one cursor loop; if you want to process all updates for an employee_id together for some reason you can add an order by clause.
    • You don’t need dynamic SQL at all; you can use the values from the cursor as part of a static SQL update.

    So a simple version with a single loop might look something like:

    CREATE OR REPLACE PROCEDURE sp_run_employee_updates (p_date IN DATE) IS
        CURSOR c_updates IS
            SELECT *
            FROM bi_employee_update
            WHERE effective_date = p_date
            AND executed = 'N' 
            AND activity_id = '0'
            FOR UPDATE;     
    BEGIN
        -- loop around all pending records
        FOR r_update IN c_updates LOOP
            -- apply this update to the bi_employee record
            UPDATE bi_employee
            SET col1 = r_update.col1, col2 = r_update.col2
            WHERE emp_id = r_update.employee_id;
    
            -- mark this update as executed
            UPDATE bi_employee_update
            SET executed = 'Y'
            WHERE CURRENT OF c_updates;
        END LOOP;
    END sp_run_employee_updates;
    

    This is using the for update and where current of constructs to both lock the row you’re working with and to simplify the update; see the documentation here.

    It’s worth noting that if either effective_date or p_date has a time component they won’t match. It’s unlikely for p_date, but harder to guess for effective_date. If it does then you either need to trunc() it, or use between to look for a range of times.

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

Sidebar

Related Questions

I have dynamically created WrapPanel (_wp) with several Borders. And I need create handler
I need to execute a SQL query two hours after I insert a row
I am creating stored procedure in which I need to build a temporary table
I'm writing a script and I need to create a loop that will execute
Got a strange problem created a little stored proc which need to execute a
I use MS SQL 2008 R2, I need create a Table with a CHECK
I need to execute a query that is highly dependent on several conditions what
Given this MySQL stored procedure: CREATE PROCEDURE customer.`getCustomers5`( sdf varchar(1000) ) BEGIN set @se
Afternoon All, I have a stored procedure in an SQL 2005 database named GasNominationsRawData_Insert.
Is there any chance to create temporary stored procedure or function on MS SQL

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.