Solved

sql server stored procedure row_number help

Posted on 2008-06-19
3
250 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
ID: 21821163
SELECT TOP (@n) * FROM
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21821243
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 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21821249
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 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