SQL: Add counter into View results

Posted on 2008-10-30
Last Modified: 2010-04-21
I have an exisiting view that is called by an application. I am looking to add an incrementing counter to the output. The counter should be reset each time the view is called, and increment for each row of results when run.

Can someone give me push in the right direction how I would do this.

Question by:JDCam
  • 5
  • 2
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22842219
try to use the ROW_NUMBER() function in the view query.
LVL 39

Expert Comment

ID: 22842292
An additional "push" would be

select row_number() over (order by Some_field_or_fields_in_your_view) rn, *
from Yourview

Author Comment

ID: 22842429
Ok Great,
give me a chance to test it out
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.


Author Comment

ID: 22842810
here is a snipit what I have...

SELECT Orders.Order_ID, ROW_NUMBER() over(Order by MO.MU_ID) RowNbr, Orders.Ship_To_Customer_Name,......

but get the result..
'ROW_NUMBER' is not a recognized function name.

is this not a standard function ??

Author Comment

ID: 22842832
I guess it is important to add the DB is on SQL 2000 (sorry). I dont see this function there. Is there something comparable I can use ??
LVL 39

Accepted Solution

BrandonGalderisi earned 500 total points
ID: 22843019
row_number() is a SQL 2005+ function.  You posted the question int the SQL Server 2005 zone and not the MS SQL Server Zone.  You really can't do this from a view.  You would have to do it through a procedure that inserts into a temp table containing an identity column.

Author Closing Comment

ID: 31511712
Thanks Brandon,
Sorry about the confusion. I use 2005 Mgr Studio on my desktop, but this particualar DB is still in 2000.  I will try as you suggest

Author Comment

ID: 22843326
I will ask for additional help with this suggestion under a new question. (and properly categorized)

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question