I
I
Iangyl2021-04-20 19:41:30
SQL Server
Iangyl, 2021-04-20 19:41:30

How to fill a table from another by condition?

I have an Employee table:
607f02bbaa24b288580975.png
pos_id is a foreign key to the Position table:
607f031957fc9771203683.png

The Employee table has a position field in which the positions of employees are written, all those positions are in the Position table.

How can I query the pos_id so that the pos_id number in the Employee table matches the pos_id number in the Position table, while the position of the Employee table is equal to the pos_name of the Position table?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
d-stream, 2021-04-20
@Iangyl

chain the tables by position (inner join) and then you can assign the id from the job directory to the id of the employee directory

update Employee 
set pos_id=Position.pos_id -- очевидно что слева обновляемая таблица
from Emploee
inner join Position on Position.pos_name=Employee.position

And most importantly, after that, link the tables, drop Employee.position so that not a single dog can score a non-existent pos_id for an
employee. it might be useful to pre-evaluate select with a similar join to make sure most or all of the entries "match" and possibly wrap both sides of the bundle in ltrim(rtrim(...))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question