I need a stored procedure that takes the following parameters...
CREATE PROCEDURE id3_MultipartQuestion_Select
...and when it is called, the stored procedure executes the query below. The query has a "main question" subquery which is named Q0 and then has "sub question" queries named Q1 and A1, Q2 and A2, Q3 and A3. However, I need it to be dynamically written so that the number of sub questions will vary depending on the parameter passed. As you can see, the sub-queries all follow the same pattern, so hopefully this is not a difficult task.
To be clear, the query below is what should be executed if @Sub_Question_IDs='22,12,20'. If @Sub_Question_IDs='24,10,15,18' then there would be Q0, Q1 and A1, Q2 and A2, Q3 and A3, and two additional joins named Q4 and A4. And if @Sub_Question_IDs='24' then there would simply be Q0, Q1 and A1