3

在 C# 中运行此 XML SQL 时发生错误。什么不见​​了?它在 SQL Server 管理工作室中运行良好,但在 c# 中运行良好。

protected DataTable ServiceCodes(string orderColumn = default(string), bool orderAscending = true)
{
    string sql = @" SELECT DISTINCT(Split.a.value('.', 'VARCHAR(100)')) AS Data  
                             FROM  
                             (
                                 SELECT CAST('<M>' + REPLACE(Text09, ',', '</M><M>') + '</M>' AS XML) AS Data  
                                 FROM  Assets
                                 WHERE Category = 'Service'
                             ) AS A CROSS APPLY Data.nodes ('/M') AS Split(a) ORDER BY Data";

    return DatabaseConnection.ExecuteDataTable(sql, 120000);
}
4

1 回答 1

4

I think there's some special character in you Text09 column. Try this:

SELECT DISTINCT(Split.a.value('.', 'VARCHAR(100)')) AS Data  
FROM  
(
 SELECT CAST('<M>' + replace((select Text09 + '' for xml path('')), ',', '</M><M>') + '</M>' AS XML) AS Data  
 FROM  Assets
 WHERE Category = 'Service'
) AS A CROSS APPLY Data.nodes ('/M') AS Split(a) ORDER BY Data

see sql fiddle demo

于 2013-08-28T09:11:23.800 回答