Solved

sql server stored procedure row_number help

Posted on 2008-06-19
3
217 Views
Last Modified: 2010-03-19
Hello experts,

I have the following stored procedure;-

SELECT TOP 15 * FROM
     (
          SELECT *,
          ROW_NUMBER() OVER (ORDER BY record_id) AS num
          FROM Records
     ) AS a
     WHERE num >= @start

But I'm needing to change the value 15 on  this line --> SELECT TOP 15 * FROM
to a variable, like so for instance;-

SELECT TOP @top * FROM

But I get errorMsg 102, Level 15, State 1, Procedure sp_GetRecord, Line 22
Incorrect syntax near '@top'.

Is this not possible or am I doing it wrong.

Many thanks,

Clara
0
Comment
Question by:claracruz
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 75 total points
Comment Utility
SELECT TOP (@n) * FROM
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
if you get that syntax error, then either
* you are connected to a sql 2000 server/database
* you are connected to a sql 2005 server, but the database is in sql 2000 (or lower) compatibility.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
anyhow, you could do this:
SELECT * FROM 
     (
          SELECT *,
          ROW_NUMBER() OVER (ORDER BY record_id) AS num
          FROM Records
     ) AS a
     WHERE num >= @start
       AND num < @start + @top

Open in new window

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

9 Experts available now in Live!

Get 1:1 Help Now