2016-03-07 66 views
0

我得到一個不是一個有效的標識符錯誤。我嘗試調整引號,但仍然無法正確引用。openrowset錯誤中的動態sql「不是有效的標識符。」

TIY

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

--Get distinct values of the PIVOT Column 
SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
     + QUOTENAME(CovCode) 
FROM (SELECT distinct 
    CovCode 

FROM Construct.dbo.View_Claim_SummaryPTS4PTS5Diamond where system in (4,5)) AS ClaimSummary 

--Prepare the PIVOT query using the dynamic 
SET @DynamicPivotQuery = 
    N'SELECT vehicleid, ' + @ColumnName + ' 
    FROM Construct.dbo.View_Claim_SummaryPTS4PTS5Diamond 
    PIVOT(max(Claim_Exp_Nbr) 
      FOR CovCode IN (' + @ColumnName + ')) AS PVTTable' 

Set @DynamicQuery = 'Select * into #tmpcde from openrowset(''SQLNCLI'', ''Server=HPL-DSQL2\HPLDSQL2;Trusted_Connection=yes;'', '''Exec @DynamicPivotQuery''')' 
exec (@DynamicQuery) 

Select * from #tmpcde 
+0

'打印@ DynamicPivotQuery' –

+0

難道我的回答可以幫助您以任何方式? –

回答

0

我想你想這樣的:

Set @DynamicQuery = 'Select * into #tmpcde from openrowset(''SQLNCLI'', ''Server=HPL-DSQL2\HPLDSQL2;Trusted_Connection=yes;'', '''[email protected]+''')'; 
相關問題