Link to home
Start Free TrialLog in
Avatar of Declan Basile
Declan BasileFlag 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
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Declan Basile

ASKER

Scott - I can't thank you enough.  I was searching all over the internet, experimenting, and trying to isolate the problem as best I can for hours before you posted the solution.  I was worried that the problem was that SQL Server wouldn't allow an update from a pivot table and am glad that's not the case.  Thank you very much.