DECLARE @PivotColumnHeaders VARCHAR(MAX)
SELECT @PivotColumnHeaders =
COALESCE(
@PivotColumnHeaders + ',[' + cast(propertyName as varchar(55)) + ']',
'[' + cast(propertyName as varchar(55))+ ']'
)
FROM property
print @PivotColumnHeaders
DECLARE @PivotTableSQL NVARCHAR(MAX)
SET @PivotTableSQL = N'
SELECT *
FROM (
SELECT
propertyname,value
FROM propertyvalue inner join Property
on propertyvalue.propertyid = property.propertyid
inner join organisationentity
on organisationentity.organisationId = propertyvalue.entityobjectid
) AS PivotData
PIVOT (
max(value)
FOR propertyname IN (
' + @PivotColumnHeaders + '
)
) AS PivotTable
'
Print @PivotTableSQL
EXECUTE(@PivotTableSQL)
SELECT @PivotColumnHeaders =
COALESCE(
@PivotColumnHeaders + ',[' + cast(propertyName as varchar(55)) + ']',
'[' + cast(propertyName as varchar(55))+ ']'
)
FROM property
print @PivotColumnHeaders
DECLARE @PivotTableSQL NVARCHAR(MAX)
SET @PivotTableSQL = N'
SELECT *
FROM (
SELECT
propertyname,value
FROM propertyvalue inner join Property
on propertyvalue.propertyid = property.propertyid
inner join organisationentity
on organisationentity.organisationId = propertyvalue.entityobjectid
) AS PivotData
PIVOT (
max(value)
FOR propertyname IN (
' + @PivotColumnHeaders + '
)
) AS PivotTable
'
Print @PivotTableSQL
EXECUTE(@PivotTableSQL)