[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Select Top (Variable) SQL 2000 vs SQL 2005

Posted on 2007-11-19
4
Medium Priority
?
2,476 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

650 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