I need SQL to check if there is any value equal to 1, 2 or 3 in the '@variavel' sent; If it is green enter the value 1, otherwise enter the value 0.
Note: the table and column can be given as example as fictitious.
I need SQL to check if there is any value equal to 1, 2 or 3 in the '@variavel' sent; If it is green enter the value 1, otherwise enter the value 0.
Note: the table and column can be given as example as fictitious.
Basically this:
INSERT INTO tabela SET coluna = IF( @variavel IN (1,2,3), 1, 0 );
The IN
returns true if the variable is in the list
The IF
returns the second value if the first expression is true, otherwise it returns the third.
See working in SQL Fiddle .
Notes:
In T-SQL from 2012, there is the IIF
function, which works the same as% MySQL% / MariaDB
For other DBs you have IF
, you can see more in detail in this post:
Using SQL Ansi, which works across all databases, you can do something like:
insert into dados (coluna1) values (
case @parametro
when 1 then 1
when 2 then 1
when 3 then 1
else 0
end)