我有这个运行良好且没有问题的查询!
SELECT [Q].sHost
, LEFT([Q].sDescription, Len([Q].sDescription) - 1) AS [sDescription]
FROM (
SELECT DISTINCT [Q2].sHost
, (
SELECT CONVERT(NVARCHAR(MAX), [Q1].[sDescription]) + N', ' AS [text()]
FROM (
SELECT (
CASE
WHEN (CHARINDEX('\', [sInstance]) = 0)
THEN [sInstance]
ELSE substring([sInstance], 0, CHARINDEX('\', [sInstance]))
END
) AS sHost
, [sDescription]
FROM [db_group].[dbo].[instanceCommentsList]
) AS [Q1]
WHERE ([Q1].sHost = [Q2].sHost)
FOR XML PATH('')
) [sDescription]
FROM (
SELECT (
CASE
WHEN (CHARINDEX('\', [sInstance]) = 0)
THEN [sInstance]
ELSE substring([sInstance], 0, CHARINDEX('\', [sInstance]))
END
) AS sHost
, [sDescription]
FROM [db_group].[dbo].[instanceCommentsList]
) AS [Q2]
) AS [Q]
当我将此查询另存为视图时,出现以下错误
'(' 附近的 WHERE 子句出错。
'=' 附近的 WHERE 子句出错。无法
解析查询文本。
不知道问题出在哪里!
它可以保存任何方式并且它有点工作,我可以在简单的选择查询中使用它,但是我在视图名称下得到一条红线,当我将鼠标放在上面时,我收到了这条消息
对象名称无效....
如果我在更复杂的查询中使用它,它根本不起作用
编辑: - - - - - - - - - - - - - - - - -
阅读后由于SelectDistinct的评论无法使 FOR XML PATH 工作
我修复了它,但仍然出现另一个错误!
'(' 附近的 WHERE 子句出错。
无法解析查询文本。
这是固定代码:
SELECT [Q].sHost
, LEFT([Q].sDescription, Len([Q].sDescription) - 1) AS [sDescription]
FROM (
SELECT DISTINCT [Q2].sHost
, (
SELECT CONVERT(VARCHAR(MAX), [Q1].[sDescription]) + ', ' AS [text()]
FROM (
SELECT (
CASE
WHEN (CHARINDEX('\', [sInstance]) = 0)
THEN [sInstance]
ELSE substring([sInstance], 0, CHARINDEX('\', [sInstance]))
END
) AS sHost
, [sDescription]
FROM [db_group].[dbo].[instanceCommentsList]
) AS [Q1]
WHERE ([Q1].sHost = [Q2].sHost)
FOR XML PATH(''), type
).value('.', 'varchar(max)') as [sDescription]
FROM (
SELECT (
CASE
WHEN (CHARINDEX('\', [sInstance]) = 0)
THEN [sInstance]
ELSE substring([sInstance], 0, CHARINDEX('\', [sInstance]))
END
) AS sHost
, [sDescription]
FROM [db_group].[dbo].[instanceCommentsList]
) AS [Q2]
) AS [Q]