SQL case when set variable - sql

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

Related

SQL Server - Update on IF condition

I can't believe I wasn't able to find the solution in other posts, but here goes..
I am trying to update different tables depending on a set variable, and I'd like the structure to be this way:
IF #etape = 1
(UPDATE table1 SET column1 = 1)
IF #etape = 2
(UPDATE table2 SET column1 = 1)
ELSE
(SELECT 'Wrong choice')
Which returns:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'update'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near ')'.
I have tried using CASE, with the same results:
CASE WHEN #etape = 1 THEN
(UPDATE table1 SET column1 = 1)
WHEN #etape = 2 THEN
(UPDATE table2 SET column1 = 1)
ELSE
(SELECT 'Wrong choice')
END
Although the documentation doesn't mention this, it seems like only SELECT statements are allowed after IF or CASE.
Would appreciate some help here.
Just remove the parenthesis...
Declare #etape int = 1
IF #etape = 1 UPDATE table1 SET column1 = 1
IF #etape = 2 UPDATE table2 SET column1 = 1
ELSE SELECT 'Wrong choice'
, or wrap your updates in BEGIN...END blocks.
In the case of updating the table based on parameters, you should use SQL stored procedure which allows you to pass the value as a parameter which is the following.
create procedure spUpdateTable
#etape int
as begin
if #etape = 1
begin
UPDATE table1 SET column1 = 1
end
else if #etape = 2
begin
UPDATE table2 SET column1 = 1
end
else
begin
print 'Wrong Choice'
end
end
Now to run your procedure simply use do like following
spUpdateTable 1 or 2
Although I see another answer has fixed your if syntax, have you considered just using the where clause?
UPDATE table1 SET column1 = 1 WHERE #etape = 1;
UPDATE table2 SET column1 = 1 WHERE #etape = 2;
IF #etape <> 2
SELECT 'Wrong choice'

Incorrect syntax near '='

DECLARE #COUNT_P INT
DECLARE #COUNT_S INT
SELECT MEMBNO,
CASE
WHEN TYPCOD = 'P' THEN #COUNT_P = #COUNT_P + 1
END AS C_P,
CASE
WHEN TYPCOD = 'S' THEN #COUNT_S = #COUNT_S + 1
END AS C_S
FROM #TEMP2
GROUP BY TYPCOD
The error that I am getting is
Msg 102, Level 15, State 1, Line 8
Incorrect syntax near '='.
SELECT MEMBNO,
SUM(CASE
WHEN TYPCOD = 'P' THEN 1 ELSE 0 END) AS C_P,
SUM(CASE
WHEN TYPCOD = 'S' THEN 1 ELSE 0 END) AS C_S
FROM #TEMP2
GROUP BY MEMBNO
You cannot increment values like that in SQL Server. A SELECT either returns values or sets variables, but not both.
DECLARE #COUNT_P INT;
DECLARE #COUNT_S INT;
If you want to set the total number for the two types, then:
select #count_p = sum(case when typecode = 'P' then 1 else 0 end),
#count_s = sum(case when typecode = 'S' then 1 else 0 end)
from #TEMP2;

sql server - setting permissions in trigger

I have a part of a trigger like so -
DECLARE #isInsert TINYINT
SET #isInsert = (CASE #actionType WHEN 'I' THEN 1 ELSE 0 END)
SELECT
(CASE #isInsert WHEN 1 THEN i.groupId ELSE d.groupId END) AS groupId
INTO #tmpRecordPermissionsToCheck
FROM inserted i
FULL JOIN deleted d
ON i.userId = d.userId
AND
i.groupId = d.groupId
-- Stop everything if the user is attempting to edit something they're not entitled to...
-- special case(s): refer above for additional tblServer-specific checks required here
DECLARE #errMsg VARCHAR(255)
SELECT #errMsg = 'You do not have permission to edit permissions for group ' + IsNULL(ug.shortName, '')
FROM #tmpRecordPermissionsToCheck tmp
LEFT JOIN tblUserGroups ug
ON ug.groupId = tmp.groupId
WHERE dbo.hasAdministrativePermissionsForGroup(tmp.groupId, dbo.getCurrentUser()) = 0
IF (#errMsg IS NOT NULL)
BEGIN
RAISERROR ( #errMsg, 16, 1 )
ROLLBACK TRANSACTION
RETURN
END
I'm calling a separate function that returns a 0 or 1 bit value.
If I do select dbo.isGlobalAdministrator(dbo.getCurrentUser()) I get a 1.
How do I structure the above code so that the IF (#errMsg IS NOT NULL) can be overridden if dbo.isGlobalAdministrator(dbo.getCurrentUser()) = 1 ?
How do I structure the above code so that the IF (#errMsg IS NOT NULL) can be overridden if dbo.isGlobalAdministrator(dbo.getCurrentUser()) = 1 ?
When you say overridden,i think you want to bypass errormessage
so just add this above error message
if ( dbo.isGlobalAdministrator(dbo.getCurrentUser()) = 1)
return

Start with IF statement in Subquery

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

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