?
Solved

Select Top (Variable) SQL 2000 vs SQL 2005

Posted on 2007-11-19
4
Medium Priority
?
2,473 Views
Last Modified: 2010-04-21
The stored proc below works on an SQL 2005 db, but doesn't work on SQL 2000.
It errors out on the SELECT line 14. I've tried changing the parens to brackets but that didn't work either.
This is the error:

Msg 170, Level 15, State 1, Procedure sp_SelectRandomEmployees, Line 14
Line 14: Incorrect syntax near '('.
USE [HR]
GO
/****** Object:  StoredProcedure [dbo].[sp_SelectRandomEmployees]    
Script Date: 11/19/2007 07:58:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_SelectRandomEmployees] 
@RndNum INT
 
AS
BEGIN
 
SET NOCOUNT ON;
 
SELECT TOP (@RndNum) File#, LastName, FirstName, SiteID, Department
FROM Associates 
ORDER By NEWID()
END

Open in new window

0
Comment
Question by:Carla Romere
[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
4 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 total points
ID: 20313746
Hello Hers2keep,

The 'TOP (@Variable) ' wont work in sql 2000


ALTER PROCEDURE [dbo].[sp_SelectRandomEmployees]
@RndNum INT
 
AS
BEGIN
 
SET NOCOUNT ON;
SET ROWCOUNT  @RndNum
 
SELECT File#, LastName, FirstName, SiteID, Department
FROM Associates
ORDER By NEWID()

SET ROWCOUNT 0
END






Aneesh R
0
 

Author Closing Comment

by:Carla Romere
ID: 31409968
Absolutely perfect!!
0
 

Expert Comment

by:vikkyv
ID: 20352334
Hi,
I have been looking for this solution.  Thanks!  But I am looking at the performance.  I am not sure if the Stored Procedure gets recompiled every time, when we use SET statement.  Could anyone throw some light on this aspect?
Vivek
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 20354895
this sp wont recompile every time,
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

771 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