SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 dynamic TOP
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

magmo
Aged Yak Warrior

509 Posts

Posted - 04/08/2013 :  03:14:06  Show Profile  Reply with Quote
Hi

I have the following stored procedure query...


SELECT        TOP (@TopItems) PdfFileName, ID, IsFetched
FROM            dbo.Cards
WHERE        (IsFetched IS NULL OR
                         IsFetched = 0) AND (PdfFileName <> N'')
ORDER BY DateAdded


I would like to change this so that if I pass @TopItems = 0 then I should retrieve all rows (not using the TOP xx part), but if I pass any value higher than 0, the TOP part should be used, how can I change this?

bandi
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 04/08/2013 :  05:20:25  Show Profile  Reply with Quote
DECLARE @TopItems INT =0
IF ( @TopItems  > 0)
BEGIN 
	SELECT TOP (@TopItems) PdfFileName, ID, IsFetched
	FROM dbo.Cards
	WHERE (IsFetched IS NULL OR IsFetched = 0) AND (PdfFileName <> N'')
	ORDER BY DateAdded
END 
ELSE 
BEGIN
	SELECT PdfFileName, ID, IsFetched
	FROM dbo.Cards
	WHERE (IsFetched IS NULL OR IsFetched = 0) AND (PdfFileName <> N'')
	ORDER BY DateAdded
END 
Go to Top of Page

magmo
Aged Yak Warrior

509 Posts

Posted - 04/08/2013 :  05:38:10  Show Profile  Reply with Quote
Thank you very much, that works great!
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 04/08/2013 :  06:10:58  Show Profile  Reply with Quote
quote:
Originally posted by magmo

Thank you very much, that works great!


Welcome

--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000