In a stored procedure, let's say I have a flag, which can be either 0 or 1
If 1, then I want to select * from table A where name = 'blah'.
If 0, then I would want to select * from table A where name = blah and age = 13.
Is there a way I can add and age = 13 to a stored procedure query?
This is what I have currently.
IF #flag = 1
SELECT * from A where name = 'blah'
ELSE
SELECT * from A where name = 'blah' and age = 13
I would like to know for cases where query becomes really long, and so copying and pasting with few more likes for ELSE case is very inefficient.
Thanks.
SELECT * from A
where name = 'blah'
and (#flag = 1 or age = 13)
You can also use a Case statement;
Select * from A
Where Name = 'blah' And Age = Case #flag when 1 then Age else 13 end
Related
I have this query (something like a case statement which I can use and fix it)
select *
from mytable
where 1=1
and (isNull(ID, 0) = 0 OR UtilityID IN (9,40))
I also want to add another statement
select *
from mytable
where 1=1
and UtilityID NOT IN (9,40)
Everything is happening in a procedure, so want to use a variable like declare #something so if that is passed as 1, use the first statement and the if 0 is passed, use the latter one.
While I appreciate the genius in Dale's answer I find this more readable:
IF #something = 0
BEGIN
select *
from mytable
where ID IS NULL OR ID = 0 OR UtilityID IN (9,40);
END
IF #something = 1
BEGIN
select *
from mytable
where UtilityID NOT IN (9,40);
END
It's procedure code, so use IF to direct the control flow. Also expanded and simplified your where clauses
I think I understand your logic, ignoring the 1=1 (which does nothing) you want to only allow id = 0 when #something = 1. This should do it:
declare #something bit = 0;
declare #mytable table (ID int, UtilityID int);
insert into #mytable (ID, UtilityID)
select 0, 1 union all
select 1, 2 union all
select 2, 9 union all
select 3, 40;
select *
from #mytable
where (
(#something = 1 and (isnull(ID, 0) = 0 or UtilityID in (9,40)))
or (#something = 0 and (UtilityID not in (9,40)))
);
A more performant approach for a larger query could be:
select *
from #mytable
where (#something = 1 and (isnull(ID, 0) = 0 or UtilityID in (9,40)))
union all
select *
from #mytable
where (#something = 0 and (UtilityID not in (9,40)));
PS: Hopefully your ID cannot ever by null - it should have a constraint on it.
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
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)
I have a query as below in my Stored Procedure:
I set the #SUBCONDITION FROM MY CODE BEHIND.
declare #SUBCONDITION VARCHAR(100)= NULL
if(#SUBCONDITION='DEPT')
BEGIN
Select * from table1 where IDDEPT=1
END
ELSE IF (#SUBCONTION='GRADE')
BEGIN
SELECT * FROM TABLE1 WHERE IDGRADE=1
END
ELSE IF(#SUBCONDITION='SECTION')
BEGIN
SELECT * FROM TABLE1 WHERE IDSECTION=1
END
Everything works just fine.
My question is whether I can do this in just one query??? Like using if or case or something in the where clause?
Yes, you can do this in one query. It would look like this:
Select *
from table1
where (#SUBCONDITION = 'DEPT' and IDDEPT = 1) or
(#SUBCONTION = 'GRADE') and IDGRADE = 1) or
(#SUBCONDITION = 'SECTION' and IDSECTION = 1)
Try this:
SELECT *
FROM table1
WHERE 1 = (CASE #SUBCONDITION
WHEN 'DEPT' THEN IDDEPT
WHEN 'GRADE' THEN IDGRADE
WHEN 'SECTION' THEN IDSECTION
ELSE 0
END);
Try this:
Select * from table1
where #SUBCONDITION = 'DEPT' AND IDDEPT=1 OR
#SUBCONDITION = 'GRADE' AND IDGRADE=1
#SUBCONDITION = 'SECTION' AND IDSECTION=1
I want to run through two IF statements in sql. The IF's are two different conditions, example:
IF (#user == 'Bob')
BEGIN
SELECT * FROM table Where id = 1
END
IF (#animal == 'Cat')
BEGIN
SELECT * FROM table WHERE id = 50
END
What i want back are rows 1 if only the first condition is correct or 1 and 50 if both conditions are met.
This fails at the second IF statement, is there another keyword I need to add?
I recommend a single statement:
SELECT
*
FROM table
WHERE
(#user = 'Bob' AND Id = 1)
OR
(#animal= 'Cat' AND Id = 50)
IF (#user == 'Bob')
BEGIN
SELECT * FROM table Where id = 1
END
ELSE IF (#animal == 'Cat') and (#user == 'Bob')
BEGIN
SELECT * FROM table WHERE id = 50
END
IF (#user = 'Bob')
BEGIN
IF (#animal = 'Cat')
BEGIN
SELECT * FROM table WHERE id = 50
END
ELSE
BEGIN
SELECT * FROM table Where id = 1
END
END
I think this might work for what you want.
SELECT * FROM table Where id = 1 && #user == 'Bob'
Union
SELECT * FROM table WHERE id = 50 && #animal == 'Cat' && #user == 'Bob'
Try this nested if function:
SELECT * FROM table Where id = IF(#user=='Bob',1,IF(#animal=='Cat',50));