You are here: Re: SQL query help « MsSQL Server « IT news, forums, messages
Re: SQL query help

Posted by Erland Sommarskog on 04/04/07 22:03

AdrianG (adrian.grigorof@altairtech.ca) writes:
> Right on, Erland! The page compiled by Anith Sen was exactly what I
> was looking for - concatenation of row values. I tried the first
> approach (Dynamic SQL) but while it worked for a small number of
> records, it failed ( Server stack limit has been reached.) against a
> larger number (i.e. 20,000 records which is not really that much).
> However, the blackbox XML method worked like a charm. Here is a
> slightly modified version (to only show distinct sports and to remove
> the trailing spaces) that worked quite fast:
>
> SELECT p1.name,
> ( SELECT distinct RTRIM(sport) + ', '
> FROM Athletes p2
> WHERE p2.name = p1.name
> ORDER BY RTRIM(sport) + ', '
> FOR XML PATH('') ) AS sports
> FROM Athletes p1
> GROUP BY name ;
>
> Now, the real database is more complex than this example but it is
> surely a great start.

Great to hear that you got a solution.

I don't recall if Anith discusses this, but there is a small problem
with the method above: if the data has charcaters that are special to
XML, they will be itemised. For instance an ampersand will become &.
There are some more or less ugly methods to habdle that, but I don't
recall the details at the moment.



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация