SQL Server update statement with join -


I have data in a load table and try to write an update statement to populate an additional self-referenced table. I'm doing, but I'm having trouble with the exact syntax.

Here is a simple example of two table layouts and simultaneous data:

  create table tmpLoad (status number VARCHAR (2), supervisor number VARCHAR (2)) tmpLoad values ​​( '01', '00') tmpLoad values ​​( '02', '01') tmpLoad values ​​( '03 put', '01') put in place to put tmpLoad value ('04, '03 '(', '05 03 ') CREATE TABLE tmpPosition (PositionID integer, PositionNumber VARCHAR (2), putting SupervisorID integer) into tmpPosition value (1 ") into tmpLoad value' 01 ', nULL) tmpPosition value (2 , '02', faucet) are tmpPosition values ​​(e.g. 3, '03' faucet) (,, 4 '04 'Tap' INSERT tmpPosition value insert into the tmpPosition value (, 5 '05', null)  

Data pole in TM As a unique identifier, And their respective supervisors represent five employees using their PositionNumber .

  • The employee is 01 employee ( supervisor number == 0)
  • report to employee 02 and 03 staff 01
  • employee 04 and 05
  • tmpPosition table is automatically referenced, where status ID can be in several other records' Supervisor ID column.

    As you can see, the supervisor ID column is currently zero for all records, together with the appropriate PositionID in both of these tables I'm trying to populate it.

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

      a.PositionNumber, a.SupervisorNumber, b.PositionID, b .PositionNumber, b.SupervisorID involved in selecting a.SupervisorNumber = the tmpLoad b.PositionNumber a tourist tmpPosition b  

    produced the desired results like that to me, it seemed:

      Situation - Supervisor Number - StatusId - Status Report - Supervisor 02 -------------- 01 ---------------- 1 ----------- 01 ------------ - Faucet 03 -------------- 01 -------- -------- 1 ----------- 01-- ------------ Faucet 04 ------------- - 03 ---------------- 3 ---- ------- 03 -------------- Tap 05 -------------- 03 ----------- ----- 3 - ---------- 03 -------------- Tap  

    From this, I assumed supervisor ID Columns will be populated with value from PositionID > column for these four records when I ran the following update query:

      update combinations based on a a.SupervisorNumber from tmpPosition set SupervisorID = b.PositionID tmpLoad tmpPosition b = B.PositionNumber  

    However, since I run the query was not the result I expected It was:

      SELECT * FROM tmpPosition PositionID - PositionNumber -SupervisorID 1 ----------- 01 -------------- 1 2 ----------- 02 ------- ------- Faucet 3 ----------- 03 -------- ------ 3 4 ----------- 04- ------------- Faucet 5 ----------- 05 - ------------ Null  

    will be the ideal results:

    StatusId - StatusNumber - Supervisor 1 ------ ----- 01 ----------- --- Faucet 2 ----------- 02 -------------- 1 3 ----------- 03 ----- --------- 1 4 ----------- 04 ---------- ---- 3 5 ----------- 05 -------------- 3

    What is happening here and I SupervisorID field to the status ID as described in this scenario?

    Try it ...

      Update tmpPosition SET SupervisorID = the tL.SupervisorNumber tmpPosition tp JOIN TmpLoad tl oN tp.PositionNumber = CONVERT (INT, tl.PositionNumber)  

    this is exactly what you want in this schema. Yet I do not think this is a good DB design.

      update tmpPosition set SupervisorID = case when tl.SupervisorNumber = 0 and then minus tl.SupervisorNumber end tmpPosition include tmpLoad tl on tp tp which .PositionNumber = CONVERT (INT, tl.PositionNumber)  

    Comments

    Popular posts from this blog

    Member with no value in F# -

    java - Joda Time Interval Not returning what I expect -

    c# - Showing a SelectedItem's Property -