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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:38:17+00:00 2026-05-23T06:38:17+00:00

I have a table with 2 varchar2 columns. I have added new number column

  • 0

I have a table with 2 varchar2 columns. I have added new number column to existing table to make this column primary key. This table now includes 3 columns. I gave a try to use anonymous block as following:


declare
cnt number;
begin
select nvl(count(*),0) into cnt from sometable;
for i in 1..cnt
loop
update sometable set id=i where i=rownum;
end loop;
end;

Using this anonymous block updates this table unexpectedly.
My solution was to use the following statement:

create table sometablecopy as select row_number() over(order by sometable.col1) as id, sometable.* from sometable;

Nevertheless I am curios why doesn’t anonymous block produce expected primary key values with the help of rownum pseudocolumn? It must be rownum related issue.

  • 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-05-23T06:38:18+00:00Added an answer on May 23, 2026 at 6:38 am

    Rownum is a pseudocolumn. Its assigned to rows as they are returned from the select. So you can’t say “select * from my_table where rownum = 42” since the row with rownum=42 hasn’t been defined yet, it will vary depending on your select and predicate (and “select * from my_table where rownum = 1” will return a single row, not the “first” row, whatever that would be). You could do something like (untested):

    declare
      cursor sel_cur is
      select rowid as row_id
      from my_table
      order by orderby_col;
    
      v_ctr pls_integer := 0;
    begin
      for rec in sel_cur
      loop
        v_ctr := v_ctr + 1;
        update my_table set pk_col = v_ctr where rowid = rec.row_id;
      end loop;
      commit;
    exception
      when others then 
      rollback;
      raise;
    end;
    

    This assumes you have sufficient rollback to update the entire table.

    Hope that helps.

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

Sidebar

Related Questions

I have a table which has two varchar(Max) columns Column 1 Column 2 -----------------------
I have created a new table including a column note. The default is varchar(255)
I have a table declaration: TYPE PERSON_TYPE AS OBJECT (ID NUMBER(38), NAME VARCHAR2(20)); TYPE
Lets say I have this SQL statements: ALTER TABLE dbo.[tbl] ALTER COLUMN col1 varchar(300)
I have a table that I added a column called phone - the table
I have a TO DO Table with a Position Column. (other columns removed for
Hello I have a table with columns: *using oracle ID NUMBER USER_ID NUMBER DATE_ADDED
I have a table with three columns: user varchar, status varchar , rep int
I have a table whose columns are varchar(50) and a float . I need
I have a table, tblDocs that has a few columns: DocName varchar(50) DocLocation int

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.