下面是代码:
CREATE VIEW [dbo].[View_GetAllContents] WITH SCHEMABINDING AS
SELECT C.ContentID,
C.Title,
C.[Description],
C.Tags,
C.Markets,
C.Formats,
C.Categories,
C.IsGlobalSalesResource,
C.IsActive,
C.CreatedBy,
C.CreatedDate,
C.ModifiedBy,
STUFF(
(SELECT ',' + T.TagName
FROM [dbo].[Tags] T
WHERE T.IsActive=1
AND T.TagID IN
(SELECT LTRIM(RTRIM(items))
FROM [dbo].[SplitString](C.Tags, ','))
FOR XML PATH(''), TYPE).value('.[1]', 'nvarchar(max)'), 1, 2, '') AS TagNames,
STUFF(
(SELECT ',' + M.MarketName
FROM [dbo].[Markets] M
WHERE M.IsActive=1
AND M.MarketID IN
(SELECT LTRIM(RTRIM(items))
FROM [dbo].[SplitString](C.Markets, ','))
FOR XML PATH(''), TYPE).value('.[1]', 'nvarchar(max)'), 1, 2, '') AS MarketNames,
STUFF(
(SELECT ',' + F.FormatName
FROM [dbo].[Formats] F
WHERE F.IsActive=1
AND F.FormatID IN
(SELECT LTRIM(RTRIM(items))
FROM [dbo].[SplitString](C.Formats, ','))
FOR XML PATH(''), TYPE).value('.[1]', 'nvarchar(max)'), 1, 2, '') AS FormatNames,
STUFF(
(SELECT ',' + Ca.CategoryName
FROM [dbo].[Categories] Ca
WHERE Ca.IsActive=1
AND Ca.CategoryID IN
(SELECT LTRIM(RTRIM(items))
FROM [dbo].[SplitString](C.Categories, ','))
FOR XML PATH(''), TYPE).value('.[1]', 'nvarchar(max)'), 1, 2, '') AS CategorieNames,
STUFF(
(SELECT '$A$' + cast(A.AttachmentID AS varchar) + '$,$' + cast(A.ActualFileName AS nvarchar(1000))
FROM [dbo].[Attachments] A
WHERE A.ContentID=C.ContentID
FOR XML PATH(''), TYPE).value('.[1]', 'nvarchar(max)'), 1, 3, '') AS Attachments
FROM [dbo].[Contents] C GO
以下代码错误:
CREATE UNIQUE CLUSTERED INDEX [IX_View_GetAllContents_ContentID] ON [dbo].[View_GetAllContents](ContentID);
GO
例外: 无法在视图“ SM_SalesWarehouse.dbo.View_GetAllContents”上创建索引,因为它包含一个或多个子查询。考虑将视图更改为仅使用联接而不是子查询。另外,请考虑不要为此视图编制索引。