0
    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)

此查询有效,但我无法根据此查询创建视图。请帮忙!

4

1 回答 1

1

正如 murtaza 所说,您可以为此创建一个存储过程,或者如果您想将其用作视图,您可以创建一个表值函数,您可以使用其他视图和连接。

http://msdn.microsoft.com/en-us/library/ms191165.aspx

于 2012-04-10T06:03:46.517 回答