需要帮助编写触发器

发布于 2024-10-17 00:51:22 字数 675 浏览 2 评论 0原文

我有三个表:

订单: orderid、valuedid、valuedesc

客户: customerid, cutomdesc

组: groupid, groupdesc

valueid - 客户或群组的 ID

valuesdesc - 必须填写适合插入的 valueid 描述来自客户取决于用户在客户端中选择的内容(客户或组)。

因此,当客户端发送 Orders 的插入查询时,它包含新订单的 orderidvaluedid。在客户端,我知道用户选择了什么:组或客户。

我需要什么:如果在 Orders 中插入新行,则插入 valuesdesc 中的客户或群组的 valuedid 对应的 valuesdesc > 栏目。

我有一个想法插入新的订单记录 valuesdesc ,其中包含键 - 假值来选择正确的字典(客户或组),但不幸的是,如何创建该触发器我现在不知道。

I have three tables:

Orders:
orderid, valuedid, valuesdesc

Customers:
customerid, cutomdesc

Groups:
groupid, groupdesc

valueid - id of a customer or a group

valuesdesc - must filled with appropriate for inserted valueid description from Customers or Groups depend on what (customer or group) user selected in the client.

So, when client send an insert query for Orders it consists orderid for new order and valuedid. And on a client side I know what user selected: group or customer.

What I need: if a new row inserted in Orders, corresponding valuesdesc for valuedid from Customers or Groups inserted in valuesdesc column.

I have an idea to insert with new order record valuesdesc which would contain key - fake value to pick the right dictionary (customers or groups), but how to create that trigger I unfortunately don't know for now.

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

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

发布评论

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

评论(1

⒈起吃苦の倖褔 2024-10-24 00:51:22

首先,我必须说这是一个非常不常见的设计。当您将记录插入 Orders 时,您可以使用 valuesdesc 来提示 valueid 引用的表。但一旦 valuesdesc 被触发器填充,valueid 基本上就失去了任何意义。也就是说,您没有告诉我们您有任何其他方法来区分对 Customers 的引用和对 Groups 的引用。因此,您也可以完全删除 valueid 并使用 valuesdesc 传递“字典”提示和该表中的引用。

除此之外,由于 valueid 可以引用多个表,因此您不能使用 外键对其进行约束。

无论如何,在您当前的设计中,您可以尝试以下操作:

CREATE TRIGGER Orders_UpdateValuesdesc
ON Orders
FOR INSERT
AS
UPDATE o
SET valuesdesc = COALESCE(c.customdesc, g.groupdesc)
FROM Orders o
  INNER JOIN inserted ON o.orderid = i.orderid
  LEFT JOIN Customers c ON i.valuesdesc = 'Customers'
    AND i.valueid = c.customerid
  LEFT JOIN Groups g ON i.valuesdesc = 'Groups'
    AND i.valueid = g.customerid

字符串 'Customers''Groups' 意味着字典说明符。您可以将它们替换为实际值。

触发器使用 LEFT JOIN 连接 CustomersGroups,然后使用 customdesc 或 < code>groupdesc,取决于哪一个不为空。

First, I must say this is a very uncommon design. When you are inserting a record into Orders, you use valuesdesc to hint at the table that valueid references. But as soon as valuesdesc gets filled by the trigger, valueid essentially loses any meaning. That is, you haven't reveal us you've got any other way to tell a reference to Customers from a reference to Groups. So, you could just as well drop valueid altogether and use valuesdesc to pass both the 'dictionary' hint and the reference within that table.

Other than that, because valueid can reference more than one table, you cannot use a foreign key constraint on it.

Anyway, in your present design you could try this:

CREATE TRIGGER Orders_UpdateValuesdesc
ON Orders
FOR INSERT
AS
UPDATE o
SET valuesdesc = COALESCE(c.customdesc, g.groupdesc)
FROM Orders o
  INNER JOIN inserted ON o.orderid = i.orderid
  LEFT JOIN Customers c ON i.valuesdesc = 'Customers'
    AND i.valueid = c.customerid
  LEFT JOIN Groups g ON i.valuesdesc = 'Groups'
    AND i.valueid = g.customerid

The strings 'Customers' and 'Groups' are meant as the dictionary specifiers. You replace them with the actual values.

The trigger uses LEFT JOIN to join both Customers and Groups, then fills Orders.valuesdesc with either customdesc or groupdesc, depending on which one is not null.

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