内容简介:http://stackoverflow.com/questions/3202415/sqlxml-without-xml-encoding
.该解决方案使用SQLXML来合并数据(子查询):
SELECT STUFF(
( SELECT ', ' + Name
FROM MyTable _in
WHERE _in.ID = _out.ID
FOR XML PATH('')), -- Output multiple rows as one xml type value,
-- without xml tags
1, 2, '') -- STUFF: Replace the comma at the beginning with empty string
FROM MyTable _out
GROUP BY ID -- Removes duplicates
这完美的工作(甚至没有那么沉重的表现),除了我的数据现在通过SQLXML获得XML编码(& =& amp; amp; amp; amp;等) – 我不想要XML数据,我只是把它用作一个诡计 – 由于通用系统,我无法对其进行编码以将其清理干净,因此编码数据直接转到报告中.我不能使用通用系统的存储过程,所以CURSOR合并或COALESCE-ing在这里不是一个选项…
所以我正在寻找的是T-SQL中的一种方式,可以让我再次解码XML,甚至更好:避免SQLXML进行编码.显然,我可以编写一个这样做的存储函数,但是我更喜欢内置,更安全的方式…
谢谢你的帮助…
如果将类型指定为xml的选项,则可以使用XPath查询将XML类型转换回varchar.使用示例表变量:
declare @MyTable table (id int, name varchar(50)) insert @MyTable (id, name) select 1, 'Joel & Jeff' union all select 1, '<<BIN LADEN>>' union all select 2, '&&BUSH&&'
一个可能的解决方案是:
select b.txt.query('root').value('.', 'varchar(max)')
from (
select distinct id
from @MyTable
) a
cross apply
(
select CASE ROW_NUMBER() OVER(ORDER BY id) WHEN 1 THEN ''
ELSE ', ' END + name
from @MyTable
where id = a.id
order by
id
for xml path(''), root('root'), type
) b(txt)
这将打印:
Joel & Jeff, <<BIN LADEN>> &&BUSH&&
这是一个没有XML转换的替代方案.它确实有一个递归查询,所以性能里程可能会有所不同.是从 Quassnoi’s blog :
;WITH with_stats(id, name, rn, cnt) AS
(
SELECT id, name,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY name),
COUNT(*) OVER (PARTITION BY id)
FROM @MyTable
),
with_concat (id, name, gc, rn, cnt) AS
(
SELECT id, name,
CAST(name AS VARCHAR(MAX)), rn, cnt
FROM with_stats
WHERE rn = 1
UNION ALL
SELECT with_stats.id, with_stats.name,
CAST(with_concat.gc + ', ' + with_stats.name AS VARCHAR(MAX)),
with_stats.rn, with_stats.cnt
FROM with_concat
JOIN with_stats
ON with_stats.id = with_concat.id
AND with_stats.rn = with_concat.rn + 1
)
SELECT id, gc
FROM with_concat
WHERE rn = cnt
OPTION (MAXRECURSION 0)
http://stackoverflow.com/questions/3202415/sqlxml-without-xml-encoding
以上所述就是小编给大家介绍的《没有XML编码的SQLXML?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 0615 - 没有比较,就没有伤害
- 编码、摘要和加密(一)——字节编码
- 新媒体编码时代的技术:编码与传输
- 对话微软ONNX负责人:没有中国的认可,开源标准没有意义
- MySQL数据库字符编码总结--数据传输编码
- PHP 开发者学 Golang 之 URL 编码 (Urlencode)、解编码 (Urldecode)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Head First Rails
David Griffiths / O'Reilly Media / 2008-12-30 / USD 49.99
Figure its about time that you hop on the Ruby on Rails bandwagon? You've heard that it'll increase your productivity exponentially, and allow you to created full fledged web applications with minimal......一起来看看 《Head First Rails》 这本书的介绍吧!