更新过程中的每一行

发布于 2024-08-10 21:56:28 字数 415 浏览 4 评论 0原文

我在表 TABLE1

DOCUMENT ------ FIELD1
中有以下数据 12345
23456
34567
45678
98765

我在视图 VIEW1

DOCUMENT ---- BUS 中有以下数据
12345 ------------ 5
23456 ------------ 6
34567 ------------ 8
45678 ------------ 12
98765 ------------ 14

我想做的是更新每一行

if (table1.document = view1.document)
然后 table1.field1 = view1.bus

任何见解都会有帮助。

谢谢。

I have the following data in a table TABLE1

DOCUMENT ------ FIELD1
12345
23456
34567
45678
98765

i have the following data in a view VIEW1

DOCUMENT ---- BUS
12345 ------------ 5
23456 ------------ 6
34567 ------------ 8
45678 ------------ 12
98765 ------------ 14

What i would like to do is update each row

if (table1.document = view1.document)
then
table1.field1 = view1.bus

Any insight will help.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁客 2024-08-17 21:56:28

这可以使用纯 SQL 来完成,无需任何过程:

UPDATE table1 SET field1 = (SELECT bus FROM view1 WHERE table1.document = view1.document)

或者,如果您的数据库允许:

UPDATE (select table1.field1, view1.bus FROM table1 JOIN view1 ON table1.document = view1.document) SET table1.field1 = view1.bus

That can be done using plain SQL, no procedures required:

UPDATE table1 SET field1 = (SELECT bus FROM view1 WHERE table1.document = view1.document)

Or, if your database allows it:

UPDATE (select table1.field1, view1.bus FROM table1 JOIN view1 ON table1.document = view1.document) SET table1.field1 = view1.bus
嘿嘿嘿 2024-08-17 21:56:28

正如 Dan 所说,但在 MS SQL Server 中,我发现这种样式更易于阅读:

UPDATE U
SET U.field1 = V.bus 
FROM table1 AS U
    JOIN view1 AS V
       ON V.document = U.document

请注意,如果 VIEW1 对于给定的 TABLE1 行 [DOCUMENT] 值可以有多个行,那么选择更新 TABLE1 的 [BUS] 值将是随机的,在匹配集。 (如果是这种情况,可以修改查询以选择 MAX / MIN / 等。)

我将优化此查询,以不更新任何已与 BUS 值匹配的行,这将使其在重新运行时更快,从而使某些值更快 如果该字段已存在于 TABLE1 中,

UPDATE U
SET U.field1 = V.bus 
FROM table1 AS U
    JOIN view1 AS V
       ON V.document = U.document
WHERE    U.field1 = V.bus
      OR (U.field1 IS NOT NULL AND V.bus IS NULL)
      OR (U.field1 IS NULL AND V.bus IS NOT NULL)

则如果该字段定义为不允许 NULL,则可以省略 NULL / NOT NULL 测试。

As Dan said, but in MS SQL Server I find this styling easier to read:

UPDATE U
SET U.field1 = V.bus 
FROM table1 AS U
    JOIN view1 AS V
       ON V.document = U.document

Note that if VIEW1 could have multiple rows for a given TABLE1 row [DOCUMENT] value then the [BUS] value choosen to update TABLE1 will be random, within the matching set. (If this is the case the query could be modified to choose MAX / MIN / etc.)

I would refine this query to NOT update any rows that already matched the BUS value, which will make it faster if it is rerun and thus some values already exist in TABLE1

UPDATE U
SET U.field1 = V.bus 
FROM table1 AS U
    JOIN view1 AS V
       ON V.document = U.document
WHERE    U.field1 = V.bus
      OR (U.field1 IS NOT NULL AND V.bus IS NULL)
      OR (U.field1 IS NULL AND V.bus IS NOT NULL)

you can leave out the NULL / NOT NULL tests if the field is defined as not allowing NULLs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文