Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sql server stored procedure row_number help

Posted on 2008-06-19
3
Medium Priority
?
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 300 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

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…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 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