簡體   English   中英

如何使用匹配的列基於另一列的ID更新列

[英]How to update a column based on the id of another column using matching columns

標題令人困惑,所以讓我解釋一下,我有三個表需要使用。

  • 第一個表稱為VendorCertifications ,具有CertID, Cert, VendorID列。
  • 第二個表是Vendors ,具有VendorIDVendor
  • 第三個表是VendorCert並具有CertCompany

我所做的是將VendorCert插入VendorCertification ,現在我有了證書列表,並且現在使用身份規范填寫了PK CertID

我想要做的是要回去和更新VendorCertification與表VendorIDVendors

我想我可以加入Vendors Vendor與列VendorCerts Company列。

然后使用CTE更新VendorCertification VendorID列。

這是我寫的:

with temptable as (
   select 
       vce.Company, v.Vendor, vce.Certification, V.VendorID
   from 
      VendorCert as VCE
   join 
      Vendors as V on V.Vendor = VCE.Company)
update VendorCertifications
set VendorID = temptable.VendorID
where temptable.Certification = Certification

這沒有用,我收到“無法綁定”錯誤,我試圖弄清楚為什么它給了我,但是我沒有運氣。 我什至不確定是否應該以這種方式進行操作,我想不出一種使用常規更新語句進行處理的方法,因為它涉及(至少我認為涉及)三個表。 任何幫助表示贊賞。 認證列表大約有300個,供應商列表大約有40個。

請嘗試以下方法:

with temptable (Company,Vendor,Certification,VendorID)
as(
select vce.Company,v.Vendor,vce.Certification,V.VendorID
from VendorCert as VCE
join Vendors as V
on V.Vendor=VCE.Company)

update VC 
set VC.VendorID=TT.VendorID
from VendorCertifications VC join temptable TT on VC.Certification=TT.Certification

您已經完成了初始插入,但是如果沒有完成,則可能已經使用OUTPUT子句返回了剛剛插入的ID,因此可以使用它們將信息放入子表中。 JUST添加此操作后,您將獲得將來執行此類任務的想法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM