Basically, I’m wondering if it’s possible to take this
IF @Value = 1
SELECT col1, col2 FROM MyTable
ELSE
SELECT col2 FROM MyTable
And do it in a single statement? Something like
SELECT IF(@Value = 1 col1), col2 FROM MyTable
I’ve tried this
SELECT case @Value when 1 then col1 end, col2 FROM MyTable
It always returns 2 columns, but the first will be null when @Value is not 1. I’d like it to return 2 columns only if @Value is 1, and one column the rest of the time.
With dynamic SQL, yes.
Without dynamic SQL you can’t really change the shape of a resultset.