Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 error with SP

Author  Topic 

tehprince
Starting Member

9 Posts

Posted - 2008-02-19 : 16:35:22
Here is my SP:

ALTER PROCEDURE [dbo].[spGetNews]
@NewsItems int = 3
AS

SELECT TOP @NewsItems FeedTitle, FeedURL
FROM NewsFeed
ORDER BY FeedDate DESC

I get an error however from the way I'm using @NewsItems in the select statement. Basically I need the ability to pass in the # of items to pull. So the default would be

SELECT TOP 3 FeedTitle, FeedURL

But I would like to change the 3 to a parameter that could change.

Any help would be most appreciated.

tprupsis
Yak Posting Veteran

88 Posts

Posted - 2008-02-19 : 16:55:19
I found this which seems to provide the answer:

http://sqlserver2000.databases.aspfaq.com/how-do-i-use-a-variable-in-a-top-clause-in-sql-server.html

Basically, you just put variable in parentheses.

SELECT TOP (@NewsItems) FeedTitle, FeedURL
FROM NewsFeed
ORDER BY FeedDate DESC

Hope that helps!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-19 : 23:55:06
quote:
Originally posted by tehprince

Here is my SP:

ALTER PROCEDURE [dbo].[spGetNews]
@NewsItems int = 3
AS

SELECT TOP (@NewsItems) FeedTitle, FeedURL
FROM NewsFeed
ORDER BY FeedDate DESC

I get an error however from the way I'm using @NewsItems in the select statement. Basically I need the ability to pass in the # of items to pull. So the default would be

SELECT TOP 3 FeedTitle, FeedURL

But I would like to change the 3 to a parameter that could change.

Any help would be most appreciated.


just put a braces around parameter
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-20 : 00:44:12
Other method is

SET ROWCOUNT @NewsItems

SELECT FeedTitle, FeedURL
FROM NewsFeed
ORDER BY FeedDate DESC

SET ROWCOUNT 0

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -