Solved

sql server stored procedure row_number help

Posted on 2008-06-19
3
262 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 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

690 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