I am iterating through a dataset of products looking for SKUs where the initial 6 characters of the product name with the sku @itemno are identical, then concatenating them into a CSV string to be used as part of a dataset to upload into an ecommerce platform. Here is the relevant operation:
DECLARE c1 CURSOR READ_ONLY
FOR
SELECT sku FROM tbl WHERE name LIKE (SELECT LEFT(name,6) FROM tbl WHERE sku = @itemno) + '%'
OPEN c1
FETCH NEXT FROM c1
INTO @c1
WHILE @@FETCH_STATUS = 0
BEGIN
SET @associated = @associated + @c1 + ','
FETCH NEXT FROM c1
INTO @c1
END
As you can imagine this is very slow considering the dataset size of 15,000+ records. Surely there is a more elegant way to do this using PIVOT or something?