Declare @Result as varchar(max)='';
SELECT DISTINCT @Result = dbo.MaterialTypes.Title + ', '+ @Result
FROM dbo.TempId LEFT OUTER JOIN
dbo.ProductMaterials ON dbo.ProductMaterials.Product = dbo.TempId.Id LEFT OUTER JOIN
dbo.MaterialTypes ON dbo.ProductMaterials.MaterialType = dbo.MaterialTypes.Id LEFT OUTER JOIN
dbo.Products ON dbo.Products.Id = dbo.TempId.Id
WHERE (dbo.Products.IsCollection = 1)
SELECT DISTINCT dbo.TempId.Id AS MaterialCollection_id, @Result
FROM dbo.TempId LEFT OUTER JOIN
dbo.ProductMaterials ON dbo.ProductMaterials.Product = dbo.TempId.Id LEFT OUTER JOIN
dbo.MaterialTypes ON dbo.ProductMaterials.MaterialType = dbo.MaterialTypes.Id LEFT OUTER JOIN
dbo.Products ON dbo.Products.Id = dbo.TempId.Id
WHERE (dbo.Products.IsCollection = 1)
此查询有效,但我无法根据此查询创建View。请帮忙!
答案 0 :(得分:1)
正如murtaza所说,您可以为此创建存储过程,或者如果您想将其用作视图,则可以创建表值函数,您可以使用其他视图和连接。