Reputation: 403
I am trying to use a temp table inside of a dynamic pivot query. I have read that the procedure will not work because the temp table is outside of the scope even though it is a global table. This is strange to me because it works in sql fiddle but not in SQL Server 2012. How can I edit my query so that it gives me an output other than (xxx row(s) affected)?
Here is my query:
SELECT cc.CaseCaseId AS CaseId, cc.Label AS CaseName, rce.EcoDate, cc.OperatorName, cc.State, cc.County, ei.IROR, rce.NDCash
into ##Temp2
FROM PhdRpt.ReportCaseList_542 AS rcl INNER JOIN
CaseCases AS cc ON rcl.CaseCaseId = cc.CaseCaseId INNER JOIN
PhdRpt.RptCaseEco_542 AS rce ON rcl.ReportRunCaseId = rce.ReportRunCaseId INNER JOIN
PhdRpt.EcoIndicators_542 AS ei ON rcl.ReportRunCaseId = ei.ReportRunCaseId
DECLARE @colsPivot AS NVARCHAR(MAX),
@colsUnpivot as NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @colsPivot = STUFF((SELECT distinct ',' + QUOTENAME(EcoDate)
from ##Temp2
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
select @colsUnpivot = stuff((select ','+quotename(C.name)
from sys.columns as C
where C.object_id = object_id('##Temp2') and
C.name LIKE 'NDCash%'
for xml path('')), 1, 1, '')
set @query
= 'select *
from
(
select cc.CaseCaseId AS CaseId, cc.Label AS CaseName, rce.EcoDate, cc.OperatorName, cc.State, cc.County, ei.IROR, val, col
FROM ##Temp2
unpivot
(
val
for col in ('+ @colsunpivot +')
) u
) x1
pivot
(
max(val)
for EcoDate in ('+ @colspivot +')
) p'
exec(@query)
Drop table ##Temp2
Here is the table structure:
CaseId EcoDate NDCash
2 2003-01-01 26.6384943638238
2 2004-01-01 23.9534867373416
2 2005-01-01 25.9197356158675
2 2006-01-01 25.3280437702064
2 2007-01-01 24.9828609298022
2 2008-01-01 26.4019321789285
2 2009-01-01 26.6384943638238
2 2010-01-01 24.0660715481002
2 2011-01-01 26.6384943638238
2 2012-01-01 22.5718444448345
2 2013-01-01 26.6384943638238
Upvotes: 1
Views: 3237
Reputation: 247850
Unless I am missing something, you don't need to use UNPIVOT
for this and you don't need to use a temp table, just query the data directly. It looks like your current query is only unpivoting one column which is not needed. You use unpivot to convert multiple columns into multiple rows.
You should be able to use:
DECLARE @colsPivot AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @colsPivot = STUFF((SELECT ',' + QUOTENAME(rce.EcoDate)
from PhdRpt.RptCaseEco_542 AS rce
group by rce.EcoDate
order by rce.EcoDate
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query
= 'select *
from
(
SELECT cc.CaseCaseId AS CaseId,
cc.Label AS CaseName,
rce.EcoDate,
cc.OperatorName,
cc.State,
cc.County,
ei.IROR,
rce.NDCash
FROM PhdRpt.ReportCaseList_542 AS rcl
INNER JOIN CaseCases AS cc
ON rcl.CaseCaseId = cc.CaseCaseId
INNER JOIN PhdRpt.RptCaseEco_542 AS rce
ON rcl.ReportRunCaseId = rce.ReportRunCaseId
INNER JOIN PhdRpt.EcoIndicators_542 AS ei
ON rcl.ReportRunCaseId = ei.ReportRunCaseId
) x1
pivot
(
max(NDCash)
for EcoDate in ('+ @colspivot +')
) p'
exec(@query)
Upvotes: 2
Reputation: 721
change the exec(@query) to sp_executeSQl @query.
exec(@query) will not execute in the current context, but where as the sp_executeSQL will execute with the current context that includes the temporary tables as well.
Upvotes: 0