Click here to Skip to main content
15,888,239 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i work on SQL server 2012
I need to run statement without using exec
meaning i need to run statement without using exec

What I have tried:

DECLARE @Columns as VARCHAR(MAX)
SELECT @Columns =
COALESCE(@Columns + ', ','') + QUOTENAME(FeatureName)
FROM
   (select distinct FT.FeatureName from [CustomerLocations].[ItemFeatures] ItemF
  inner join [CustomerLocations].FeatureTypes FT ON ItemF.FeatureId=ft.FeatureId
   ) AS B
   ORDER BY B.FeatureName

DECLARE @SQLs as VARCHAR(MAX)

SET @SQLs = 'SELECT ItemId,IPN,PartnerName,CustomerName,FeatureValue, ' + @Columns + '
FROM
(
  select F.ItemId,i.IPN,i.PartnerName,i.PartnerPart,it.CustomerName,t.FeatureName,f.FeatureValue from #ItemFeatures It 
 inner join [CustomerLocations].[ItemFeatures] F on f.CustomerId=it.CustomerId  
 left join [CustomerLocations].[Items] I on i.ItemID=f.ItemId
 left join CustomerLocations.FeatureTypes T on t.FeatureId=f.FeatureId
) as PivotData
PIVOT
(
   COUNT(F.ItemId)
   FOR FeatureName IN (' + @Columns + ')
) AS PivotResult
ORDER BY ItemId'

EXEC(@SQLs)
Posted
Updated 3-Feb-20 4:05am

1 solution

You can't generate a dynamic pivot without using dynamic SQL. Which means you have to use EXEC.

You could potentially use sp_executesql[^] if you had parameters to pass in. But you can't pass the pivot column names as parameters.

The only other option would be for your query to return the raw data, and for your application to perform the pivot in code.
 
Share this answer
 
Comments
CHill60 3-Feb-20 10:06am    
Beat me to it! 5'd
ahmed_sa 3-Feb-20 22:30pm    
can you help me
i need to run as select statement
see How to run that
DECLARE @Columns as VARCHAR(MAX)
SELECT @Columns =
COALESCE(@Columns + ', ','') + QUOTENAME(FeatureName)
FROM
--distinct FT.FeatureName
(select distinct FT.FeatureName from #InputData Feat inner join #ItemFeatures ItemF
on ItemF.CustomerId=Feat.CustomerId INNER join #FeatureType FT on ItemF.FeatureId=FT.FeatureId

) AS B
ORDER BY B.FeatureName

SELECT ItemCode, IPN,PartnerName,CustomerName, ' + @Columns + '
FROM
(
select F.ItemId,F.ItemId as ItemCode,I.IPN,I.PartnerName,I.PartnerPart,c.CustomerName,t.FeatureName from #InputData Itm
inner join #ItemFeatures F on F.CustomerId=Itm.CustomerId
inner join #Items I on I.ItemID=F.ItemId
inner join #FeatureType T on T.FeatureId=F.FeatureId
inner join #customers c on c.CustomerID=F.CustomerID
) as PivotData
PIVOT
(
COUNT(ItemId)
FOR FeatureName IN (' + @Columns + ')
) AS PivotResult
ORDER BY CustomerName
Richard Deeming 4-Feb-20 7:21am    
As I said, you can't.

You can't run a dynamic pivot without dynamic SQL. Which means using EXEC or sp_executesql.

If you don't want to do that, then return the raw data from your query, and perform the pivot in code.
ahmed_sa 3-Feb-20 22:41pm    
but i face problem on this line
FeatureName IN (' + @Columns + ')

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900