0

I have a manager_details table with team_id being NULL. I have another table Team_details where the team id and manager id are populated. I want to, update the manager_details.Team_id with the values of team_details.team_id values based on the manager_id values.

This is manager_details table :

Manger_id Manager_name Team_id
1009 Edward Null
1101 Gori Null
1002 Shiv Null

This is Team_details table :

Team_id Team_name manager_id
100 NOAM 1101
101 EMEA 1009
102 APGC 1002

Then final output of the Mananger_details should be something like this:

Manger_id Manager_name Team_id
1009 Edward 101
1101 Gori 101
1002 Shiv 102

I have tried the queries :

INSERT INTO dbo.Manager_details (team_id)
SELECT Team_details.Team_id
from [dbo].[Team_details]
INNER JOIN [dbo].[Manager_details]
on Team_details.team_id =  Manager_details.team_id
where manager_details.team_ID is null

The result I get is "0 rows affected". Thanks to @larnu's comment, I figured that as the manager_details.team_id is NULL, the insert into was not working. The solution that worked for me is below :

UPDATE dbo.Manager_details
SET manager_details.team_id = team_details.[Team_id]
FROM dbo.team_details
where Team_details.Manager_id= Manager_details.Manager_id
Duh_ragon
  • 1
  • 3
  • `manager_details.team_ID = null` can **never** be true. When comparing the `NULL` values you *need* to use `IS (NOT) NULL` (or in 2022+ you also have `IS (NOT) DISTINCT FROM`) – Thom A Oct 19 '22 at 10:54
  • @larnu thanks for the insights. What worked for me is this : `UPDATE dbo.Manager_details SET manager_details.team_id = team_details.[Team_id] FROM dbo.team_details where Team_details.Manager_id= Manager_details.Manager_id` – Duh_ragon Oct 19 '22 at 12:10

0 Answers0