See the small example below.
Declare @A nvarchar(100)
Declare @S nvarchar(100)
Declare @T nvarchar(10)
Select @T = '1 '
Select @A = ' = 2'
Select @S = 'if ' + @T + @A + ' begin Select * from Table where SomeField = '''xx''' end'
Select @S
EXECUTE sp_executeSQL @S
The principle behind this is to create a string which is your SQL statement out of the conditions you have and then execute that SQL statement. (hopefully the if statement is not too long)
See
sp_executesql (Transact-SQL)[
^]
When creating the SQL statement be careful of single quotes as these need to be added in as required. I have added an example of this as well.
e.g. 'A' in a select statement will end up being become '''A'''.
See
How to include a single quote in a sql query[
^]
What you should end up with is a SQL statement as a large nvarchar or string that can be executed.