简体   繁体   English

更新一张表中的值(其中一列的值不相等)

[英]Update Values in One Table where value of one column is not equal

I have 2 tables like below: 我有2张桌子,如下所示:

Table Employee1: 表Employee1:

Emp Id      Name        Department
0001        Jack        Accounts
0002        Peter       Sales
0003        Beck        Sales
0004        Nancy       Marketing
0005        Parker      HR

Table Employee2: 表Employee2:

Emp Id      Name        Department
0001        Jack        HR
0002        Peter       Marketing
0004        Nancy       Sales
0005        Parker      Accounts

I would like to have a SQL Server script that will update the table Employee1 to: 我想要一个将表Employee1更新为的SQL Server脚本:

Emp Id      Name        Department
0001        Jack        HR
0002        Peter       Marketing
0003        Beck        Sales
0004        Nancy       Sales
0005        Parker      Accounts

Any pointers? 有指针吗?

You could try UPDATE with JOIN 您可以尝试使用JOIN UPDATE

UPDATE E1
    SET E1.Department = E2.Department
FROM Employee1 E1
INNER JOIN Employee2 E2
    ON E1.[Emp Id] = E2.[Emp Id]

We can use an update join here: 我们可以在这里使用更新联接:

UPDATE a
SET Department = b.Department
FROM Employee1 a
INNER JOIN Employee2 b
    ON a.[Emp Id] = b.[Emp Id]
WHERE
    a.Department <> b.Department

Try this 尝试这个

Update T1
SET T1 = CASE WHEN T1.Department <> T2.Department  
              THEN T2.Department 
              ELSE T1.Department 
          END
FROM Employee1 T1
INNER JOIN Employee2 T2  ON T1.[Emp Id] = T2.[Emp Id]  

You want the MERGE SQL command to update lines different and insert new ones int he same command: 您希望MERGE SQL命令更新不同的行并在同一命令中插入新行:

MERGE INTO Employee1 WITH (HOLDLOCK) AS target
USING Employee2 AS source
    ON target.[Emp Id] = source.[Emp Id]
WHEN MATCHED THEN 
    UPDATE SET target.Department = source.Department
WHEN NOT MATCHED BY TARGET THEN
    INSERT ([Emp Id], Name, Department)
    VALUES (source.[Emp Id], source.Name, source.Department);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据另一个表的值更新一个表的列值 - Update column value of one table based on values from another table 选择第2列的唯一值,其中第1列等于某个值 - selecting a distinct value of column 2 where column one is equal to a certain value SQL UPDATE SET 一列等于另一列引用的相关表中的值? - SQL UPDATE SET one column to be equal to a value in a related table referenced by a different column? 如何从同一个表的两列查询等于一个值 - How to query from two column of a same table where equal to one value 在powerpivot中选择一列的值彼此相等的行 - Select rows where values of one column are equal to each other in powerpivot 从另一个表中 ID 不匹配的列更新一个表中列的值 - Update value of column in one table from a column in another table where ID's do not match MySql 获取其中一列等于另一列等于另一表中的另一列的行 - MySql get rows where one column equals to another colum in another table where one column is equal 如何使用 SQL 将同一表和列上的值从一个 where 条件更新到另一个 where 条件 - How to update values from one where condition to other where condition on same table and column using SQL 使用另一个表中的数据更新一个表中的行,基于每个列中的一列相等 - Update rows in one table with data from another table based on one column in each being equal 根据多个列值相等,将一个表中的多行设置为另一个表中的多行 - Setting multiple rows in one table equal to multiple rows in another table based on multiple column values being equal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM