multi part identifier could not be bound sql

2019-06-14 23:37发布

问题:

I get multipart can not be bound error on following query

update nfltx
set 
b.boxno = a.boxno,
b.message = a.message,
b.nameboxno = a.nameboxno,
b.namemsg = a.namemsg,
b.phoneboxno = a.phoneboxno,
b.phonemsg = a.phonemsg

FROM ofltx a JOIN nfltx b 
ON a.ls_fullnam = b.ls_fullnam

but if i remove b from boxno message and all i do not get the error . What is the reason behind this. Thank You using sql server 2008

回答1:

A table alias specified in a FROM clause cannot be used as a qualifier in SET column_name. This is not valid:

update nfltx
set 
b.boxno = a.boxno,
b.message = a.message,
b.nameboxno = a.nameboxno,
b.namemsg = a.namemsg,
b.phoneboxno = a.phoneboxno,
b.phonemsg = a.phonemsg

FROM ofltx a JOIN nfltx b 
ON a.ls_fullnam = b.ls_fullnam

To make it work, remove the b. alias from the column name.

update nfltx
set 
boxno = a.boxno,
message = a.message,
nameboxno = a.nameboxno,
namemsg = a.namemsg,
phoneboxno = a.phoneboxno,
phonemsg = a.phonemsg

FROM ofltx a JOIN nfltx b 
ON a.ls_fullnam = b.ls_fullnam

Raj



回答2:

What is the reason behind this?

An UPDATE (and DELETE, INSERT) can affect one, and only one, table. You've already identified which table you want to affect here:

update nfltx

Therefore, it doesn't make sense to allow an alias for the left hand side of assignments in the SET clause. They must be columns belonging to the previously identified table.

If the same table is included in the FROM clause multiple times (and it's the table you wish to update), you would need to provide an alias to indicate which instance of the table is to be updated - but you'd provide it (once) in the UPDATE clause rather than in the SET clause.



回答3:

just use

update b 

instead of

update nfltx

Man you guys make things too difficult for those that are learning.