Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to do inside quotes
Message
 
 
À
10/09/2013 18:09:28
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 6.5 and older
Application:
Web
Divers
Thread ID:
01582787
Message ID:
01582793
Vues:
37
>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!

It should be where p.ApprovedStatus = ''Active'' (double single quote).

BTW, since in your case you don't use parameters, you can do

execute (@SQL)
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform