Two sql if statements - sql

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));

Related

Conditionally modify query based on parameter

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.

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)

How to use case statement inside an SQL select Query

I need to get the output of a selected query depending on certain conditions
Means if(id=uid)
then I need the below query
select * from table1 where id=5;
else
I need the below one
select * from table1 where id=10
I know i can use if condition for this. But my query is very long one so when I use if else then it would look like
if(#id=#uid)
begin
select * from table1 where id=5;// query 1
end
else
select * from table1 where id=10;//query 2
but here I need to replace the entire query once again for a single check. I hope I can do something like this:
declare #id int=4;
declare #uid=10;
select * from table1 where
case
when #id=#uid
then
id=5
else
id=10;
end
Updation
I need one more condition too
in this case id=5 and uid=10
then if(id=uid)
then
select * from table1 where id=5
and
if(id!=uid)
then
select * from table1
something like this
You can use the case expression to return the value id should be equal to:
SELECT *
FROM table1
WHERE id = CASE WHEN #id = #uid THEN 5 ELSE 10 END;
EDIT:
The updated requirement in the question is to return all rows when #id != #uid. This can be done by comparing id to id:
SELECT *
FROM table1
WHERE id = CASE WHEN #id = #uid THEN 5 ELSE id END;
Alternatively, with this updated requirement, a simple or expression might be simpler to use:
SELECT *
FROM table1
WHERE #id = #uid OR id = 5;
SELECT * FROM table1
WHERE (id=5 AND #id=#uid) OR (id=10 AND #id<>#uid)
SELECT
*
FROM
table1
WHERE
(
#id = #uid
AND
id =5
)
OR
(
not #id = #uid
AND
id=10
)

Where and if clause in SQL query

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

Display all records of a table unless I pass a key

Is there an other way to get all rows of a table using a stored procedure unless I pass the procedure a key?
If so, how can I code that in SQL Server?
try this
CREATE PROCEDURE GetData(#key int = null)
BEGIN
SELECT * FROM Table WHERE (#Key Is NULL or id = #Key)
END
You could also use the COALESCE operator:
CREATE PROCEDURE dbo.uspMySprocName(#Key INT = NULL)
AS
BEGIN
SELECT * FROM MyTable WHERE ID = COALESCE(#Key, ID);
END
you can use a if condition and have 2 queries
**syntax:**
if (#key is null ) then
begin
select * from table1 ;
end
else
being
select * from tabel1 where field1 = #key ;
end
endif
The following link should help
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=146620