Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I update a table with data retrieved by a join on itself?

I have the following data :

SectorKey Sector foo  
1         A      null 
2         B      null
...       ...    ...
1         null   a  
2         null   b 
2         null   c 
1         null   d 
2         null   e 
...       ...    ...

I want to update column Sector when it's null based on the value of sectorKey, ie I want Sector to be 'A' when SectorKey is 1 and 'B' when SectorKey is 2

I've tried this query :

update tbFoo
set Sector=A.sector 
from tbFoo A INNER JOIN tbFoo B
ON A.SectorKey=B.SectorKey 
and A.Sector is not null 
and B.Sector is null

and got this error message :

The table 'tbFoo' is ambiguous.

I've tried to alias the first tbFoo, but it doesn't seem to be a valid syntax. I don't understand why SQLServer complains about an ambiguous naming since I've got all my tables aliased.

I've found this thread, and I feel like I'm doing exactly the same thing as in the upvoted answer. I've also tried the query suggested in the accepted answer :

    update tbFoo A
    set Sector = 
       (select Sector from tbFoo 
        where A.SectorKey=SectorKey and Sector is not null)

and then SQLServer complains about an incorrect syntax near 'A'

Any ideas on what may be happening, and to fix this? I'm using SQLServer 2008.

EDIT I've not shown the total data of my table. I don't have only two cases (A and B), but rather a few thousands of cases. So an explicit case is not an option

like image 380
Brann Avatar asked Apr 07 '09 13:04

Brann


People also ask

Can you update a table with a join?

SQL UPDATE JOIN could be used to update one table using another table and join condition. UPDATE tablename INNER JOIN tablename ON tablename.

How do you update data using join?

SQL Server UPDATE JOIN syntax In this syntax: First, specify the name of the table (t1) that you want to update in the UPDATE clause. Next, specify the new value for each column of the updated table. Then, again specify the table from which you want to update in the FROM clause.

What happens when you join a table to itself?

The SELF JOIN in SQL, as its name implies, is used to join a table to itself. This means that each row in a table is joined to itself and every other row in that table. However, referencing the same table more than once within a single query will result in an error.


2 Answers

Use the alias in the first part of your update query:

update B
set Sector=A.sector 
from tbFoo A INNER JOIN tbFoo B
ON A.SectorKey=B.SectorKey 
and A.Sector is not null 
and B.Sector is null

Otherwise it doesn't know which instance of the table to update.

like image 117
Joel Coehoorn Avatar answered Sep 20 '22 14:09

Joel Coehoorn


Try to use a CTE and change the name of field for alias:

WITH CTE_TBFOO(SETOR)
AS
(  
    SELECT Sector
    FROM tbFoo T1  
)
update tbFoo
set Sector= A.SETOR
from CTE_TBFOO A 
WHERE A.SETOR = SectorKey 
and   A.SETOR  is not null 
and   B.Sector is null  
like image 20
CLAUDECINO Avatar answered Sep 19 '22 14:09

CLAUDECINO