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)
 conditional select

Author  Topic 

magmo
Aged Yak Warrior

558 Posts

Posted - 2013-01-15 : 09:59:15
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
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2013-01-15 : 10:17:29
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
Go to Top of Page

magmo
Aged Yak Warrior

558 Posts

Posted - 2013-01-15 : 10:33:32
Nice, Thanks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-15 : 12:24:12
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
   

- Advertisement -