input parameter used in case statement in where clause - sql

I have the following simplified stored procedure where based on on the input parameter, I need to then do a case in the where clause. It will not execute as it says: Incorrect syntax near '='
PROCEDURE [dbo].[DataInfo]
#Allowactive BIT = 1
AS
BEGIN
Select * from tbl1 1
where (CASE #Allowactive
WHEN 0 then (t.Isactive = 1) END
AND isSubmitted = 1
END

your where clause will be like below
where
CASE #Allowactive
WHEN 0 then t.Isactive END =1
AND isSubmitted = 1

You shouldn't use parameters in a query like this, as it messes up the query plan. When the right plan to use changes depending on the parameter, you need separate queries, or to force SQL to always recompile.
So do this instead:
create or alter procedure [dbo].[DataInfo] #Allowactive bit = 1
as
begin
if #Allowactive = 0
begin
Select * from tbl1 1
where Isactive = 1
AND isSubmitted = 1
end
else
begin
select * from tbl1 1
where isSubmitted = 1
end
end
Instead run separate queries.

Try to run the following and see the results:
SELECT *
FROM Tbl1 AS T
WHERE CASE #Allowactive
WHEN 0 THEN 1 ELSE #Allowactive END = T.Isactive
AND
isSubmitted = 1;
If you have 2012+ version then you could also do:
SELECT *
FROM Tbl1 AS T
WHERE IIF(#Allowactive = 0, 1, #Allowactive) = T.Isactive;

It seems Zaynul Abadin Tuhin directly answers your question.
But, I believe a case statement complicates what you want to achieve.
I think a query like this satisfies your desired outcome:
PROCEDURE [dbo].[DataInfo]
#Allowactive BIT = 1
AS
BEGIN
SELECT * FROM tbl1 t
WHERE (#Allowactive = 1 OR (#Allowactive = 0 AND t.Isactive = 1))
AND t.isSubmitted = 1
END

Related

Range of values as ELSE clause in a CASE statement

I have a stored proc which is supposed to filter results based on an integer which can be NULL and I need to be able to choose results that either match the integer or retrieve all results but I can't figure out the syntax for the "All" results part. Query is basically this:
Select * from dbo.TABLE
WHERE [IDField] =
CASE
WHEN ISNULL(#ID, 0) > 0 THEN #ID
ELSE ?????
END
I want to use an IN range to include all the values because setting it to 0 or Null will return no results, but this doesn't work:
Select * from dbo.TABLE
WHERE [IDField] =
CASE
WHEN ISNULL(#ID, 0) > 0 THEN #ID
ELSE 1 OR 2 OR 3
END
or this:
Select * from dbo.TABLE
WHERE [IDField] IN (
CASE
WHEN ISNULL(#mID, 0) > 0 THEN #mID
ELSE 1,2,3
END
)
Phrase this without the case:
SELECT *
FROM dbo.TABLE
WHERE #id is null OR IDField = #id;
Then you can expand this to:
SELECT *
FROM dbo.TABLE
WHERE (COALESCE(#ID, 0) > 0 AND IDField = #id) OR
IDField IN (1, 2, 3)

Choose multiple cases condition

I want to get multipule choises after then in case statment as
#value
select * from [dbo].[Currency_Tbl]
WHERE [Currency_Active_YN]=
CASE WHEN #value = 1 THEN
( 1 or 0)
ELSE
#Value = 0 then 0
END
it didn't accept the first line in col1 but accept the col2
how can I select multiple numbers after THEN?
You don't use case in where clauses. Use boolean logic
select * from [dbo].[Currency_Tbl]
WHERE (#value = 1 and [Currency_Active_YN] in (0,1))
OR (#value = 0 and [Currency_Active_YN] = 0)
You dont need a case to do what you're trying to do. Assuming Currency_Active_YN is a not null bit field the following logic should suffice.
select * from [dbo].[Currency_Tbl]
WHERE (#value=1 OR [Currency_Active_YN]=#Value)

Can i have multiple select but only return one result set

If I have multiple selects like so:
select * from A where A.name = 'linköping'
IF ##ROWCOUNT = 0
begin
select * from A where A.amount = 45
end
...I get 1 result set if the first select returns stuff. But if it runs the second, I get two result sets; the first with no rows and the second with some rows.
Is there a way to only return the second result set if the second select is run ?
I write code like this because of Andrey Gordeev's answer to this post: Can you have if-then-else logic in SQL?
(MSSQL 2000)
Thanks!
You will need to prevent the first select by checking if you will get any results back before running the select.
For example:
IF EXISTS (SELECT 1 FROM A WHERE A.name = 'linköping')
BEGIN
SELECT * FROM A WHERE A.name = 'linköping'
END
ELSE
BEGIN
SELECT * FROM A WHERE A.amount = 45
END
No it is not possible The returning results are depending on number of select statements.
If you want to do the single select follow #RB or #Jwalin Shah solution.
This will work with IF-ELSE Logic. [Tested]
DECLARE #count as int
set #count = (SELECT count(1) from A where A.name = 'linköping')
if (#count = 0)
BEGIN
select * from A where A.amount = 45
END
ELSE
BEGIN
select * from A where A.name = 'linköping'
END
Try this
IF ##ROWCOUNT = 0
BEGIN
select * from A where A.amount = 45
END
ELSE
BEGIN
select * from A where A.name = 'linköping'
END

How do I use an if statement inside the where clause in this sql statement?

select *
from mytable
where (if #key=0
pkey>=0
else
pkey = #key)
#key is the value passed in to the stored procedure and pkey is a column in mytable.
How about this:
select *
from mytable
where ((#key=0 AND pkey>=0) OR (#key<>0 AND pkey = #key))
You use CASE (bit like the way you are trying with if) as below. (DEMO)
select *
from mytable
where pkey = case when #key <> 0 then #key
else Abs(pkey) end
This will work for you . by using case statement you can apply dynamic filter ..
select *
from mytable
where 1 = case
when #key=0 then case when pkey>=0 then 1 else 0 end
else case when pkey = #key then 1 else 0 end
end

sub query with parameter?

I'd like to ask help on this small query of mine. Im doing a query and a sub query on my sub query i want to have it parameterized. Is there a way to do it?
Please see my query script.
select sum(issue) as [Issue], sum(nonissue) as [NonIssue]
from
(
AS
select
case when isissue = 1 then 1 else 0 end as 'issue',
case when isissue = 0 then 1 else 0 end as 'nonissue',
LastTicketStatusID
from
vw_Tickets
where
LastTicketStatusID = #LastTicketStatusID
)
as Tickets
I always got an error Must declare the table variable "#LastTicketStatusID". where should i declare the parameter?
Thanks,
Nhoyti
If this is for a stored procedure...
CREATE PROCEDURE [dbo].[ProcedureName]
#LastTicketStatusID INT
AS
select
sum(issue) as [Issue],
sum(nonissue) as [NonIssue]
from (
select
case when isissue = 1
then 1 else 0 end as 'issue',
case when isissue = 0
then 1 else 0 end as 'nonissue',
LastTicketStatusID
from vw_Tickets
where LastTicketStatusID = #LastTicketStatusID ) as Tickets
Otherwise
DECLARE #LastTicketStatusID INT
SELECT #LastTicketStatusID = yourDesiredID
Not pertinant to your question, but assuming that vw_Tickets.isissue is a bit field (or otherwise constrained to values of zero or one.) the inline query can be removed to simplify Launchy's answer:
select sum(isissue) as [Issue],
sum(1 - isissue) as [NonIssue]
from vw_Tickets
where LastTicketStatusID = #LastTicketStatusID
at the very top of the query
Declare #LastTicketStatusID int
set #lastTicketStatusID = ####