我怎么做这样选择

时间:2012-04-10 05:53:11

标签: sql-server-2008-r2

    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。请帮忙!

1 个答案:

答案 0 :(得分:1)

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

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