我收到的不是有效的标识符错误。我试过调整引号但仍然无法做到正确。
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 :(得分:0)
我想你想要这样:
Set @DynamicQuery = 'Select * into #tmpcde from openrowset(''SQLNCLI'', ''Server=HPL-DSQL2\HPLDSQL2;Trusted_Connection=yes;'', '''+@DynamicPivotQuery+''')';