删除后的 T-SQL 触发器

发布于 2024-10-20 01:26:29 字数 654 浏览 2 评论 0原文

情况:假设您有一个组织、学生和课程购买的数据库,并且管理员想要获得他们分配的课程的退款。软件默认不支持退款。您无权访问源代码,但可以在数据库中设置触发器。

数据库结构:

  • organizations
    • organization_id
    • account_balance
  • Students
    • student_id
    • organization_id
    • < /ul>

一般的想法是,当一个条目(或超过一个!)从购买表中删除后,将运行触发器来更新相应组织的帐户余额amount_to_refund 可以为空或零,因此在这种情况下不会退款。如果购买时间超过 30 天前,也不应退款。

有什么想法如何实现这一目标吗?我一直在使用另一个触发器对其进行建模,但是被 UPDATE ... FROM ... 语法迷惑了,我不能说我以前使用过该语法。我看了MSDN,但我有点不知所措。

或者,我还想将行插入另一个包含退款金额和组织 ID 的表(此处未记录)。我只需要一个大概的想法,这个想法适合什么,并且可以自己处理其余的事情。

Situation: Let's say you have a database of organizations, students, and course purchases, and an administrator wants to get a refund for a course they assigned out. The software doesn't support refunds by default. You don't have access to the source code, but you can set up triggers in the database.

Database Structure:

  • organizations
    • organization_id
    • account_balance
  • students
    • student_id
    • organization_id
  • purchases
    • student_id
    • time_of_purchase
    • amount_to_refund

General idea is that, when an entry (or more than one!) is removed from the purchases table, a trigger runs to update the account balance of the appropriate organization. amount_to_refund can be null or zero, so no refund is given in that case. Refunds should also not be given if time_of_purchase was more than 30 days ago.

Any ideas how to pull this off? I've been modeling it off of another trigger, but am getting thrown off by the UPDATE ... FROM ... syntax, which I can't say I've used before. I have looked at MSDN, but I'm a bit overwhelmed.

Optionally, I'd also like to insert rows into another table (not documented here) containing the refund amount and organization ID. I just need a general idea where this fits in and can probably handle the rest myself.

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

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

发布评论

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

评论(1

梦初启 2024-10-27 01:26:29
CREATE TRIGGER [dbo].[TrgPurchasesDelete] ON [dbo].[Purchases] FOR DELETE
AS
BEGIN
    UPDATE
        [dbo].[Organizations]
    SET
        account_balance = account_balance + DLTD.ammount_to_refund
    FROM
        [dbo].[Organizations] ORGA
        INNER JOIN [dbo].[Students] STDT ON
            STDT.organization_id = ORGA.organization_id
        INNER JOIN DELETED DLTD ON
            DLTD.student_id = STDT.student_id
    WHERE
        DLTD.ammount_to_refund > 0
        AND DLTD.time_of_purchase > DATEADD(DAY, -30, SYSDATE)
END
GO            
CREATE TRIGGER [dbo].[TrgPurchasesDelete] ON [dbo].[Purchases] FOR DELETE
AS
BEGIN
    UPDATE
        [dbo].[Organizations]
    SET
        account_balance = account_balance + DLTD.ammount_to_refund
    FROM
        [dbo].[Organizations] ORGA
        INNER JOIN [dbo].[Students] STDT ON
            STDT.organization_id = ORGA.organization_id
        INNER JOIN DELETED DLTD ON
            DLTD.student_id = STDT.student_id
    WHERE
        DLTD.ammount_to_refund > 0
        AND DLTD.time_of_purchase > DATEADD(DAY, -30, SYSDATE)
END
GO            
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文