Solved

SQL: Add counter into View results

Posted on 2008-10-30
8
1,265 Views
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.

Thanks
0
Comment
Question by:JDCam
  • 5
  • 2
8 Comments
 
LVL 142

Expert Comment

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

Expert Comment

by:BrandonGalderisi
Comment Utility
An additional "push" would be


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

Author Comment

by:JDCam
Comment Utility
Ok Great,
give me a chance to test it out
Thanks
0
 

Author Comment

by:JDCam
Comment Utility
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 ??
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:JDCam
Comment Utility
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 ??
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
Comment Utility
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.
0
 

Author Closing Comment

by:JDCam
Comment Utility
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
0
 

Author Comment

by:JDCam
Comment Utility
I will ask for additional help with this suggestion under a new question. (and properly categorized)
thanks.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now