Avatar of Declan Basile
Declan Basile
Flag for United States of America

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
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Declan Basile

8/22/2022 - Mon