简体   繁体   English

使用来自另一个表的外键将数据插入或更新到表中

[英]Insert or Update Data into Table with Foreign Key from Another Table

Have two tables 有两张桌子

Table VendorMaster
|VendorID|Vendor|
|    1   |Vendor1|
|    2   |Vendor2|

Table Data
|DataID|VendorID|ProductName|Quantity|
|   1  |    1   |  Product1 |   100  |
|   2  |    1   |  Product2 |   200  |
|   3  |    2   |  Product1 |   400  |
|   4  |    2   |  Product2 |   100  |

A vendor can have multiple products which are referred using foreign key Now I have another table used for staging the data 一个供应商可以有多个使用外键引用的产品现在,我还有另一个用于暂存数据的表

Table Staging
|StagingID|Vendor|ProductName|Quantity|

I need to insert or update the data into the Data table and Vendor table from the Staging table. 我需要将数据从暂存表插入或更新到数据表和供应商表中。 Like the staging table can be used to update only the quantities or add new vendor and / or product. 像登台表一样,可用于仅更新数量或添加新的供应商和/或产品。

How do I proceed with it? 我该如何进行呢? I am using SQL Server. 我正在使用SQL Server。

I tried using this , but it cannot update / insert the foreign key master table. 我尝试使用此方法 ,但是它无法更新/插入外键主表。

You need to write below code to insert & update respective tables and I am assuming that VendorID & DataID columns are identity columns. 您需要编写以下代码来插入和更新相应的表,并且我假设VendorID和DataID列是标识列。

insert into VendorMaster (Vendor)
select Vendor
from Staging s
where not exists (
select 1 from  VendorMaster v 
where v.vendor = s.vendor
);


Update d
set Quantitty = s.Quantity
from Data d
inner join venderMaster v on d.vendorid = v.vendorid
inner join staging s on s.vendor = v.vendor 
and s.productName = d.productname
and d.Quantity <> s.Quantity;

insert into Data (VendorID,ProductName,Quantity)
select VendorID,ProductName,Quatity
from Vendor v
inner join staging s on s.Vendor = v.Vendor 
where not exists
(
 select 1 from Data d 
where d.vendorid = v.vendorid
and s.productName = d.productname
);

You can do this in 2 steps in this particular order: 您可以按照以下特定顺序分两个步骤进行操作:

  1. Keep the VendorMaster table up to date with new vendors. 使VendorMaster表与新供应商保持最新。 I'm assuming that the vendor name is your key here (it shouldn't be the name though, what happens if you have 2 different vendors with same name?) and VendorID is an IDENTITY . 我假设供应商名称是您的密钥(尽管它不应该是名称,如果您有两个名称相同的不同供应商会发生什么?),并且VendorIDIDENTITY

     INSERT INTO VendorMaster ( Vendor) SELECT DISTINCT S.Vendor FROM Staging AS S WHERE NOT EXISTS (SELECT 'vendor not yet loaded' FROM VendorMaster AS V WHERE V.Vendor = S.Vendor) 
  2. Update (if exists) & Insert (if not exists) the data from each vendor. 更新(如果存在)并插入(如果不存在)来自每个供应商的数据。 I'm assuming the key on this table will be VendorID and ProductName (it shouldn't be product name, it should be ProductID , or the such), and that DataID is IDENTITY . 我假设此表上的键将是VendorIDProductName (它不应该是产品名称,它应该是ProductID或类似的名称),而DataIDIDENTITY The operations must be in this particular order to prevent pointless updating. 操作必须按照此特定顺序进行,以防止无意义的更新。 The update won't update if it doesn't exist and the insert won't insert if it already exist: 如果更新不存在,则更新不会更新;如果已经存在,则插入将不会插入:

     UPDATE D SET Quantity = S.Quantity FROM Staging AS S INNER JOIN VendorMaster AS V ON S.Vendor = V.Vendor -- Fetch the VendorID from VendorMaster INNER JOIN Data AS D ON V.VendorID = D.VendorID AND S.ProductName = D.ProductName WHERE S.Quantity <> D.Quantity -- Update only if there are differences INSERT INTO Data ( VendorID, ProductName, Quantity) SELECT VendorID = V.VendorID, ProductName = S.ProductName, Quantity = S.Quantity FROM Staging AS S INNER JOIN VendorMaster AS V ON S.Vendor = V.Vendor -- Fetch the VendorID from VendorMaster WHERE NOT EXISTS ( SELECT 'data not yet loaded' FROM Data AS D WHERE V.VendorID = D.VendorID AND S.ProductName = D.ProductName) 

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

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