Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to do inside quotes
Message
 
 
À
10/09/2013 18:32:34
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:
01582794
Vues:
42
>By the way, on a less important note....I've discovered recently (and a recent post by Boris reminded me), it's not necessary to use the FOR XML trick to simply generate a string of values.
>
>Just as a simple example....using the ShipMethod table in Adventureworks....I used to do this...
>
>
>declare   @ShipMethodNameList nvarchar(1000) = ''
>set @ShipMethodNameList = 
>      stuff (  (  select distinct ',[' +  
>           cast(ShipMethodID as varchar(100))  +
>               '] as [' + Name + ']'     
>               from Purchasing.ShipMethod 
>                        for xml path('') ), 1, 1, '')
>
>
>That certainly works, but usually can be done simply by doing this...where SQL Server will concatenate the string for each row the engine encounters.
>
>
>declare @otherlist varchar(8000) = ''
>select @OtherList = @OtherList + '['  + cast(ShipMethodID as varchar(2)) + '] as [' + Name + '],' 
>        from Purchasing.ShipMethod
>
>set @OtherList = substring(@OtherList,1,len(@OtherList)-1)
>
>
>The FOR XML PATH has great uses, but in this case it can also be done a bit easier.

This is undocumented feature and should not be used. First of all, it doesn't respect the ORDER BY, secondly it has problems with NULL values. So, I would not recommend to do this in a production code.

You may check MSDN T-SQL forum and posts by Erland Sommarskog if you don't believe me.
If it's not broken, fix it until it is.


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

Click here to load this message in the networking platform