I have the f开发者_如何学Goollowing T-SQL (SQL Server 2008) and I need to email the result using database mail.
select top 5 col1, col2, col3, col4, col5, col6
from aTable
order by colOrder desc
col1 to col6 are long nvarchar(4000) and result is not readable. I wish the result be shown as
col1: aaa asa 11 11111111111111111111111111111111111111
col2: 22222222222222222222222222222222222222222222222222222222
col3: 333333333333333333333333333333333333333333333333333333333333333333
col4: 44444444444444444
col5: 555555555555555555555
col6: 666666666666
col1: bbbbbbb 1111111111111111111111111111111111
col2: 22 222 222222222222222222222
....
Is it possible to use for xml
with xlst to transform the result?
You should take a look a the COALESCE function to pivot you resultset :
Here is an example for your query :
DECLARE
@col1 nvarchar(max),
@col2 nvarchar(max)
SELECT top 5
@col1 = COALESCE(@col1,'') + col1 + ' '
,@col2 = COALESCE(@col2,'') + col2 + ' '
FROM aTable
ORDER BY colOrder
SELECT 'Col1: ' + @col1
union
SELECT 'Col2: ' + @col2
Source :http://www.mssqltips.com/sqlservertip/1521/the-many-uses-of-coalesce-in-sql-server/
精彩评论