Using the tutorial here:
http://www.sqlservercurry.com/2008/06/combine-multiple-rows开发者_JS百科-into-one-row.html
I wrote the following query:
SELECT DISTINCT STUFF( (SELECT '<li>' + CountryCode + '</li>' from Country FOR XML PATH('')),1,1,'') as Codes FROM Country
This works great, but all my HTML is now encoded (i.e. <li>
)
Is there a way around this? Using a SUBSTRING() approach does this also.
Change '<li>' + CountryCode + '</li>'
to CountryCode as li
and you will probably get what you want.
it is because XML PATH makes it xml safe so <
becomes <
try
DECLARE @CountryCode VARCHAR(MAX)
SELECT @CountryCode = ''
SELECT @CountryCode = @CountryCode + '<li>' + CountryCode + '</li>' from Country
SELECT @CountryCode
example
DECLARE @CountryCode VARCHAR(MAX)
SELECT @CountryCode = ''
SELECT @CountryCode = @CountryCode + '<li>' + name + '</li>'
FROM master..spt_values
WHERE type = 'a'
SELECT @CountryCode
you can also of course use the REPLACE
function to replace '<
' to '<'
In my opinion adding li
should be done at the front end and not in the database
精彩评论