Start with IF statement in Subquery - sql

I need to select rows in a subquery with an IF condition:
DECLARE #VersionNo varchar(30)
SET #VersionNo = 'Hello'
SELECT (
CASE #VersionNo WHEN 'Hello'
THEN (SELECT '22','22')
ELSE (SELECT '25','22')
END)
But I am getting this error:
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'IF'.
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'Then'.
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'Else'.
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ')'.
Can anyone help me to resolve this?
Based on the answers, I have changed my query to:
DECLARE #VersionNo varchar(30)
SET #VersionNo = 'Hello'
SELECT (
CASE #VersionNo WHEN 'Hello'
THEN (SELECT '22','22')
ELSE (SELECT '25','22')
END)
Now I am getting this error:
Msg 116, Level 16, State 1, Line 4
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

Use CASE WHEN:
declare #VersionNo varchar(30);
Set #VersionNo = 'Hello';
Select CASE WHEN #VersionNo='Hello' Then 'Yes' Else 'No' END AS SomeCol;

You need to use CASE, you cannot use IF in a select query
select case #VersionNo when 'Hello'
then 'Yes'
else 'No'
end
Alternatively, you can do this to run different select queries based on the value of your variable:
IF #VersionNo = 'Hello'
SELECT 'Yes'
ELSE
SELECT 'No'

Check this one :
DECLARE #VersionNo VARCHAR(30)
SET #VersionNo = 'Hello'
SELECT CASE #VersionNo
WHEN 'Hello' THEN '22'
ELSE '25'
END ,
CASE #VersionNo
WHEN 'Hello' THEN '22'
ELSE '22'
END

Related

select into a table using a variable as table name dynamic sql not working

I'm trying to execute the code below (I know a lot of people have already asked this question but none of them worked for me). since I have a loop I need to select into multiple new tables with the value of variable name as tables names.
I tried using dynamic sql for the tables name like this:
WHILE ( #Counter <= #len)
begin
DECLARE #name VARCHAR(10),#sql nvarchar(max)
SET #name= (select product from #ProductType where NewPK= #Counter)
set #sql='
select m=DATEPART(month,t2.initial_date),y=DATEPART(year,t2.initial_date) ,t1.user_orders,t1.account_id
into '+#name+'
from (select dbo.sales_info.account_id, count(*) as user_orders
from dbo.sales_info
where dbo.sales_info.product= #name
group by dbo.sales_info.account_id) t1
left join
(select dbo.sales_info.account_id, initial_date= min( dbo.sales_info.start_date)
from dbo.sales_info group by dbo.sales_info.start_date,dbo.sales_info.account_id )t2
on t1.account_id=t2.account_id
order by m,y'
exec (#sql)
SET #Counter = #Counter + 1
end
but I keep getting this error :
Must declare the scalar variable "#name".
Msg 102, Level 15, State 1, Line 10
Incorrect syntax near 't2'.
Msg 137, Level 15, State 2, Line 6
Must declare the scalar variable "#name".
Msg 102, Level 15, State 1, Line 10
Incorrect syntax near 't2'.
The problem is this line of code:
where dbo.sales_info.product = #name
You can use parameters when you execute the code:
exec sp_executesql #sql,
N'#name varchar(10),
#name=#name;

Msg 102, Level 15, State 1, Line 3 Incorrect syntax near '='

I want to select from ReceiveNote if fromloc = 1 then print Factory else have to print Other
SELECT PurDate,
case ReceiveNote.FromLOC
when ReceiveNote.FromLOC = '1' THEN 'Factory'
when ReceiveNote.FromLOC = '2' THEN 'Other'
else ''
end as FromLOC FROM tbl1
You already specified field after CASE word. No need to specify it again.
SELECT PurDate,
CASE ReceiveNote.FromLOC
WHEN '1' THEN 'Factory'
WHEN '2' THEN 'Other'
ELSE ''
END AS FromLOC
FROM tbl1
And here is documentation on CASE for tsql
SELECT PurDate,
case when ReceiveNote.FromLOC = '1' THEN 'Factory'
when ReceiveNote.FromLOC = '2' THEN 'Other'
else ''
end as FromLOC FROM tbl1

Error in selecting

I am trying to do a select like this -
SELECT COUNT(*)
FROM
(
DECLARE #Emp varchar(100)
SET #Emp = 'Rick'
SELECT *
FROM [Temporary].[dbo].[Employee_Test]
WHERE [Emp_Name] = #Emp
) AS EMPS
I know that I can fix this by putting the DECLARE statements before outer select. But, due to some crappy design, I would prefer to be able to use the inner query as is and then count the number of rows in it. How do I do that ?
The error I get is -
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'DECLARE'.
Msg 102, Level 15, State 1, Line 9
Incorrect syntax near ')'
DECLARE #Emp varchar(100)
SET #Emp = 'Rick';
SELECT COUNT(*)
FROM [Temporary].[dbo].[Employee_Test]
WHERE [Emp_Name] = #Emp
You need to Declare The variable and SET its values before you pass it to the SELECT Statement. You cannot Declare a Variable Inside a Select Statement.
Should you put a semi-colon after the SET statement?
SET #EMP = 'Rick';

What's wrong this T-SQL statement?

Why is this throwing an error on the equal sign?
select IIF((SUBSTRING('A1234', 1, 1) = 'A'), TRUE, FALSE) as IsAustraliaUser
Error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '='.
IIF is a SQL Server 2012 feature, you'll need to use CASE
SELECT CASE SUBSTRING('A1234', 1, 1)
WHEN 'A' THEN 'TRUE'
ELSE 'FALSE'
END
You should replace IIF with CASE, also TRUE and FALSE don't exists in SQL Server, you can use VARCHAR or BIT
select CASE WHEN SUBSTRING('A1234', 1, 1) = 'A' THEN 'TRUE' ELSE 'FALSE' END as IsAustraliaUser
I don't think SQL supports this syntax of IIF (unless you're using 2012), replace with case.
SELECT CASE WHEN ( SUBSTRING('A1234', 1, 1) = 'A' ) THEN 'TRUE'
ELSE 'FALSE'
END AS IsAustraliaUser

SQL case when set variable

I am trying to set 2 declared variable with case when blocks
Here's my code:
DECLARE #like bit,#dislike bit
if exists
( select *
,#like = (CASE WHEN likeordislike = 1 THEN 'true' ELSE 'false' END)
,#dislike=(CASE WHEN likeordislike = 0 THEN 'true' ELSE 'false' END)
from likeordislike
)
But when I execute query throws errors:
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '='.
Everything is ok? Couldn't understand
if (select count(*) from likeordislike where user = #user and comment_id = #comment_id) = 0
begin
insert into likeordislike etc
end
else
update likeordislike etc
end