如何在插入Oracle/PLSQL之前修改Create Trigger中的:新变量的类型:

发布于 2025-01-28 15:14:53 字数 415 浏览 1 评论 0原文

我使用DataType int定义了表存储中的专栏年。数据在CSV内部。插入时,我以字符串的身份通过了这一年,因为它是一个数字或“ n/a”。我尝试声明触发器来纠正此问题,

应该执行以下操作: 如果插入的字符串为'n/a'插入null。否则插入字符串的值。

CREATE TRIGGER checkYear
BEFORE INSERT ON STORE
FOR EACH ROW BEGIN
    IF :new.YEAR = 'N/A' THEN
        :new.YEAR := NULL;
    ELSE
        :new.YEAR := CAST(:new.Year AS INT);
    END IF;
END;

当插入值时,我会在SQL开发人员内部从Chepyear(ORA-04098)中获得触发错误

I defined the column YEAR in my table STORE with datatype int. The data is inside a csv. When inserting i pass the year as a string, because it is either a number or 'N/A'. I tried declaring a trigger to correct this

It should do the following:
If the inserted string is 'N/A' Insert NULL. ELSE insert the value of the string.

CREATE TRIGGER checkYear
BEFORE INSERT ON STORE
FOR EACH ROW BEGIN
    IF :new.YEAR = 'N/A' THEN
        :new.YEAR := NULL;
    ELSE
        :new.YEAR := CAST(:new.Year AS INT);
    END IF;
END;

When inserting values i get a trigger error from checkYear (ORA-04098) inside SQL Developer compiling the trigger gives PLS-00382

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

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

发布评论

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

评论(2

眼趣 2025-02-04 15:14:53

数据类型:new.year变量将始终始终与表中的列的数据类型匹配。在将它们放在SQL语句中之前,您必须将字符串过滤或解析为整数

The data type of the :new.year variable will always match the data type of the column in the table. You must filter or parse your strings into integers before placing them in the SQL statement.

标点 2025-02-04 15:14:53

您只能使用 替换() 功能,以便在即将到来的年度价值等于n/a 时摆脱这种情况

CREATE OR REPLACE TRIGGER checkYear
  BEFORE INSERT ON store
  FOR EACH ROW
BEGIN
  :new.year := REPLACE(:new.year,'N/A');
END;
/

。 (如果是一年的字面意义,可以转换为数字含量,例如'2022')无需明确的铸造数值,该数字值已被DB隐含地发现。

btw

  • 您可以更喜欢将其添加到 insert 语句中,例如

     插入商店(...,年,...)value(...替换(年,'n/a'),...),...)
     

    而不是创建触发器。

  • 您可以直接在控制文件中施放,例如
    ... 
    year  CHAR(4000) "REPLACE(:year,'N/A')" 
    ...  

您的导入工具为 sql Loader

You just can use a REPLACE() function in order to get rid of the case whenever upcoming year value equals to N/A such as

CREATE OR REPLACE TRIGGER checkYear
  BEFORE INSERT ON store
  FOR EACH ROW
BEGIN
  :new.year := REPLACE(:new.year,'N/A');
END;
/

Otherwise(if it's a year literal which can be convertible to a numeric one such as '2022' ) no need an explicit casting to a numeric value which's already found out implicitly by the DB.

Btw,

  • you can prefer to adding that to your Insert statement such as

    INSERT INTO store(...,year,...) VALUES(...,REPLACE(year,'N/A'),...)
    

    rather than creating a trigger.

or

  • you can cast directly within the control file such as
    ... 
    year  CHAR(4000) "REPLACE(:year,'N/A')" 
    ...  

if your importing tool is SQL loader.

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