简体   繁体   English

SQL将一列插入另一个匹配的表

[英]SQL Insert one column into another table where there is a match

I have two tables a and b. 我有两个表a和b。

a: A:

TYPE            nvarchar(MAX)   
USERID          nvarchar(MAX)   
FIRSTNAME       nvarchar(MAX)   
LASTNAME        nvarchar(MAX)   
USERSTATUS      nvarchar(MAX)   
EMPLOYEETYPE    nvarchar(MAX)   
MANAGERID       nvarchar(MAX)   
STATUS          nvarchar(MAX)   
WEEKNUM         nvarchar(MAX)   
POLICYNAME      nvarchar(MAX)   
Acc_Check       nvarchar(MAX)   

b: b:

TYPE            nvarchar(MAX)   
USERID          nvarchar(MAX)   
FIRSTNAME       nvarchar(MAX)   
LASTNAME        nvarchar(MAX)   
USERSTATUS      nvarchar(MAX)   
EMPLOYEETYPE    nvarchar(MAX)   
MANAGERID       nvarchar(MAX)   
STATUS          nvarchar(MAX)   
WEEKNUM         nvarchar(MAX)   
Acc_Check       nvarchar(MAX)   
Policy_Name     nvarchar(MAX)   

Table b is created by using SELECT DISTINCT on table a without POLICYNAME. 通过对不带POLICYNAME的表a使用SELECT DISTINCT创建表b。

I now need to add the correct policyname back in to the new table. 现在,我需要将正确的策略名称重新添加到新表中。

Where Acc_Check is equal in both tables I want to take the matching value of a.POLICYNAME and insert it into b.Policy_Name 在两个表中Acc_Check相等的情况下,我想获取a.POLICYNAME的匹配值并将其插入到b.Policy_Name中

You just have to join these tables 您只需要加入这些表

UPDATE b
SET b.Policy_Name  = a.POLICYNAME      
FROM b INNER JOIN a
ON b.Acc_Check = a.Acc_Check       

暂无
暂无

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

相关问题 Postgres将数据从一个表插入到另一列名称匹配的表 - Postgres insert data from one table to another where column names match sql查询从审计表中获取数据,其中列匹配一个值,但不匹配另一个 - sql query to get data from an audit table where column match one value, but not another 一个表中的SQL副本插入另一个表中-使用where子句 - SQL copy from one table insert into another - using a where clause 一个表中的SQL sum列,其中另一表中的值为“ S” - SQL sum column in one table where value in another table is 'S' SQL - 一个表的行是另一个表的列标题的位置 - SQL - Pivoting where the rows of one table are the column titles of another 从另一个表中 ID 不匹配的列更新一个表中列的值 - Update value of column in one table from a column in another table where ID's do not match SQL从一个表列中选择一个最小值,然后将结果插入一个SQL语句的另一个表列中 - Sql Select a minimum value from a table column and insert the results in another table column in one SQL statement 根据where条件将一个表中的一列的值插入到另一个表中 - Insert value of one column from one table to another table based on where condition SQL-是否可以从一列中选择所有不同的值,而另一列中的所有值都匹配? - SQL - Is it possible to select all Distinct Values from one column where all values from another column match? SQL - 仅在匹配的地方用另一个表列替换列值,否则保留原始值? - SQL - Replace column values with another table column ONLY where they match, else keep original value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM