2016-10-03 88 views
0

我正在使用存儲過程創建SSRS報告。存儲過程可以動態旋轉幾次。中間表的模式是動態的,因此我使用臨時表。由於該過程使用動態旋轉,因此我也少用動態SQL;因此不能在我的腳本中使用本地臨時表。存儲過程運行正常;但是當同一個存儲過程嵌入到SSRS查詢設計器中時,它會給出全局臨時表已經存在的錯誤。更不用說,我將它們全部放到代碼中。SQL Server 2012 SSRS:使用全局臨時表創建報告

請找代碼:

USE [IGD_HISTORY_COMBINED] 
GO 
/****** Object: StoredProcedure [dbo].[USP_GETIGDDETAILS] Script Date: 03-10-2016 12:05:46 ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
ALTER PROCEDURE [dbo].[USP_GETIGDDETAILS] @ID CHAR(17) ,@YEAR INT=null 
AS 
BEGIN 




--declare @ID CHAR(17) 
--set @id='1208700418b8b9c44' 



SELECT id, floodzone, FloodzoneLastUpdatedDate, versionid INTO ##HIST 
FROM IGD_HISTORY_MONROE 
WHERE [email protected] and floodzone is not null and RecordStatus='U' 
--and VersionId BETWEEN 587 AND 748 
ORDER BY VERSIONID desc 

Select *, 'VERSION_'+ cast(ROW_NUMBER() over (order by VERSIONID) AS varchar) 
as New_VERSION into ##temp from ##HIST ORDER BY VERSIONID ASC 


--Dynamic Pivoting in order to put the Id and versionid values relational 

DECLARE @DynamicPivotQuery AS NVARCHAR(MAX) 
DECLARE @ColumnName AS NVARCHAR(MAX) 

--Get distinct values of the PIVOT Column 
SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
+ QUOTENAME(New_VERSION) 
FROM (SELECT DISTINCT New_VERSION FROM ##temp) AS Courses 



--Prepare the PIVOT query using the dynamic 
SET @DynamicPivotQuery = 
N'select * into ##Final from (SELECT ID, FLOODZONE, New_Version' +' 
FROM ##Temp)SRC 
PIVOT(MAX(FLOODZONE) 
FOR New_vERSION IN (' + @ColumnName + ')) AS PVTTable' 


EXEC (@DynamicPivotQuery) 


select a.*,b.ParcelIDActual, b.PropertyLocationStreet1,b.PropertyLocationStreet2, 
b.PropertyCity,b.PropertyZip, B.CountyName 
INTO ##CE 
from ##Final a 
join IGD_DEV_2012..igd b 
on a.id=b.id; 

select * from ##CE; 


WITH CTE_New as (
select 
case when right (C.SourceFileName,8) like '[0-9]%' 
Then C.SourceFileName 
When right (C.SourceFileName,8) not like '[0-9]%' 
Then c.SourceFileName + '_'+ replace(cast(cast(C.CompletedTime as date) as varchar),'-','') 
end as SourceFileName, 
c.SourceVersion, 
'Version_'+SourceVersion as Version1 
from 
(select distinct versionid from ##temp) D 
join igd_datasourcestatus C 
on d.versionid=c.SourceVersion 
) 
select VERSION1 +': '+upper(SourceFileName) as Version_information into ##version_info from CTE_New; 





DECLARE @DynamicPivotQuery1 AS NVARCHAR(MAX) 
DECLARE @ColumnName1 AS NVARCHAR(MAX); 


SELECT Version_information , 'VERSION'+ CAST(ROW_NUMBER() OVER (ORDER BY Version_information) AS varchar) vERSIONS 
INTO ##TE 
FROM ##version_info 


--Get distinct values of the PIVOT Column 
SELECT @ColumnName1= ISNULL(@ColumnName1 + ',','') 
+ QUOTENAME(VERSIONS) 
FROM (SELECT DISTINCT VERSIONS FROM ##TE) AS Courses 

--Prepare the PIVOT query using the dynamic 
SET @DynamicPivotQuery1 = 
N'select * into ##Final1 from (SELECT Version_information, versions' +' 
FROM ##TE)SRC 
PIVOT(MAX(version_information) 
FOR versions IN (' + @ColumnName1 + ')) AS PVTTable' 


EXEC sp_executesql @DynamicPivotQuery1 

truncate table new1 


SELECT A.*,B.* 
into ##TEMPO 
FROM ##CE A 
JOIN ##FINAL1 B 
ON A.ID = @ID 


if exists (select max(versions) from ##te having max(versions)='version7') 
begin 
insert new1 ([id] 
     ,[version1] 
     ,[version2] 
     ,[version3] 
     ,[version4] 
     ,[version5] 
     ,[version6] 
     ,[version7] 
     ,[ParcelIDActual] 
     ,[PropertyLocationStreet1] 
     ,[PropertyLocationStreet2] 
     ,[PropertyCity] 
     ,[PropertyZip] 
     ,[CountyName] 
     ,[VERSION_1] 
     ,[VERSION_2] 
     ,[VERSION_3] 
     ,[VERSION_4] 
     ,[Version_5] 
     ,[version_6] 
     ,[version_7]) 
select [id] 
     ,[version1] 
     ,[version2] 
     ,[version3] 
     ,[version4] 
     ,[version5] 
     ,[version6] 
     ,[version7] 
     ,[ParcelIDActual] 
     ,[PropertyLocationStreet1] 
     ,[PropertyLocationStreet2] 
     ,[PropertyCity] 
     ,[PropertyZip] 
     ,[CountyName] 
     ,[VERSION_1] 
     ,[VERSION_2] 
     ,[VERSION_3] 
     ,[VERSION_4] 
     ,[Version_5] 
     ,[version_6] 
     ,[version_7] 
     from ##TEMPO 
end 

if exists (select max(versions) from ##te having max(versions)='version6') 
Begin 
    insert new1 ([id] 
     ,[version1] 
     ,[version2] 
     ,[version3] 
     ,[version4] 
     ,[version5] 
     ,[version6] 

     ,[ParcelIDActual] 
     ,[PropertyLocationStreet1] 
     ,[PropertyLocationStreet2] 
     ,[PropertyCity] 
     ,[PropertyZip] 
     ,[CountyName] 
     ,[VERSION_1] 
     ,[VERSION_2] 
     ,[VERSION_3] 
     ,[VERSION_4] 
     ,[Version_5] 
     ,[version_6] 
    ) 
select [id] 
     ,[version1] 
     ,[version2] 
     ,[version3] 
     ,[version4] 
     ,[version5] 
     ,[version6] 

     ,[ParcelIDActual] 
     ,[PropertyLocationStreet1] 
     ,[PropertyLocationStreet2] 
     ,[PropertyCity] 
     ,[PropertyZip] 
     ,[CountyName] 
     ,[VERSION_1] 
     ,[VERSION_2] 
     ,[VERSION_3] 
     ,[VERSION_4] 
     ,[Version_5] 
     ,[version_6] 

     from ##TEMPO 
end 

if exists (select max(versions) from ##te having max(versions)='version5') 
Begin 
    insert new1 ([id] 
     ,[version1] 
     ,[version2] 
     ,[version3] 
     ,[version4] 
     ,[version5] 

     ,[ParcelIDActual] 
     ,[PropertyLocationStreet1] 
     ,[PropertyLocationStreet2] 
     ,[PropertyCity] 
     ,[PropertyZip] 
     ,[CountyName] 
     ,[VERSION_1] 
     ,[VERSION_2] 
     ,[VERSION_3] 
     ,[VERSION_4] 
     ,[Version_5] 
    ) 
select [id] 
     ,[version1] 
     ,[version2] 
     ,[version3] 
     ,[version4] 
     ,[version5] 

     ,[ParcelIDActual] 
     ,[PropertyLocationStreet1] 
     ,[PropertyLocationStreet2] 
     ,[PropertyCity] 
     ,[PropertyZip] 
     ,[CountyName] 
     ,[VERSION_1] 
     ,[VERSION_2] 
     ,[VERSION_3] 
     ,[VERSION_4] 
     ,[Version_5] 

     from ##TEMPO 
end 

if exists (select max(versions) from ##te having max(versions)='version4') 
Begin 
    insert new1 ([id] 
     ,[version1] 
     ,[version2] 
     ,[version3] 
     ,[version4] 

     ,[ParcelIDActual] 
     ,[PropertyLocationStreet1] 
     ,[PropertyLocationStreet2] 
     ,[PropertyCity] 
     ,[PropertyZip] 
     ,[CountyName] 
     ,[VERSION_1] 
     ,[VERSION_2] 
     ,[VERSION_3] 
     ,[VERSION_4] 

    ) 
select [id] 
     ,[version1] 
     ,[version2] 
     ,[version3], 
     [version4] 
    ,[ParcelIDActual] 
     ,[PropertyLocationStreet1] 
     ,[PropertyLocationStreet2] 
     ,[PropertyCity] 
     ,[PropertyZip] 
     ,[CountyName] 
     ,[VERSION_1] 
     ,[VERSION_2] 
     ,[VERSION_3], 
     [version_4] 
    from ##TEMPO 

end 


if exists (select max(versions) from ##te having max(versions)='version3') 
Begin 
    insert new1 ([id] 
     ,[version1] 
     ,[version2] 
     ,[version3] 


     ,[ParcelIDActual] 
     ,[PropertyLocationStreet1] 
     ,[PropertyLocationStreet2] 
     ,[PropertyCity] 
     ,[PropertyZip] 
     ,[CountyName] 
     ,[VERSION_1] 
     ,[VERSION_2] 
     ,[VERSION_3] 


    ) 
select [id] 
     ,[version1] 
     ,[version2] 
     ,[version3] 

     ,[ParcelIDActual] 
     ,[PropertyLocationStreet1] 
     ,[PropertyLocationStreet2] 
     ,[PropertyCity] 
     ,[PropertyZip] 
     ,[CountyName] 
     ,[VERSION_1] 
     ,[VERSION_2] 
     ,[VERSION_3] 

    from ##TEMPO 

end 

if exists (select max(versions) from ##te having max(versions)='version2') 
Begin 
    insert new1 ([id] 
     ,[version1] 
     ,[version2] 


     ,[ParcelIDActual] 
     ,[PropertyLocationStreet1] 
     ,[PropertyLocationStreet2] 
     ,[PropertyCity] 
     ,[PropertyZip] 
     ,[CountyName] 
     ,[VERSION_1] 
     ,[VERSION_2] 



    ) 
select [id] 
     ,[version1] 
     ,[version2] 


     ,[ParcelIDActual] 
     ,[PropertyLocationStreet1] 
     ,[PropertyLocationStreet2] 
     ,[PropertyCity] 
     ,[PropertyZip] 
     ,[CountyName] 
     ,[VERSION_1] 
     ,[VERSION_2] 


    from ##TEMPO 

end 


if exists (select max(versions) from ##te having max(versions)='version1') 
Begin 
    insert new1 ([id] 
     ,[version1] 



     ,[ParcelIDActual] 
     ,[PropertyLocationStreet1] 
     ,[PropertyLocationStreet2] 
     ,[PropertyCity] 
     ,[PropertyZip] 
     ,[CountyName] 
     ,[VERSION_1] 




    ) 
select [id] 
     ,[version1] 
     ,[ParcelIDActual] 
     ,[PropertyLocationStreet1] 
     ,[PropertyLocationStreet2] 
     ,[PropertyCity] 
     ,[PropertyZip] 
     ,[CountyName] 
     ,[VERSION_1] 


    from ##TEMPO 

end 









Drop table ##TEMPO 
drop table ##Final 
DROP TABLE ##CE 
DROP TABLE ##HIST 
DROP TABLE ##TE 
DROP TABLE ##temp 
DROP TABLE ##version_info 
DROP TABLE ##Final1 



select * from NEW1 


END 
+0

可能是報告並行運行? – TheGameiswar

+0

你的意思是,沒有多重連接? –

+0

我只在查詢設計器中出現錯誤,在初始階段 –

回答

0

有幾個原因,這個代碼是容易發生故障。

  1. 通過使用具有相同名稱的全局臨時表,此過程不能並行運行(不能有重疊的執行)。第二次迭代將嘗試創建已經存在的表。
  2. 在沒有錯誤處理的情況下放置DROP TABLE語句意味着如果此過程因任何原因失敗,則DROP TABLE語句將不會全部執行,從而使臨時表以下一次執行的方式執行。這就是拋出你所看到的錯誤。

,你可以做些什麼來解決這個問題(因爲你已經做動態SQL)爲動態命名的表與在程序開始時確定一個單一的後綴,例如:

DECLARE @TableSuffix NVARCHAR(10)) 
SET @TableSuffix = CAST(ABS(CHECKSUM(NewId())) % 9999999999 AS NVARCHAR(10)); 

然後爲每個表,釘在後綴:

SET @DynamicPivotQuery = 
N'select * into ##Final' + @TableSuffix + 
N' from 
    (SELECT ID, FLOODZONE, New_Version' +' 
    FROM ##Temp' + @TableSuffix + N')SRC 
    PIVOT(MAX(FLOODZONE) 
    FOR New_vERSION IN (' + @ColumnName + ')) AS PVTTable' 

你需要讓每個全局臨時表的創造活力。只要您不打開查詢設計器的過程,SSRS應該可以使用動態SQL。絕對避免將過程的DDL作爲內聯報告SQL。