Let’s say I have a table with five columns. Full select statement would be :
Select col1, col2, col3, col4, col5
from tbl
But I need this query to be “dynamic”, every column must be possible to enable/disable. So, as far as I understand I need five tags (bool let’s say).
If i have tag1=0, tag2=1, tag3=1, tag4=0, tag5 =0 then the select statement should be like this:
Select col2, col3
from tbl
If i have tag1=1, tag2=0, tag3=0, tag4=0, tag5 =0 then the select statement should be like this:
Select col1
from tbl
So is there a possibility to do so in TSQL? I intend to create SP and execute it from php.
P.s. I understand that there’s a solution to make a bunch of IF statements with all possible tag1...tag5 variations, but this is not very effective when the number of columns (and enable tags) is high.
1 Answer