DECLARE @denominationList NVARCHAR(MAX)
SELECT @denominationList = STUFF(
(SELECT '],[' + CONVERT(NVARCHAR(20),VALUE)
FROM Gift_Denomination WHERE IsInactive = 0
FOR XML PATH('')),1,2,'') +']'
DECLARE @sql NVARCHAR(max)
SET @sql =
'SELECT * FROM (
SELECT ShopId, ShopName,Value, 0 IssueQty FROM dbo.ShopList s, Gift_Denomination d
WHERE LEFT(s.ShopId,1) = ''G'' AND d.IsInactive = 0
) as SourceTable
PIVOT
(
MAX(IssueQty)
FOR Value IN ('+@denominationList+')
) AS PivotTable order by shopid'
EXECUTE (@sql)
SELECT @denominationList = STUFF(
(SELECT '],[' + CONVERT(NVARCHAR(20),VALUE)
FROM Gift_Denomination WHERE IsInactive = 0
FOR XML PATH('')),1,2,'') +']'
DECLARE @sql NVARCHAR(max)
SET @sql =
'SELECT * FROM (
SELECT ShopId, ShopName,Value, 0 IssueQty FROM dbo.ShopList s, Gift_Denomination d
WHERE LEFT(s.ShopId,1) = ''G'' AND d.IsInactive = 0
) as SourceTable
PIVOT
(
MAX(IssueQty)
FOR Value IN ('+@denominationList+')
) AS PivotTable order by shopid'
EXECUTE (@sql)
No comments:
Post a Comment