I have a table with Product
records and Model
records. Both the product and model records has a QTY
and Size
column. For a given product_id
, I would like to grab all qtys
and sizes
from the Model
records, and place them in to the single* qty and size
field for the **parent Product
record, delimited by a pipe "|
". Here is the sql I have so far. It successfully grabs the size and qty values from the Model records for a given product_id, but I am unable to get it to update the parent Product record fields, and do it recursively. Can someone give me a hand? Thx
This example uses 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