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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:16:50+00:00 2026-06-04T17:16:50+00:00

I am trying to use row_number or rank just to insert a row number

  • 0

I am trying to use row_number or rank just to insert a row number so I can enumerate over a temp table without using a cursor. Both functions give me error “Incorrect syntax near ‘rank’, expected ‘OVER'”. I do not need all of the functionality + performance impact that OVER provides – I don’t care one bit about order/ranking etc. I’m sure I’ve done this before but it was a few years ago, can someone point me to the correct function?

declare @SomeTempTable table (RowNum int NOT NULL, SomeField int NOT NULL);

insert into @SomeTempTable
  select rank() as RowNum, SomeField
  from SomeTable 

declare @RowNum int = 1;
declare @NumRows int = (select max(RowNum) from @SomeTempTable);

while@RowNum <= @NumRows
begin
  declare @SomeField int;

  select @SomeField = SomeField
  from @SomeTempTable
  where RowNum = @RowNum;

  --TODO: Do stuff  

  set @RowNum = @RowNum + 1;               
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-04T17:16:51+00:00Added an answer on June 4, 2026 at 5:16 pm

    Hmmm, you don’t get to decide on the syntax of SQL functions, even if it is appealing to you.

    There are two methods to get what you want. The first is the standard method, that doesn’t use windows functions at all:

    declare @SomeTempTable table (
        RowNum int identity(1,1) not null,
        SomeField int NOT NULL
    );
    
    insert into @SomeTempTable(SomeField)
         select SomeField
         from SomeTable 
    

    Voila! Let the identity do the work.

    The second is the approach you were taking:

    insert into @SomeTempTable(RowNum, SomeField)
        select row_number() over (order by (select NULL)) as RowNum, SomeField
        from SomeTable 
    

    The “order by (Select NULL)” appears to be a magical incantation in SQL Server to just assign numbers, without actually doing a sort.

    As an aside, when you do an insert, you should always specify the columns in the insert statement.

    Also, it is quite possible that the WHILE loop could be re-written as a query, making everything more efficient (not always, but sometimes).

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

Sidebar

Related Questions

I'm trying to use jQuery to append a row to a table and to
I'm trying to use jQuery to add dynamic Add/Remove row function, but I meet
I'm trying to covert a coordinate value in excel to a row number and
I am trying to use the unnest function in insert . While doing so,
I'm trying to use a UITextField inside a UITableViewCell as you can see in
Im trying to use a dictionary to check a given number of servers listed
So, I'm trying to use the following trigger so that, when a row in
I am trying to use the ROW_NUMBER function, but I am running into two
I am trying to use link to submit a form using the following code:
I am trying to use JSP on server-side to perform a variable number of

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.