我有一张Product
记录和Model
记录的表格。产品和型号记录都有一个QTY
和Size
列。对于给定的product_id
,我想从记录中获取所有qtys
和,并将它们放入**父记录的单个*字段中,由管道“”分隔。这是我到目前为止的sql。它成功地从给定 product_id 的模型记录中获取大小和数量值,但我无法让它更新父产品记录字段,并递归执行。有人可以帮帮我吗?谢谢sizes
Model
qty and size
Product
|
此示例使用 product_id'26206'
--declare local variables
DECLARE
@size_DelimitedString VARCHAR(MAX),
@qty_DelimitedString VARCHAR(MAX)
--pivot rows into delimited string
SELECT
@size_DelimitedString = COALESCE(@size_DelimitedString, '') + CONVERT(VARCHAR(10), [BD New Product Data].model_size) + '|'
FROM
[BD New Product Data]
where record_type = 'model' and product_id = '26206'
SELECT
@qty_DelimitedString = COALESCE(@qty_DelimitedString, '') + CONVERT(VARCHAR(10), [BD New Product Data].model_quantity) + '|'
FROM
[BD New Product Data]
where record_type = 'model' and product_id = '26206'
--trim off last pipe
SET @size_DelimitedString = (SELECT SUBSTRING(@size_DelimitedString, 1, LEN(@size_DelimitedString)-1))
SET @qty_DelimitedString = (SELECT SUBSTRING(@qty_DelimitedString, 1, LEN(@qty_DelimitedString)-1))
--show results
SELECT @size_DelimitedString
SELECT @qty_DelimitedString