Declan Basile
asked on
Error running an update statement with a pivot table in SQL Server
In SQL Server 2012, why do I get the error "The multi-part identifier "T1.Text1" could not be bound." when I run the following statement:
UPDATE DBGUIds SET T1.Text1 = PT.Text1 FROM DBGUIds T1 INNER JOIN
(SELECT InstanceId, EntityId, Text1
FROM
(SELECT InstanceId, EntityId, FieldName, Value FROM TB WHERE TableName = 'DBGUIds') T3
PIVOT
(
MAX(Value) FOR FieldName IN (Text1)
) V) PT ON T1.int1 = PT.InstanceId
Please note that the following statement works ...
SELECT T1.Text1, PT.Text1 FROM DBGUIds T1 INNER JOIN
(SELECT InstanceId, EntityId, Text1
FROM
(SELECT InstanceId, EntityId, FieldName, Value FROM TB WHERE TableName = 'DBGUIds') T3
PIVOT
(
MAX(Value) FOR FieldName IN (Text1)
) V) PT ON T1.int1 = PT.InstanceId
and outputs ...
Text1 Text1
Null 10
Null 20
UPDATE DBGUIds SET T1.Text1 = PT.Text1 FROM DBGUIds T1 INNER JOIN
(SELECT InstanceId, EntityId, Text1
FROM
(SELECT InstanceId, EntityId, FieldName, Value FROM TB WHERE TableName = 'DBGUIds') T3
PIVOT
(
MAX(Value) FOR FieldName IN (Text1)
) V) PT ON T1.int1 = PT.InstanceId
Please note that the following statement works ...
SELECT T1.Text1, PT.Text1 FROM DBGUIds T1 INNER JOIN
(SELECT InstanceId, EntityId, Text1
FROM
(SELECT InstanceId, EntityId, FieldName, Value FROM TB WHERE TableName = 'DBGUIds') T3
PIVOT
(
MAX(Value) FOR FieldName IN (Text1)
) V) PT ON T1.int1 = PT.InstanceId
and outputs ...
Text1 Text1
Null 10
Null 20
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER