删除后的 T-SQL 触发器
情况:假设您有一个组织、学生和课程购买的数据库,并且管理员想要获得他们分配的课程的退款。软件默认不支持退款。您无权访问源代码,但可以在数据库中设置触发器。
数据库结构:
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)