如果列值 id 与另一个 table(规范化形式)中的 ID 描述匹配,则在指定列中插入数据

Insert Data in specified column if column value id matches the description of the ID in another table (normalized form)

为了做到这一点(在标题描述中),很明显它已经是一个规范化的数据库,所以肯定会使用内部连接类型的检查。

假设我们有一个客户 table 和一个给定的 customer_ID 作为主键以及描述名字姓氏等和另一个 table(Customer_Sales_History) Customer_ID 列也是为了使其可搜索 query-wise。

更新: 我的困难是我需要在特定列中添加以 XML(肥皂响应)格式获取的特定数据,但通过 Customer_Name 和 Customer_Lastname 附加到特定 Customer_IDs查看。

如果 Customer_Name = "John" 且 Customer_Lastname = "Martin" 则使用值

填充销售代码 productquantity 和 productdescription

卷很大(数千行),因此 sql 可能必须作为另一个 table 的插入来填充,我对那种类型的 slqing 卷很陌生。

           Customer Table
Customer_ID | Customer_Name | Customer_Lastname 
1           |  John         | Martin
2           |  Jack         | White
3           |  Don          | Carrera

     Customer_Sales_History Table

Customer_ID | Sales_Code | ProductQuantity | ProductDescription
2           |     X      |        X        |         X

为了将给定数据插入 Customer_Sales_History Table 并以 Customer_ID 2 作为给定目标,我必须匹配 Customer_Name = Jack 的行和 Customer_Lastname = 白色并填充 Sales_Code Product_Quantity 和产品描述。

如果你声明customer_ID in customer table作为主键,customer_ID in Customer_Sales_History table作为外键,那么它会自动执行检查部分。 那么,为什么需要显式检查 customer_id?

Update ch
set ch.sales_code = ...,
    ch. ProductQuantity =...,
from  Customer_Sales_History ch join Customer c on ch. Customer_ID= c.Customer_ID
where ch.Customer_ID=2