sql server 2005中的更新语句

发布于 2024-08-18 12:47:16 字数 659 浏览 1 评论 0原文

考虑以下 Dig,假设所有三个表都有一个列 Is_Deleted,默认设置为 0...我想更新 Is_Deleted=1< Customers 表的 /strong> 字段,其中 CustId=2 仅当行包含 CustId=2Is_Deleted=1 时> 在订单OrderItems中 表格...我不想使用Cascade选项。任何建议

替代文本
(来源:microsoft.com)

Consider the following Dig, Assume that all the three tables have a column Is_Deleted by default it is set to 0... I want to update Is_Deleted=1 field of Customers table where CustId=2 only when the rows containing CustId=2 and Is_Deleted=1 in Orders and OrderItems
Tables... I dont want to use Cascade option.. Any suggestion

alt text
(source: microsoft.com)

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

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

发布评论

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

评论(3

泼猴你往哪里跑 2024-08-25 12:47:16

最简单的方法是存在。我假设您想检查 Orders 和 OrderItems。这也意味着您只需过滤 CustID 一次。

UPDATE
   C
SET
   IsDeleted = 1
FROM
   Customers C
WHERE
   C.CustID = 2
   AND
   EXISTS (SELECT *
        FROM
            Orders O
        WHERE
            O.CustID = C.CustID AND O.IsDeleted = 1)
   AND
   EXISTS (SELECT *
        FROM
            Orders O
            JOIN
            OrderItems OI ON O.OrderID = OI.OrderID
        WHERE
            O.CustID = C.CustID AND OI.IsDeleted = 1)

Easiest way is EXISTS. I assume you want to check both Orders and OrderItems. This also means you only filter on CustID once.

UPDATE
   C
SET
   IsDeleted = 1
FROM
   Customers C
WHERE
   C.CustID = 2
   AND
   EXISTS (SELECT *
        FROM
            Orders O
        WHERE
            O.CustID = C.CustID AND O.IsDeleted = 1)
   AND
   EXISTS (SELECT *
        FROM
            Orders O
            JOIN
            OrderItems OI ON O.OrderID = OI.OrderID
        WHERE
            O.CustID = C.CustID AND OI.IsDeleted = 1)
一影成城 2024-08-25 12:47:16

好吧,听起来不错...
我们是仅在删除与该客户关联的所有订单和所有订单商品时设置删除标志,还是仅在删除至少 1 个商品时设置删除标志。

Ok sounds fine...
Do we only set the deleted flag when all the orders and all the order items associated with that customer are deleted, or only if at least 1 item is deleted.

黯淡〆 2024-08-25 12:47:16

您可以使用表上的触发器 - 客户

您可以获得有关触发器的详细信息:

触发您可以检查 Updated 列的值,根据它您可以更新不同表的数据。

You can make the use of Triggers on Table - Customers

You can get details about triggers :

With Trigger you can check the Value of Updated column and depending on it you can update the data of different tables.

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