【发布时间】:2017-08-22 12:22:59
【问题描述】:
我目前无法运行以下 SQL 查询:
SELECT
c.[DeviceName],
STUFF((SELECT ' + ' + sw.[SuiteName] FROM [dbo].[AppSoftwareSuites] as sw where sw.[Computer_Idn] = c.[Computer_Idn] AND sw.[SuiteName] like 'CFRM-Server%' FOR XML PATH('')),1,3,'') AS 'CFRM',
STUFF((SELECT ' + ' + sw.[SuiteName] FROM [dbo].[AppSoftwareSuites] as sw where sw.[Computer_Idn] = c.[Computer_Idn] AND sw.[SuiteName] like 'Communication Module%' FOR XML PATH('')),1,3,'') AS 'Communication Module',
STUFF((SELECT ' + ' + sw.[SuiteName] FROM [dbo].[AppSoftwareSuites] as sw where sw.[Computer_Idn] = c.[Computer_Idn] AND sw.[SuiteName] like 'GEBIT-Commons-Java%' FOR XML PATH('')),1,3,'') AS 'Gebit Commons Java',
STUFF((SELECT ' + ' + sw.[SuiteName] FROM [dbo].[AppSoftwareSuites] as sw where sw.[Computer_Idn] = c.[Computer_Idn] AND sw.[SuiteName] like 'GEBIT-Commons_JBOSS%' FOR XML PATH('')),1,3,'') AS 'Gebit Commons JBOSS',
STUFF((SELECT ' + ' + sw.[SuiteName] FROM [dbo].[AppSoftwareSuites] as sw where sw.[Computer_Idn] = c.[Computer_Idn] AND sw.[SuiteName] like 'Mobile Store%' FOR XML PATH('')),1,3,'') AS 'Mobile Store',
STUFF((SELECT ' + ' + sw.[SuiteName] FROM [dbo].[AppSoftwareSuites] as sw where sw.[Computer_Idn] = c.[Computer_Idn] AND sw.[SuiteName] like 'NEWPOSS-Store-Server%' FOR XML PATH('')),1,3,'') AS 'NEWPOSS',
STUFF((SELECT ' + ' + sw.[SuiteName] FROM [dbo].[AppSoftwareSuites] as sw where sw.[Computer_Idn] = c.[Computer_Idn] AND sw.[SuiteName] like 'Store Portal - Complete%' FOR XML PATH('')),1,3,'') AS 'Store Portal',
STUFF((SELECT ' + ' + sw.[SuiteName] FROM [dbo].[AppSoftwareSuites] as sw where sw.[Computer_Idn] = c.[Computer_Idn] AND sw.[SuiteName] like 'Store S&R Services%' FOR XML PATH('')),1,3,'') AS 'SSR'
FROM
[dbo].[Computer] as c WITH (NOLOCK)
WHERE
c.[DeviceName] LIKE '%STL01'
ORDER BY
c.[DeviceName] ASC;
输出没问题,但似乎数据库中的一个产品(用于“SSR”)有一个“&”字符,导致输出损坏(“Store S&R Services”而不是“S&R Services xx.xx.xx ')。
是否有机会在输出中执行字符串替换?
“For XML”的原因是因为数据库中每个 DeviceName 的双重条目导致结果为空。
任何帮助表示赞赏:)
【问题讨论】:
标签: sql string sql-server-2008 replace for-xml