【问题标题】:Stored procedure in Oracle with Case and WhenOracle中的存储过程与案例和时间
【发布时间】:2023-03-07 09:37:01
【问题描述】:

我有一个场景,我有 5 个不同的表:

Table 1 - Product, Columns - ProductId, BatchNummer, Status, GroupId, OrderNummer

Table 2 - ProductGrop, Columns - GropId, ProductType, Description

Table 3 - Electronics, Columns - EId, Description, BatchNummer, OrderNummer, OrderData

Table 4 - Manual, Columns - MId, Description, Status, OrderNummer, ProcessStep

Table 5 - ProcessedProduct, columns same as Product with one extra column of datetime

现在,根据业务流程,我需要从 Product 表中填充所有数据,并且必须检查基础表(电子或手动,取决于 ProductGoup 的 ProductType 列)是否具有 ordernuumer 值,然后插入一条记录在表 5“ProcessedProduct”中,否则跳过记录。

为了这个要求,我想创建一个程序。但是我被困在如何检查我必须参考哪个基础表(电子/手册)以及如何实现它。

此外,我应该如何编写插入记录的循环。

注意:我无法更改表架构。

【问题讨论】:

  • 首先,你能在这里发帖吗?你试过什么?

标签: stored-procedures oracle11g


【解决方案1】:

使用 PL/SQL 过程,您可以在 LOOP 内切换,但如果您只需要检查 OrderNummer 是属于电子产品还是手册,则不需要命令式算法。

假设通过 ProductType 值“Electronics”或“Manuals”选择明细表,您可以:

INSERT INTO ProcessedProduct (ProductId, BatchNummer, Status, GroupId, OrderNummer, TS)
SELECT ProductId, BatchNummer, Status, GroupId, OrderNummer, SYSDATE
FROM Product p 
INNER JOIN ProductGroup pg USING (GroupId)
WHERE EXISTS (
  SELECT NULL FROM Electronics e
  WHERE p.OrderNummer = e.OrderNummer
  AND pg.ProductType = 'Electronics'
  UNION
  SELECT NULL FROM Manuals m
  WHERE m.OrderNummer = m.OrderNummer 
  AND pg.ProductType = 'Manuals')

普通 SQL 总是最快的方式,而“WHERE EXISTS”通常是最快的条件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 2011-10-29
    相关资源
    最近更新 更多