The problem is as follows: I have some parameters
Declare @testParam nvarchar(max) = '',
Declare @ID int = 3
and I have many complex tables and all of them have a column that is named differently, but resembles some kind of status that is of type BIT
.
ID | Status | some other values... |
---|---|---|
1 | 0 | ... |
2 | 1 | ... |
3 | 1 | ... |
4 | 1 | ... |
I want to expend text of @testParam
every time the Status
value = 1 for the record matching my ID
. The text I need to add to this @testParam
will be different for every table.
As for example:
select @testParam = @testParam + 'Full capacity '
from CapacityTable
where CapacityStatus= 1 and ID = @ID
select @testParam = @testParam + 'Not enough coal '
from CoalTable
where LackOfCoal = 1 and ID = @ID
select @testParam = @testParam + 'Too low temperature'
from OvenTable
where OvenStatus = 1 and ID = @ID
select @testParam as Result
If all of the select statments meet the conditions, the result that I expect would look like:
Result |
---|
Full capacity Not enough coal Too low temperature |
I know I could achieve this by using many IF
statements, but I need this query to be as optimal as possible. Unless using IF
statements doesn't change much in case of execution time I'm looking for an alternate solution.