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)
 conditional select
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

magmo
Aged Yak Warrior

530 Posts

Posted - 01/15/2013 :  09:59:15  Show Profile  Reply with Quote
Hi

I have a stored procedure that I want to pass in a parameter that would enable me to retrieve different result from a query.

This is the query..


  SELECT     COUNT(ID) AS Qty
  FROM         dbo.tbl_Cards WHERE (IsFetched = 0)





If I pass @CheckMe Int = 1 then the query should be ..


  SELECT     COUNT(ID) AS Qty
  FROM         dbo.tbl_Cards WHERE (IsFetched = 0) AND (Duplicate = 0)




If the @CheckMe is different from 1 the query should be..



  SELECT     COUNT(ID) AS Qty
  FROM         dbo.tbl_Cards WHERE (IsFetched = 0)




What do I need to do to get this to work?

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 01/15/2013 :  10:17:29  Show Profile  Reply with Quote
SELECT COUNT(ID) AS Qty
FROM dbo.tbl_Cards
WHERE (@checkMe = 1 and IsFetched = 0 AND Duplicate = 0)
OR
(@checkMe = 0 AND IsFetched = 0)

Jim








Everyday I learn something that somebody else already knew

Edited by - jimf on 01/15/2013 10:18:24
Go to Top of Page

magmo
Aged Yak Warrior

530 Posts

Posted - 01/15/2013 :  10:33:32  Show Profile  Reply with Quote
Nice, Thanks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/15/2013 :  12:24:12  Show Profile  Reply with Quote
this is enough after simplifying redundant conditions

SELECT COUNT(ID) AS Qty
FROM dbo.tbl_Cards 
WHERE IsFetched = 0
AND (Duplicate = 0 OR @CheckMe <> 1)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

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.04 seconds. Powered By: Snitz Forums 2000