简体   繁体   English

在Oracle中,当插入表时如何处理NULL值

[英]In Oracle, How to handle NULL values when inserted to a Table

I have one table, which has a filed. 我有一张桌子,有一张桌子。 When I insert in that table with the Empty value like '' or null, it should get converted to 'DUMMY-VALUE'. 当我在表中插入“''或null之类的Empty值时,它应该转换为“ DUMMY-VALUE”。

--Have one table;
CREATE TABLE TEST  ( FIELD1 VARCHAR2(50) );

--When I insert ''
INSERT INTO TEST(FIELD1) VALUES('');

SELECT * FROM TEST
--Expected Result 'DUMMY-VALUE' but not NULL

I can apply NVL('','DUMMY-VALUE') in INSERT statement but I am allowed to change CREATE statement only. 我可以在INSERT语句中应用NVL('','DUMMY-VALUE') ,但只允许更改CREATE语句。 For Now, I am handing this with TRIGGER but , wondering if there is any alternative in 11g however I know about DEFAULT ON NULL for oracle 12c. 现在,我将使用TRIGGER处理,但是,我想知道11g中是否还有其他选择,但是我知道oracle 12c的DEFAULT ON NULL

You can do like this: 您可以这样:

create table TEST  (FIELD1 VARCHAR2(50) default 'DUMMY-VALUE'  );

and when you want to insert you should insert without that field if the values is NULL or empty 当您要插入时,如果值为NULL或为空,则应插入不带该字段的字段

Try this: 尝试这个:

CREATE TABLE TEST (FIELD1 VARCHAR2(50) DEFAULT 'DUMMY-VALUE');

then use the DEFAULT keyword in the INSERT statement: 然后在INSERT语句中使用DEFAULT关键字:

INSERT INTO TEST (FIELD1) VALUES (DEFAULT);

SQLFiddle here SQLFiddle在这里

Share and enjoy. 分享并享受。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM