SQL Server How to update Table A, when there are conditions in Table B

92vpleto  于 2023-05-28  发布在  其他
关注(0)|答案(1)|浏览(185)

I am trying to run a query that updates a SQL Server table (Table A) value in column 3 only on the include condition that is present in Table B and in column 2 (pref) of Table A

The primary key on both tables is Employee_number .

And the condition in Table B active or inactive

Select Distinct employee_number 
from dbo.tableB  
where employee_number not in (select distinct employee_number 
                              from dbo.tableA 
                              where pref Pref = 'work') 
  and active_or_inactive = 'a'

Update TableA
Set value = “good”
Where employee_number from dbo.tableB  
where employee_number not in (employee_number 
                              from do.tableA 
                              where Pref = 'work') 
  and active_or_inactive = 'a'
nkhmeac6

nkhmeac61#

It's not entirely clear how everything fits together in the sample code, but try this:

UPDATE a
SET value = 'good'
FROM TableA a
WHERE NOT EXISTS (
        SELECT 1 
        FROM TableB b
        WHERE b.employee_number = a.employee_number
            AND b.active_or_inactive = 'a'
   )
   AND a.Pref='work'

相关问题