Here is the problem.
I have a stored procedure that transforms normalized data into a standard dataset.
I need to report on some data which consists of a view, and two data sets from the stored procedure with different parameters being submitted to the stored procedure which returns different columns.
Example
If My view Contains something like:
ID Title Status Date
1 How To Party Like a rockstar Approved 7/15/1989
2 Too much of a good thing Approved 7/25/2001
3 Needs More Cowbell Denied 11/11/2011
4 Here Today Gone Tommorrow Approved 8/13/1969
5 The way She moves Approved 12/13/2011
And the procedure run with one paramater returns:
ID Do you like the ice Cream? How much would you pay for the ice cream?
1 Yes 2
2 Yes 5
3 Yes 7
4 No 2
5 No 3
And the same procedure run with a different parameter returns:
ID Total Number of Bell Peppers Total Number of Apples Total Number of Oranges
1 7 6 6
2 6 8 8
3 32 7 5
4 7 3 1
5 12 1 1
Assuming that the ID column is a key that can be used to join the data sets, how would i go about getting:
ID Title Status Date Do you like the ice Cream? How much would you pay for the ice cream? Total Number of Bell Peppers Total Number of Apples Total Number of Oranges
1 How To Party Like a rockstar Approved 7/15/1989 Yes 2 7 6 6
2 Too much of a good thing Approved 7/25/2001 Yes 5 6 8 8
3 Needs More Cowbell Denied 11/11/2011 Yes 7 32 7 5
4 Here Today Gone Tommorrow Approved 8/13/1969 No 2 7 3 1
5 The way She moves Approved 12/13/2011 No 3 12 1 1
Keeping in mind that the stored procedures cannot be done with inline sql due to the nature of the normalization and the transformation of the normalization, and I do not believe that they can be done as a table variable udf due to the fact that the stored procedure returns a variable number of columns depending on the parameter that is passed to it when it is run (if someone can prove me wrong on this and point me in the direction of a dynamic column table-valued udf i would be very appreciative)
If i can someway bring the view and the two stored procedures into one stored procedure and return the dataset in that fashion, that would be my ultimate goal.
I also need to mention that Ad Hoc Distributed Queries cannot be turned on on this server (not my decision)
Resolution
With a little help from Stuart Ainsworth's answer I was able to work it out. My initial problem with the whole thing was that the columns were dynamic for each of the crosstab tables. so as a part of that I had created a function that returned the columns as a comma deliminated string...
DECLARE
@PivotColumns VARCHAR(MAX)
SET @PivotColumns = dbo.fnGetFormPivotColumns(9)
Where the 9 in this case is the parameter which lets me know which item i'm building for. From there it was just a repeat of the functionality in the base sp to build the temp tables dynamically so I came up with this:
IF object_id('tempdb..#temp_DEP') IS NOT NULL
DROP TABLE #temp_DEP
CREATE TABLE #temp_DEP (APPLICATION_ID int)
EXEC ('ALTER TABLE #temp_DEP ADD ' + @PivotColumns)
INSERT INTO #temp_DEP
EXEC GetFormCrossTab 9
A simple SELECT * FROM #temp_DEP shows that I'm getting exactly what I want.
All I have to do is repeat the process for the other parameter and I get my two data sets within the same stored procedure.