You mean something like:SELECTIF @PARAM = 1 THEN Col1, Col2, Col3ELSE IF @PARAM = 2 THEN Col1, Col4, Col5, Col6END IF
then the answer is no
You could have two different queries, and execute one or other depending on a parameter.You could select the PK from a master query into a Temp Table, and then have similar condition logic to above to JOIN that Temp Table and return different resultsets.But you can't conditionally have different numbers of columns.You could always return the same number of columns, but return some of them NULL (depending on your parameters) - which would reduce the amount of data returned.However, all these routes are not going to be very efficient as the query plan will be "vague".We tend to doCREATE PROCEDURE Sproc1@SomeParam intASCREATE #TempTable( MyPK int)EXEC FindTheDataSProc @SomeParam = @SomeParamSELECT Col1, Col2, Cool3FROM MyTable AS M JOIN #TempTable AS T ON T.MyPK = M.MyPK
and then create another procedure SProc2 that calls the same FindTheDataSProc procedure, but has a different SELECT statement thereafter.That way we encapsulate the "data finder" logic in one SProc, and thus only one place to fix any bugs etc., but then have different Sprocs that return the data, and each will have its own, optimised, query plan