简体   繁体   English

从非唯一标识符将列信息从一个表导入到另一个表

[英]Import Column info from one table to another from non-unique identifier

I have two tables, Table A which holds the information I want to pass and Table BI want to accept the information. 我有两个表,表A包含要传递的信息,表BI希望接受该信息。 (19 columns of varchar). (19列varchar)。

The only column they both hold common is "ItemTitle" (Don't ask, I didn't create it) So I am trying to import based on ItemTitle matching. 它们共同的唯一列是“ ItemTitle”(不要问,我没有创建它),因此我试图基于ItemTitle匹配进行导入。 My problem is in Table A the ItemTitle is unique but on Table B there can be many with the same name. 我的问题是表A中的ItemTitle是唯一的,但表B上可能有许多同名。

I want to import the column info to all rows that hold the same ItemTitle not just the first one it finds. 我想将列信息导入所有具有相同ItemTitle的行,而不仅仅是它找到的第一个行。

Is it possible to do this? 是否有可能做到这一点?

This is what I tried: (I only used the set for 1 column to see if it worked) It didn't. 这是我尝试的方法:(我只使用1列的集合来查看它是否起作用),但没有。 Error Msg 4104, Level 16, State 1, Line 29 The multi-part identifier "prodData.ItemTitle" could not be bound. 错误消息4104,级别16,状态1,行29无法绑定多部分标识符“ prodData.ItemTitle”。

SELECT prodData.[prodID]
      ,prodData.[Item]
      ,prodData.[ParentItem]
      ,prodData.[ItemTitle]
      ,prodData.[15]
      ,prodData.[16]
      ,prodData.[17]
      ,prodData.[18]
      ,prodData.[19]
      ,prodData.[20]
      ,prodData.[21]
      ,prodData.[22]
      ,prodData.[23]
      ,prodData.[24]
      ,prodData.[25]
      ,prodData.[26]
      ,prodData.[27]
      ,prodData.[28]
      ,prodData.[29]
      ,prodData.[30]
      ,prodData.[31]
      ,prodData.[32]
      ,prodData.[33]
    From [database].[dbo].[tbl_ProductInfoDump] as prodData
    Inner Join [database].[dbo].[tbl_ProductFilterDump] as filterData 
    on filterData.ItemTitle = prodData.ItemTitle


Update [database].[dbo].[tbl_ProductInfoDump] set [15] = (Select [15] from [database].[dbo].[tbl_ProductFilterDump] as filterData where filterData.ItemTitle = prodData.ItemTitle)

You must use an update with join, or update from select (whichever you want to name it). 您必须使用带有join的更新,或者使用select进行更新(无论您想为其命名)。

The idea is to join the destination table (B) with the origin table (A), and make the update taken for each row of B the values of the columns from the joined A row. 想法是将目标表(B)与原始表(A)联接在一起,并使B的每一行进行的更新都是联接的A行中的列的值。

You can fidn a good explanation here: SQL SERVER – UPDATE From SELECT Statement – Using JOIN in UPDATE Statement – Multiple Tables in Update Statement . 您可以在此处找到一个很好的解释: SQL SERVER –从SELECT语句进行更新–在UPDATE语句中使用JOIN –在Update语句中有多个表 The query looks like this: 查询如下所示:

UPDATE B
SET
   Col1 = A.Col1,
   Col2 = A.Col2
FROM B
INNER JOIN A ON A.Title = B.Title

Of course, you can include additional filters ( where clause) to limit the updated rows. 当然,您可以包括其他过滤器( where子句)以限制更新的行。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM