Oracle Apex 中财政年度的 SQL 触发器
Posted
技术标签:
【中文标题】Oracle Apex 中财政年度的 SQL 触发器【英文标题】:SQL trigger for fiscal year in Oracle Apex 【发布时间】:2021-03-05 14:48:40 【问题描述】:我有这个代码来获取财政年度。
from
(SELECT
CASE EXTRACT (MONTH FROM SYSDATE)WHEN 1 THEN EXTRACT(YEAR FROM SYSDATE)-1
WHEN 2 THEN EXTRACT(YEAR FROM SYSDATE)-1
WHEN 3 THEN EXTRACT(YEAR FROM SYSDATE)-1
ELSE EXTRACT(YEAR FROM SYSDATE)
END
AS PR_YEAR ,
CASE EXTRACT (MONTH FROM SYSDATE)
WHEN 04 THEN 1
WHEN 05 THEN 1
WHEN 06 THEN 1
WHEN 07 THEN 1
WHEN 08 THEN 1
WHEN 09 THEN 1
WHEN 10 THEN 2
WHEN 11 THEN 2
WHEN 12 THEN 2
WHEN 01 THEN 2
WHEN 02 THEN 2
WHEN 03 THEN 2
END AS PR_MONTH
FROM dual)
我想使用触发器将之前的代码在 Apex 的 Oracle 数据库表名称 MM_Inventory 的列名称 Fiscaly 中插入的数据插入,我有它是这样的:
create or replace TRIGGER add_fiscaly
BEFORE INSERT ON MM_Inventory
FOR EACH ROW
BEGIN
SELECT PR_YEAR||PR_MONTH
from
(SELECT
CASE EXTRACT (MONTH FROM SYSDATE)WHEN 1 THEN EXTRACT(YEAR FROM SYSDATE)-1
WHEN 2 THEN EXTRACT(YEAR FROM SYSDATE)-1
WHEN 3 THEN EXTRACT(YEAR FROM SYSDATE)-1
ELSE EXTRACT(YEAR FROM SYSDATE)
END
AS PR_YEAR ,
CASE EXTRACT (MONTH FROM SYSDATE)
WHEN 04 THEN 1
WHEN 05 THEN 1
WHEN 06 THEN 1
WHEN 07 THEN 1
WHEN 08 THEN 1
WHEN 09 THEN 1
WHEN 10 THEN 2
WHEN 11 THEN 2
WHEN 12 THEN 2
WHEN 01 THEN 2
WHEN 02 THEN 2
WHEN 03 THEN 2
END AS PR_MONTH
FROM dual)
END;
但我不知道它有什么问题或遗漏,我会很感激你的帮助。
【问题讨论】:
【参考方案1】:这里的触发器中需要一个 INTO 子句:
SELECT PR_YEAR||PR_MONTH
INTO :NEW.Fiscaly
from ...
我相信这个简化的代码可以达到同样的效果:
create or replace TRIGGER add_fiscaly
BEFORE INSERT ON MM_Inventory
FOR EACH ROW
BEGIN
:NEW.Fiscaly := CASE WHEN EXTRACT (MONTH FROM SYSDATE) < 4
THEN EXTRACT(YEAR FROM SYSDATE)-1
ELSE EXTRACT(YEAR FROM SYSDATE)
END
|| CASE WHEN EXTRACT (MONTH FROM SYSDATE) BETWEEN 4 AND 9
THEN 1
ELSE 2
END;
END;
/
【讨论】:
谢谢,但现在我收到此消息:“第 31 行错误:PLS-00103:在预期以下情况之一时遇到符号“文件结尾”:”。该行是END AS PR_MONTH FROM dual)
您在FROM dual)
后面缺少一个分号来结束语句。
成功了,非常感谢。感谢您的帮助以上是关于Oracle Apex 中财政年度的 SQL 触发器的主要内容,如果未能解决你的问题,请参考以下文章