Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1272
  • Last Modified:

SQL: Add counter into View results

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
JDCam
Asked:
JDCam
  • 5
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
try to use the ROW_NUMBER() function in the view query.
0
 
BrandonGalderisiCommented:
An additional "push" would be


select row_number() over (order by Some_field_or_fields_in_your_view) rn, *
from Yourview
0
 
JDCamAuthor Commented:
Ok Great,
give me a chance to test it out
Thanks
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
JDCamAuthor Commented:
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
 
JDCamAuthor Commented:
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
 
BrandonGalderisiCommented:
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
 
JDCamAuthor Commented:
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
 
JDCamAuthor Commented:
I will ask for additional help with this suggestion under a new question. (and properly categorized)
thanks.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now