开发者

What is wrong with this use of the SQL alias (AS)?

I'm getting an error message when running the follwoing SQL statement. It's a pretty basic statement and I've got to be overlooking something REALLY simple.

If I remove the alias, the statement executes just fine. Add the alias back in and I get the error.

The message in SSMS:

Msg 4104, Level 16, State 1, Line 2
The multi-part identifie开发者_如何学Gor "T1.titleId" could not be bound.

The SQL statement:

UPDATE People
SET T1.titleId = T2.ID 
FROM people as T1 
INNER JOIN titles as T2
ON T1.title = T2.Title 

Update 1 - The comma is not part of the statement and I'm still getting the error.


You have some syntax issues in your UPDATE. It should be:

UPDATE T1
SET titleId = T2.ID
FROM people as T1
INNER JOIN titles as T2
ON T1.title = T2.Title


You have two issues.

1 - The comma. I'm guessing you used the deprecated implicit JOIN syntax at some point.

2 - You should also use the alias on the UPDATE line when you have a JOIN. You are SETing a field in t1 but UPDATEing people which is inconsistent.

Try:

UPDATE t1
SET T1.titleId = T2.ID 
FROM people as T1
INNER JOIN titles as T2
ON T1.title = T2.Title 


EDIT: Changed People to T1 You have a comma after T1 that is creating the problem.

Change the statement to:

UPDATE T1 -- People 
  SET T1.titleId = T2.ID  
 FROM people as T1 --Removed the comma that was here
 INNER JOIN titles as T2 
   ON T1.title = T2.Title


Try this:

UPDATE t1
SET T1.titleId = T2.ID 
FROM people as T1 
INNER JOIN people as T2
ON T1.titleId = T2.titleId 

It can not find T1.titleId because you are updating "Person" and sql can only see the Person tables that you aliased as T1 and T2. So you would want to update T1 in your case. Also you had an extra comma in your inner join.


I think the comma after as T1, is superfluous. Please try removing it.


UPDATE t1 
SET titleId = T2.ID  
FROM people as T1  
INNER JOIN people as T2 
ON T1.titleId = T2.titleId  

You do not alias the field on the left side of the SET

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜