Created
May 16, 2019 11:19
-
-
Save timgaunt/5dbf370364c0d982bf348a4639277f74 to your computer and use it in GitHub Desktop.
List all Ucommerce products with their properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @cols AS NVARCHAR(MAX), @nullLessCols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX); | |
SET @cols = STUFF(( | |
SELECT ',' + QUOTENAME(pdf.Name) | |
FROM uCommerce_ProductDefinitionField AS pdf | |
WHERE pdf.Deleted=0 | |
ORDER BY pdf.SortOrder | |
FOR XML PATH(''), TYPE | |
).value('.', 'NVARCHAR(MAX)'),1,1,'') | |
set @query = ' | |
SELECT TOP 10 p.ProductId, p.Sku, p.VariantSku, p.Name, ' + @cols + ' | |
FROM | |
( | |
SELECT pp.ProductId, pdf.Name, pp.[Value] | |
FROM dbo.uCommerce_ProductProperty pp LEFT JOIN uCommerce_ProductDefinitionField pdf ON pp.ProductDefinitionFieldId = pdf.ProductDefinitionFieldId | |
) AS x | |
pivot | |
( | |
MAX([Value]) | |
FOR [Name] in (' + @cols + ') | |
) AS pvt | |
JOIN uCommerce_Product p ON p.ProductId=pvt.ProductId | |
' | |
print(@query) | |
execute(@query); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment