简体   繁体   中英

SQL Server update statement with join

I have data in a load table and am trying to write an update statement to populate an additional self-referencing table, but am having difficulty with the exact syntax.

Here is a simplified example of the two table layouts and accompanying data:

CREATE TABLE tmpLoad (
    PositionNumber   VARCHAR(2),
    SupervisorNumber VARCHAR(2)
    )

INSERT INTO tmpLoad VALUES ('01', '00')
INSERT INTO tmpLoad VALUES ('02', '01')
INSERT INTO tmpLoad VALUES ('03', '01')
INSERT INTO tmpLoad VALUES ('04', '03')
INSERT INTO tmpLoad VALUES ('05', '03')

CREATE TABLE tmpPosition (
    PositionID int,
    PositionNumber VARCHAR(2),
    SupervisorID int
    )

INSERT INTO tmpPosition VALUES (1, '01', null)
INSERT INTO tmpPosition VALUES (2, '02', null)
INSERT INTO tmpPosition VALUES (3, '03', null)
INSERT INTO tmpPosition VALUES (4, '04', null)
INSERT INTO tmpPosition VALUES (5, '05', null)

The data in tmpLoad represents five employees, using their PositionNumber as a unique identifier, and their respective supervisors.

  • Employee 01 is the boss ( SupervisorNumber == 0)
  • Employees 02 and 03 report to Employee 01
  • Employees 04 and 05 report to Employee 03

The tmpPosition table is self-referencing, where the PositionID can be in many other records' SupervisorID column.

As you can see, the SupervisorID column is currently null for all records. I am trying to populate it with the appropriate PositionID by way of joining these two tables together.

To verify my idea, I ran the following SELECT query:

 SELECT a.PositionNumber,
        a.SupervisorNumber,
        b.PositionID,
        b.PositionNumber,
        b.SupervisorID
 FROM tmpLoad a
      JOIN tmpPosition b 
      ON a.SupervisorNumber = b.PositionNumber

Which, to me, looked like it produced the desired results:

PositionNumber--SupervisorNumber--PositionID--PositionNumber--SupervisorID
02--------------01----------------1-----------01--------------NULL
03--------------01----------------1-----------01--------------NULL
04--------------03----------------3-----------03--------------NULL
05--------------03----------------3-----------03--------------NULL

From this, I assumed that the SupervisorID column would be populated with the value from the PositionID column for these four records when I ran the following UPDATE query:

UPDATE tmpPosition
SET SupervisorID = b.PositionID
FROM tmpLoad a
     JOIN tmpPosition b 
     ON a.SupervisorNumber = b.PositionNumber

However, after I ran the query, the results were not what I expected:

SELECT *
FROM tmpPosition

PositionID--PositionNumber--SupervisorID
1-----------01--------------1
2-----------02--------------NULL
3-----------03--------------3
4-----------04--------------NULL
5-----------05--------------NULL

The ideal results would be:

PositionID--PositionNumber--SupervisorID
1-----------01--------------NULL
2-----------02--------------1
3-----------03--------------1
4-----------04--------------3
5-----------05--------------3

What is going on here and how can I populate the SupervisorID field with the PositionID as described in this scenario?

Try this...

UPDATE tmpPosition
SET SupervisorID = tL.SupervisorNumber
FROM tmpPosition tp   
JOIN tmpLoad tl ON tp.PositionNumber = CONVERT(INT,tl.PositionNumber) 

This will give exactly what you want in this schema. Still I don't think this is a good DB Design.

UPDATE tmpPosition 
SET SupervisorID = CASE WHEN tl.SupervisorNumber = 0 
                    THEN NULL 
                    ELSE tl.SupervisorNumber 
                   END
FROM tmpPosition tp   
JOIN tmpLoad tl ON tp.PositionNumber = CONVERT(INT,tl.PositionNumber)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM