Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to do inside quotes
Message
De
10/09/2013 18:09:28
 
 
À
Tous
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Titre:
How to do inside quotes
Versions des environnements
SQL Server:
SQL Server 6.5 and older
Application:
Web
Divers
Thread ID:
01582787
Message ID:
01582787
Vues:
79
Hi All -

My query is built with an SQL string that is executed by separate command.
declare @SQL nvarchar(max), @Columns VARCHAR(max)

set @columns = stuff((
select ', ' + quotename(FundType) 
FROM ( 
SELECT FundType 
FROM LookupFundtypes P ) S
WHERE FundType <> ''
ORDER BY FundType 
FOR XML PATH('')), 1,2,'') 

print @Columns

set @SQL = 
';with cte as (
SELECT * 
FROM (
SELECT sponsor, fundtype, amount
from TIP_ApprovedFunding f
inner join TIP_Projects p on p.TipID = f.TipID
where p.ApprovedStatus = [Active] 
 )
 select * from cte  
 PIVOT
 (
   Sum(amount)
   FOR [FundType]
   IN (' + @Columns + ')
 ) AS PivotTableAlias'
 
execute sp_ExecuteSQL @SQL
Note that it attempts to embed a string 'Active' inside the main sql string. This is erroring out with a message of 'incorrect syntax near SELECT'. I am fairly sure the problem is delineating the word 'Active'. Can someone tell me how to do this, or do you see something else wrong with the query?

Thanks UT is great!
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform