Sql选择From子句中的值

时间:2014-06-09 18:35:40

标签: sql-server sql-server-2008-r2

我有一个棘手的时间这样做,所以希望有人可以提供帮助。这就是我想要的最终结果:

SELECT  'PRODUCT' AS ItemType, 
    'x' + CAST(MB_StaticOrderProducts.Quantity AS varchar(50)), 
     MB_StaticOrderProducts.ProductName + ' (' + CAST((MB_StaticOrderProducts.ProductSize) AS varchar(50)) + ' ' + MB_StaticProductMeasure.Value + ')' AS Name, 
     MB_StaticOrderProducts.ProductSizeID,
     GTIN as BarCode
 FROM 
     MB_StaticOrderProducts 
 INNER JOIN MB_StaticOrderVersions ON MB_StaticOrderProducts.StaticOrderVersionId = MB_StaticOrderVersions.StaticOrderVersionId 
     INNER JOIN MB_StaticProductMeasure ON MB_StaticOrderProducts.StaticProductMeasureId = MB_StaticProductMeasure.StaticProductMeasureId 
 Inner Join ProductVariantAttributeCombination pvac on (pvac.Id = (select id  from (select Id, cast(AttributesXml as xml) data from ProductVariantAttributeCombination) d cross apply data.nodes('//ProductVariantAttributeValue[Value[1] = 32]') data(d)))
  WHERE 
      MB_StaticOrderProducts.StaticOrderVersionId = '8D803EAE-2CFC-455C-9CE7-0849618E1548'

我希望列MB_StaticOrderProducts.ProductSizeId位于From部分的第4行,ProductVariantAttributeCombination,其中数字为32。有没有办法在该区域使用变量?

2 个答案:

答案 0 :(得分:1)

对于那些需要它的人。我将data.nodes行更改为:

data.nodes( '//ProductVariantAttributeValue[Value[1]
              =sql:column("MB_StaticOrderProducts.ProductSizeID")]')

并且一切顺利。感谢。

答案 1 :(得分:0)

使用Concat function

像这样:

data.nodes( concat('//ProductVariantAttributeValue[Value[1] = ',
                    MB_StaticOrderProducts.ProductSizeId, 
                   ']')