简体   繁体   English

ORA-01400无法插入空值...但我没有插入空值!

[英]ORA-01400 can't insert null value… but I am NOT inserting null value!

I am trying to insert data in an Oracle table by using ODP.NET from a C# application, but I am getting an ORA-01400 can't insert null value error for a column in which I am NOT inserting a null value. 我试图通过使用来自C#应用程序的ODP.NET在Oracle表中插入数据,但是我得到一个ORA-01400不能为我没有插入空值的列插入空值错误。

This is the stripped down version of the parametrized SQL command I am trying to execute. 这是我试图执行的参数化SQL命令的精简版本。 It is wrapped in an OracleCommand and executed with an invokation of ExecuteNonQuery : 它包含在OracleCommand并通过ExecuteNonQuery的调用执行:

declare c int; 
begin
   select count(*) into c from "Entradas" where "Id" = :Id and nvl("AppId", 0) = nvl(:AppId, 0);
   if c>0 then
       update "Entradas" set
         /*...a bunch of columns...*/,
         "VisitaLaboral" = :VisitaLaboral,
         /*...some more columns...*/
         where "Id" = :Id and nvl("AppId",0) = nvl(:AppId, 0); 
   else
       insert into "Entradas" (
         /*... a bunch of columns...*/,
         "VisitaLaboral",
         /*...some more columns...*/
         ) values (
         /*...a bunch of values...*/,
         :VisitaLaboral,
         /*...some more values...*/
       );
   end if;
end;

The row does not exist previously so it is the insert part of the command the one that is executed. 该行以前不存在,因此它是命令的insert部分,即执行的那一行。 Of course I have verified that all the column names and column value parameters are properly placed in the SQL text. 当然,我已经验证所有列名和列值参数都正确放在SQL文本中。

The problem is in the VisitaLaboral column. 问题出在VisitaLaboral专栏中。 It is of type NUMBER(1,0), it does not accept NULLs, and I am trying to insert a value of 0. This is what Visual Studio displays about the associated OracleParameter immediately before the command execution: 它的类型为NUMBER(1,0),它不接受NULL,我试图插入值0.这就是Visual Studio在命令执行之前立即显示关于相关OracleParameter

在此输入图像描述

However if I execute the command directly in Application Express (providing the values directly in the command text), it works fine and the row is inserted. 但是,如果我直接在Application Express中执行命令(直接在命令文本中提供值),它可以正常工作并插入行。

So, what is happening here? 那么,这里发生了什么? Is there a bug in the ODP.NET library, or am I doing something wrong? ODP.NET库中是否有错误,或者我做错了什么?

Additional information: 附加信息:

  • Database is Oracle 10g Express Release 10.2.0.1.0 数据库是Oracle 10g Express Release 10.2.0.1.0
  • Oracle.DataAccess.dll version is 4.112.1.2 Oracle.DataAccess.dll版本是4.112.1.2
  • Using Visual Studio 2010, targeting .NET framework 4.0 使用Visual Studio 2010,以.NET Framework 4.0为目标

Thank you in advance! 先感谢您!

UPDATE: 更新:

Everything works fine if I use the (deprecated) System.Data.OracleClient classes instead of ODP.NET. 如果我使用(已弃用的) System.Data.OracleClient类而不是ODP.NET,一切正常。

WTF, Oracle? WTF,甲骨文? No, really, WTF? 不,真的,WTF?

Your problem seems to be that you don't know what is actually happening in the database. 您的问题似乎是您不知道数据库中实际发生了什么。 The quickest solution will be to find out what happens and use that. 最快的解决方案是找出发生了什么并使用它。

  1. connect to the database 连接到数据库
  2. use dbms_session.set_sql_trace(true) to enable an sql trace 使用dbms_session.set_sql_trace(true)启用sql跟踪
  3. do your application action 做你的申请行动
  4. disconnect from the database 断开与数据库的连接
  5. find - or ask your dba - to send you the raw trace file 查找 - 或询问您的dba - 向您发送原始跟踪文件

In the tracefile (a plain text file) find your code and see what happens when the error is raised. 在tracefile(纯文本文件)中找到您的代码,看看引发错误时会发生什么。

It could be that a trigger fired .... 它可能是一个触发器......

This is a DB level error message, you can be sure that the insert has null value. 这是一个DB级别的错误消息,您可以确保该插入具有null值。 Is it possible to log out the sql statement generated by the ODP.NET? 是否可以注销ODP.NET生成的sql语句?

Are you really really sure that you have the columns in the same order on both the insert clause and the values clause? 您是否真的确定insert子句和values子句中的列具有相同的顺序? Check your "bunch of columns" ... 检查你的“一堆列”......

我对ODP.net一无所知,但如果参数中有值,那么precison,scale和size属性是否都为零(看起来如此)?

We had the same problem. 我们遇到了同样的问题。 We solved the problem setting column property "IsNullable" to true. 我们解决了将列属性“IsNullable”设置为true的问题。

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

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