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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:37:18+00:00 2026-06-11T18:37:18+00:00

We have a sql script to update a set of sequences after seed data

  • 0

We have a sql script to update a set of sequences after seed data populated our tables. The code below would not work:

declare
cursor c1 is 
    select
      'select nvl(max(id),0) from '||uc.table_name sql_text,
      uc.table_name||'_SEQ' sequence_name 
    from
      user_constraints uc,
      user_cons_columns ucc
    where uc.constraint_type='P'
      and ucc.constraint_name = uc.constraint_name
      and ucc.column_name='ID'
      and uc.owner='ME';

alter_sequence_text varchar2(1024);
TYPE generic_cursor_type IS REF CURSOR;
max_id number;
c2 generic_cursor_type;
begin
for r1 in c1 loop
    open c2 for r1.sql_text;
    fetch c2 into max_id;
    close c2;

    if( max_id != 0 ) then
        dbms_output.put_line( 'seq name = '||r1.sequence_name );
        execute immediate 'alter sequence '||r1.sequence_name||' increment by '||to_char(max_id);
        dbms_output.put_line( 'max_id = '||to_char(max_id) );
        execute immediate 'select '||r1.sequence_name||'.nextval from dual';
        dbms_output.put_line( 'sequence value = '||to_char(next_id) );
        execute immediate 'alter sequence '||r1.sequence_name||' increment by 1';
        dbms_output.put_line( 'sequence: '||r1.sequence_name||' is at '||to_char(max_id+1) );
    end if;
end loop;
end;

After searching I found a reference that stated I needed to change the line:

execute immediate 'select '||r1.sequence_name||'.nextval from dual'

and add ‘into next_id;’ (of course declaring next_id appropriately) so the result would be:

execute immediate 'select '||r1.sequence_name||'.nextval from dual into next_id;

I’ve only dealt lightly with pl/sql and sql in general and am interested to know why this change was necessary to make the script work correctly.

Thanks.

  • 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-11T18:37:19+00:00Added an answer on June 11, 2026 at 6:37 pm

    When you are using select inside PL/SQL block you have to place data returned by that select statement somewhere. So you have to declare a variable of appropriate data type and use select into clause to put data select returns into that variable even if select statement is executed by execute immediate statement.

    Examples

    declare
      x number;
    begin
      select count(*)
        into x
        from all_objects;
    end;
    
    
    declare
      x number;
    begin
      execute immediate 'select count(*)from all_objects' into x;
    end;
    

    So your execute immediate statement would be

    execute immediate 'select '||sequence_name||'.nextval from dual' into newseqval;
    

    If you are using Oracle 11g onward you can assign sequence’s value directly to a variable, there is no need of using select into clause.

     declare
       x number;
     begin
       x := Sequence_Name.nextval;
     end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sql script that looks like UPDATE dbo.Pictures SET Data = (SELECT
I have a 500,000 line SQL script: UPDATE users SET region_id = 9814746 WHERE
I have a 500,000 line sql script. update users set region_id = 9814746 where
I am using SQL Server 2008 R2, I have a script to update the
I have this sql script DECLARE @XmlStr XML SET @XmlStr = '<EmployeeID> <Employee>48f9194f-8d46-e111-8849-0050569445f1</Employee> <Employee>7d725561-8d46-e111-8849-0050569445f2</Employee>
I have a SQL Script that I execute in a job to transform tables
I have written an SQL script having the below query. The query works fine.
I have a .sql script file that I want to run once every 24
I have an SQL script which creates a scheduled event: CREATE EVENT Daily_1200PM SCHEDULE
I have a SQL script that populates a temp column and then drops the

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.