I'm working on migrating an API to a new platform and this includes migrating the queries that were previously written directly into the code for functions in the database. One of the problems I've been facing is that some of the checks we made on the code are not available in SQL Server (or I do not know how to do them).
I need to put a condition of where
depending on the value of a variable. For example:
DECLARE @var AS INT = 10;
SELECT * FROM TB_SQL
WHERE col1 = 'StackOverflow'
IF(@VAR = 1) THEN
BEGIN
AND col2 = @var
END
ELSE
BEGIN
-- não coloca condição nenhuma
END
In this case, the condition AND col2 = @var
would only be included in the query if the value of @var equals 1.
Is there any way to do this?