SQL Select查询返回外键值并更新原始值

时间:2018-10-18 15:39:09

标签: sql-server

我对MS SQL有点陌生,但是我已经能够弄清楚如何获取外键以通过查询返回,但是我无法弄清楚如何使它更新原始值。

这是我到目前为止所拥有的:

SELECT prd_pk, prd_alt_desc
FROM RM_AM.dbo.product
WHERE EXISTS (SELECT prd_pk FROM dbo.Bn_Import WHERE prd_alt_desc = dbo.Bn_Import.Commodity);

我认为我需要一个变量来完成此操作,但是我对如何构造查询的了解还不够。

+------------+------------------+-------------------------+------------------+-----------------+-------------+
| Ship Date  |   Original ETA   | Confirmed Delivery Date |   Current ETA    |    Commodity    | Load Status |
+------------+------------------+-------------------------+------------------+-----------------+-------------+
| 10/9/2018  | 10/16/2018 11:48 | 10/19/2018              |                  | SANDFRAC4070MES | L           |
| 10/1/2018  | 10/1/2018 18:49  | 10/4/2018               | 10/18/2018 10:28 | SANDFRAC4070MES | L           |
| 10/11/2018 | 10/18/2018 11:48 | 10/20/2018              |                  | SANDFRAC4070MES | L           |
| 10/9/2018  | 10/16/2018 11:48 | 10/19/2018              |                  | SANDFRAC4070MES | L           |
| 10/1/2018  | 10/1/2018 18:49  | 10/4/2018               | 10/18/2018 10:28 | SANDFRAC4070MES | L           |
| 10/10/2018 | 10/17/2018 11:48 | 10/20/2018              |                  | SANDFRAC4070MES | L           |
| 10/16/2018 | 10/19/2018 6:00  | 10/21/2018              | 10/19/2018 1:04  |                 | L           |
| 10/9/2018  | 10/16/2018 11:48 | 10/19/2018              |                  | SANDFRAC4070MES | L           |
| 10/4/2018  | 10/11/2018 11:48 | 10/13/2018              |                  | SANDFRAC4070MES | L           |
| 10/16/2018 | 10/19/2018 6:00  | 10/21/2018              | 10/19/2018 1:04  |                 | L           |
| 9/17/2018  | 9/17/2018 18:49  | 9/21/2018               |                  | SANDFRAC4070MES | L           |
| 10/1/2018  | 10/1/2018 18:49  | 10/4/2018               | 10/18/2018 10:28 | SANDFRAC4070MES | L           |
| 10/9/2018  | 10/16/2018 11:48 | 10/18/2018              |                  | SANDFRAC4070MES | L           |
| 10/9/2018  | 10/16/2018 11:48 | 10/19/2018              |                  | SANDFRAC4070MES | L           |
| 10/15/2018 | 10/18/2018 6:00  | 10/21/2018              | 10/19/2018 4:24  |                 | L           |
| 10/4/2018  | 10/11/2018 11:48 | 10/13/2018              |                  |                 | L           |
| 10/1/2018  | 10/1/2018 18:49  | 10/4/2018               | 10/18/2018 10:28 | SANDFRAC4070MES | L           |
| 10/16/2018 | 10/19/2018 6:00  | 10/21/2018              | 10/19/2018 1:04  |                 | L           |
| 10/6/2018  | 10/13/2018 11:48 | 10/16/2018              |                  | SANDFRAC4070MES | L           |
| 10/11/2018 | 10/18/2018 11:48 | 10/20/2018              | 10/19/2018 0:02  | SANDFRAC4070MES | L           |
| 10/9/2018  | 10/16/2018 11:48 | 10/19/2018              |                  | SANDFRAC4070MES | L           |
| 10/4/2018  | 10/11/2018 11:48 | 10/14/2018              |                  | SANDFRAC4070MES | L           |
| 10/1/2018  | 10/1/2018 18:49  | 10/4/2018               | 10/18/2018 10:28 | SANDFRAC4070MES | L           |
| 10/9/2018  | 10/16/2018 11:48 | 10/19/2018              |                  | SANDFRAC4070MES | L           |
| 10/9/2018  | 10/16/2018 11:48 | 10/19/2018              |                  | SANDFRAC4070MES | L           |
| 10/4/2018  | 10/11/2018 11:48 | 10/14/2018              |                  | SANDFRAC4070MES | L           |
| 10/9/2018  | 10/16/2018 11:48 | 10/18/2018              |                  | SANDFRAC4070MES | L           |
| 9/14/2018  | 9/22/2018 19:50  | 9/26/2018               | 10/18/2018 14:01 | SANDFRAC4070MES | L           |
| 10/4/2018  | 10/11/2018 11:48 | 10/13/2018              |                  | SANDFRAC4070MES | L           |
| 10/7/2018  | 10/14/2018 11:48 | 10/18/2018              |                  |                 | L           |
| 10/9/2018  | 10/16/2018 11:48 | 10/19/2018              |                  | SANDFRAC4070MES | L           |
| 10/1/2018  | 10/1/2018 18:49  | 10/4/2018               | 10/18/2018 10:28 | SANDFRAC4070MES | L           |
| 9/17/2018  | 9/17/2018 18:49  | 9/21/2018               |                  | SANDFRAC4070MES | L           |
+------------+------------------+-------------------------+------------------+-----------------+-------------+

我需要将商品从SANDFRAC4070MES更改为6710

6710是数据库中另一个表的外键。

0 个答案:

没有答案