如何在插入Oracle/PLSQL之前修改Create Trigger中的:新变量的类型:
我使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数据类型: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.
您只能使用
替换()
功能,以便在即将到来的年度价值等于n/a 时摆脱这种情况
。 (如果是一年的字面意义,可以转换为数字含量,例如
'2022'
)无需明确的铸造数值,该数字值已被DB隐含地发现。btw ,
您可以更喜欢将其添加到 insert 语句中,例如
而不是创建触发器。
或
您的导入工具为 sql Loader 。
You just can use a
REPLACE()
function in order to get rid of the case whenever upcoming year value equals toN/A
such asOtherwise(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
rather than creating a trigger.
or
if your importing tool is SQL loader.