本文介绍了为什么包将null插入非空列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个没有空列和默认值的Oracle 11g表。启用非null约束,没有可以修改插入值的触发器(在dba_triggers中)。通过脚本中的insert命令将null插入此列是不可能的,但是在这种情况下,包中的相同插入会插入默认值。 我无法找到任何有关Google的帮助。可能是,有人知道是什么问题以及如何纠正它? 更多信息: 插入之一(这里是3) : INSERT INTO ks_salda_kont (id,ksk_id,instrument,numer_rejestru,saldo,obroty_d,obroty_k, saldo_energii,obroty_energ_d,obroty_energ_k,udtczas_ks,udtczas_sys, ksdo_id) VALUES (kssk_sq.nextval,iid,vInst,vRej,rr.kw_s,rr.kw_dt,rr.kw_kr,rr.en_s, rr.en_dt,rr.en_kr,dData ,dSys,iddo); 所有变量名称与列名称不同。错误的列是 numer_rejestru 。表创建语句是: CREATE 表 PMSKS。 KS_SALDA_KONT ( ID NUMBER( 17 , 0 ) NOT NULL ENABLE, KSK_ID NUMBER NOT NULL ENABLE, INSTRUMENT VARCHAR2( 130 ) DEFAULT ' XXX' NOT NULL ENABLE, NUMER_REJESTRU VARCHAR2( 100 ) DEFAULT ' XXX' NOT NULL ENABLE, SALDO NUMBER( 17 , 2 ) DEFAULT 0 NOT NULL ENABLE, OBROTY_D NUMBER( 17 , 2 ) DEFAULT 0 NOT NU LL ENABLE, OBROTY_K NUMBER( 17 , 2 ) DEFAULT 0 NOT NULL ENABLE, OBROTY_ENERG_D NUMBER( 20 , 4 ) DEFAULT 0 NOT NULL ENABLE, OBROTY_ENERG_K NUMBER( 20 , 4 ) DEFAULT 0 NOT NULL ENABLE, SALDO_ENERGII NUMBER( 20 , 4 ) DEFAULT 0 NOT NULL ENABLE, BILANS_OTW_D NUMBER( 17 , 2 ) DEFAULT 0 NOT NULL ENABLE, BILANS_OTW_K NUMBER( 17 , 2 ) DEFAULT 0 NOT NULL ENABLE, UDTCZAS_KS DATE DEFAULT to_date(' 19000101',' yyyymmdd') NOT NULL ENABLE, UDTCZAS_SYS DATE DEFAULT to_date(' 19000101' ,' yyyymmdd') NOT NULL ENABLE, KSDO_ID NUMBER DEFAULT 0 N OT NULL ENABLE, CONSTRAINT KSSK_KSSK_2_UK UNIQUE ( KSK_ID, INSTRUMENT, NUMER_REJESTRU) 解决方案 看看这里: Oracle提示:如何在数据库列中使用默认值 [ ^ ] Quote:您必须至少指定一列,但您可以使用DEFAULT关键字允许使用默认值而不是硬编码值,因此以下是有效的语法,它将创建一个包含所有DEFAULT值的行。 所以,最后, INSERT 语句应如下所示: INSERT INTO TableName(< NumericField>,< FieldWithDefaultValue> ) VALUES ( 0 , DEFAULT ) 如果您是其他表中的addind值,请使用 COALESCE [ ^ ]函数,用默认值替换 NULL s。 INSERT INTO 表1( < Field_list>) VALUES (Table2.Field1, COALESC E (Table2.Field2, DEFAULT )) 但是,我认为 Table2 返回空字符串('')。 注意:未经测试,因为我没有Oracle数据库;) 正如JörgenAndersson建议的那样,脚本在这里: http://sqlfiddle.com/#!4/6e974/4 [ ^ ] 来自波兰的干杯;) 解决方法正常。但是,我并不高兴;没有解决方法它也有效。我的上一次测试有一个愚蠢的错误,错误结果也是错误的...可能是表已损坏。也许(另一种解决方案)使用我的包的人在错误后做出承诺。 对于JörgenAndersson和Maciej Los来说 I have an Oracle 11g table with not null column and default value. Not null constraint is enabled, there are no triggers (in dba_triggers) which could to modify inserted value. It's impossible to insert null into this column by insert command from script, but thesame insert from package inserts default value in such case.I can't find any help with Google. May be, someone knows what's the matter and how to correct it?More info:One of inserts(here're 3):INSERT INTO ks_salda_kont (id, ksk_id, instrument, numer_rejestru, saldo, obroty_d, obroty_k, saldo_energii, obroty_energ_d, obroty_energ_k, udtczas_ks, udtczas_sys, ksdo_id)VALUES (kssk_sq.nextval, iid, vInst, vRej, rr.kw_s, rr.kw_dt, rr.kw_kr, rr.en_s, rr.en_dt, rr.en_kr, dData, dSys, iddo);All variable names differ from column names. Erronous column is numer_rejestru. Table creation statement is:CREATE TABLE "PMSKS"."KS_SALDA_KONT" ("ID" NUMBER(17,0) NOT NULL ENABLE,"KSK_ID" NUMBER NOT NULL ENABLE,"INSTRUMENT" VARCHAR2(130) DEFAULT 'XXX' NOT NULL ENABLE,"NUMER_REJESTRU" VARCHAR2(100) DEFAULT 'XXX' NOT NULL ENABLE,"SALDO" NUMBER(17,2) DEFAULT 0 NOT NULL ENABLE,"OBROTY_D" NUMBER(17,2) DEFAULT 0 NOT NULL ENABLE,"OBROTY_K" NUMBER(17,2) DEFAULT 0 NOT NULL ENABLE,"OBROTY_ENERG_D" NUMBER(20,4) DEFAULT 0 NOT NULL ENABLE,"OBROTY_ENERG_K" NUMBER(20,4) DEFAULT 0 NOT NULL ENABLE,"SALDO_ENERGII" NUMBER(20,4) DEFAULT 0 NOT NULL ENABLE,"BILANS_OTW_D" NUMBER(17,2) DEFAULT 0 NOT NULL ENABLE,"BILANS_OTW_K" NUMBER(17,2) DEFAULT 0 NOT NULL ENABLE,"UDTCZAS_KS" DATE DEFAULT to_date('19000101','yyyymmdd') NOT NULL ENABLE,"UDTCZAS_SYS" DATE DEFAULT to_date('19000101','yyyymmdd') NOT NULL ENABLE,"KSDO_ID" NUMBER DEFAULT 0 NOT NULL ENABLE, CONSTRAINT "KSSK_KSSK_2_UK" UNIQUE ("KSK_ID", "INSTRUMENT", "NUMER_REJESTRU") 解决方案 Have a look here: Oracle Tip: How to use default values with database columns[^]Quote:You must specify at least one column, but you may use the DEFAULT keyword to allow the default value rather than hard-coding a value, so the following is valid syntax that will create a row with all DEFAULT values.So, finally, INSERT statement should looks like:INSERT INTO TableName (<NumericField>, <FieldWithDefaultValue>)VALUES(0, DEFAULT)In case, you are addind values from other table, use COALESCE[^] function, which replaces NULLs with default value.INSERT INTO Table1 (<Field_list>)VALUES(Table2.Field1, COALESCE(Table2.Field2,DEFAULT))But, i think that Table2 returns empty string ('').Note: Not tested, because i don't have Oracle database ;)As Jörgen Andersson suggested, the script is here: http://sqlfiddle.com/#!4/6e974/4[^]Cheers from Poland ;)Workaround works ok. But, I am not glad; without workaround it works too. My last test had a silly mistake, error results were erronous too ... May be the table was corrupted. Maybe (another solution) the person, who used my package, do commit after errors.Thx for Jörgen Andersson and Maciej Los 这篇关于为什么包将null插入非空列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-29 03:44