SQL Server - Update on IF condition - sql

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'

Related

If exists (select top 1 1) with multiple instructions

I have a stored procedure that checks for the existence of a record in a table and if it exists, updates the record, else, inserts a new record.
#myFieldID int = NULL,
#newTitle varchar(50) = NULL
IF EXISTS (SELECT TOP 1 1
FROM table
WHERE myField = #myFieldID)
UPDATE table1
SET title = #newTitle
WHERE myFieldID = #myFieldID
UPDATE table2
SET newTitle = #newTitle
WHERE myFieldID = #myFieldID
ELSE
INSERT INTO table1
(
title
)
SELECT #newTitle
The problem is, when I have the second UPDATE, I get an error "Incorrect syntax near the keyword 'ELSE'"
How do I update 2 tables within the same conditional "IF"? Or, do I simply have to test for the existence of the same record twice?
The issue is you're trying to multiple updates in a single IF statement with no BEGIN/END. Change your if statement to
IF EXISTS (SELECT TOP 1 1
FROM table
WHERE myField = #myFieldID)
BEGIN
UPDATE table1
SET title = #newTitle
WHERE myFieldID = #myFieldID
UPDATE table2
SET newTitle = #newTitle
WHERE myFieldID = #myFieldID
END
ELSE

input parameter used in case statement in where clause

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

IF ELSE in where block of sql query

I have the below stored procedure
CREATE PROCEDURE sp1
(
#param1 bit,
#parma2 bit
)
AS
BEGIN
SELECT * from Table1 where
if #param1 = 1 then column1 is null
else if #parma2 = 1 then column1 is not null
END
Obviously the If else part in the where clause is not correct . Could anyone suggest how to tackle it ?
Something like this:
SELECT * from Table1 where
case
when #param1 = 1 then [column 1] is null
when #param2 = 1 then [column 1] is not null
end
But you don't need two parameters for this procedure, only one should suffice.
Try the following:
SELECT
Case WHEN #param1 = 1 THEN Null
Case WHEN #param2 = 1 THEN column1 Else '' End as column1 ,
* FROM Table1
The above statement will set column1 to null if param1 = 1, it will get the value of column1 if param2 = 1 otherwise it will set it to empty string.
Seems to me like the simplest option would be to use AND and OR:
SELECT *
FROM Table1
WHERE (#param1 = 1 AND column1 is null)
OR (#parma2 = 1 AND column1 is not null)
Use this:
CASE WHEN condition THEN value ELSE value END

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

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