---
title: Oracle 模式下 NULL 与 DATE 运算报错，类型不一致-OceanBase数据库使用指南
description: 了解OceanBase数据库在实际应用中关于 Oracle 模式下 NULL 与 DATE 运算报错，类型不一致相关的常见问题和使用技巧，帮助您快速解决 Oracle 模式下 NULL 与 DATE 运算报错，类型不一致的难题。
---
切换语言

- 简体中文
- English

划线反馈

# Oracle 模式下 NULL 与 DATE 运算报错，类型不一致

更新时间：2026-05-18 09:11

适用版本： V2.1.x、V2.2.x、V3.1.x、V3.2.x 内容类型：TechNote  

## 问题描述

对于存储过程中，结果存在 NULL 与 DATE 的运算时：

- Oracle 中可以正常做类型运算。
 - OceanBase 数据库的 Oracle 模式报错 `ORA-00932: inconsistent datatypes`。

## Oracle 数据库测试

1. 创建测试表 t1，t2。
 2. 创建表 t1。

```
```Shell
SQL> CREATE TABLE t1 (c1 date);
Table created.
```

```

2. 创建表 t2。

```
```shell
SQL> create table t2 (c1 number);
Table created
```

```

2. t1 查询结果为 NULL 时，执行正常，NULL 和 DATE 类型的运算结果为 NULL。
 3. 查询 t1 表。

```
```shell
SQL> select * from t1;
 no rows selected
```

```

2. 执行如下存储过程。

```
```shell
SQL> declare  
    fee1 date;
    fee2 number(14, 2);
    begin
    select min(c1) into fee1 from t1;
    insert into t2 values(fee1 - to_date('2020-01-01', 'yyyy-mm-dd'));
    commit;
    end;
    /
PL/SQL procedure successfully completed.
```

```

3. 查询 t2 表。

```
```shell
SQL> select* from t2;
```

输出结果如下。

```shell
C1
----------
1 rows selected
```

```

3. t1 查询结果不为 NULL 时，执行也正常，结果为时间差值。
 4. 向 t1 表中插入数据。

   ```shell
    SQL> insert into t1 values(sysdate);
    1 row created.

   ```

   提交插入数据。

   ```shell
   SQL> commit;
   Commit complete.

   ```
 5. 查询表 t1。

   ```shell
   SQL> select * from t1;

   ```

   输出结果如下。

   ```shell
   C1
   -------------------
   2023-07-31 09:54:55
   1 rows selected.

   ```
 6. 执行如下存储过程。

```
```shell
SQL> declare  
    fee1 date;
    fee2 number(14, 2);
    begin
    select min(c1) into fee1 from t1;
    insert into t2 values(fee1 - to_date('2020-01-01', 'yyyy-mm-dd'));
    commit;
    end;
    /
PL/SQL procedure successfully completed.

```

```

4. 查询 t2 表。

 ```shell
 SQL> select * from t2;
 ```

 输出结果如下。

 ```shell  
 C1
 ----------
 1307.41314
 2 rows selected.

```

## OceanBase 数据库 Oracle 模式下测试

1. 创建测试表 t1，t2。
 2. 创建表t1。

```
```Shell
obclient> CREATE TABLE t1 (c1 date);
Query OK, 0 rows affected (0.668 sec)
```

```

```
```shell
obclient> create table t2(c1 number);
Query OK, 0 rows affected (0.172 sec)
```

```

2. t1 查询结果为NULL时，执行报错 `ORA-00932: inconsistent datatypes`，null 和 date 类型无法运行。
 3. 查询 t1 表。

```
```shell
obclient >  select *from t1;
Empty set (0.004 sec)
```

```

```
```shell
obclient > declare  
        fee1 date;
        fee2 number(14, 2);
        begin
        select min(c1) into fee1 from t1;
        insert into t2 values(fee1 - to_date('2020-01-01', 'yyyy-mm-dd'));
        commit;
        end;
        /
```

 输出结果如下。

 ```shell
 ORA-00932: inconsistent datatypes
 at anonymous block , line : 6, col : 3
 ```

```

3. 查询 t2。

```
```shell
obclient [CHUER]> select*from t2;
Empty set (0.040 sec)
```

```

3. t1 查询结果不为 NULL 时，执行正常，结果为时间差。
 4. 向 t1 表插入数据。

```
```shell
obclient >  insert into t1 values(sysdate);
Query OK, 1 row affected (0.059 sec)
```

提交插入数据。

```shell
obclient > commit;
Query OK, 0 rows affected (0.005 sec)
```

```

2. 查询 t1 表。

```shell
obclient > select*from t1;

```

输出结果如下:

```shell
 +-----------+
 | C1        |
 +-----------+
 | 31-JUL-23 |
 +-----------+
 1 row in set (0.004 sec)

```

3. 执行如下存储过程。

```
```shell
 obclient > declare  
        fee1 date;
        fee2 number(14, 2);
        begin
        select min(c1) into fee1 from t1;
        insert into t2 values(fee1 - to_date('2020-01-01', 'yyyy-mm-dd'));
        commit;
        end;
        /
Query OK, 1 row affected (0.112 sec)

```

```

  ```shell
  obclient > select * from t2;
  ```

   输出结果如下。

 ```shell
 +-------------------------------------------+
 | C1                                        |
 +-------------------------------------------+
 | 1307.410474537037037037037037037037037037 |
 +-------------------------------------------+
 1 row in set (0.026 sec)
 ```

## 适用版本

OceanBase V2.x 和 V3.x 版本。

## 问题原因

这个上面的例子在 OceanBase 数据库中会报错，insert 中的 fee1 会被抽为参数 0，它的值是 NULL，减法类型推导时，NULL类型 - DATE类型预期会报错。

在 Oracle 中不会报错，Oracle 中会用 fee1 的静态类型 date 进行类型推导。

## 解决方法

暂时没有好的解决方式。如果允许 null 和 date 做运算，会影响执行计划评估。ps 中参数是 NULL 和不是 NULL 的时候会命中不同的计划。
```

Previous

[MySQL 租户与 Oracle 租户在非 INT 类型与 INT 类型比较时的差异](https://www.oceanbase.com/knowledge-base/oceanbase-database-1000000000207700)

Next

[MySQL 模式下时间类型间隔计算错误，错误代码 4016](https://www.oceanbase.com/knowledge-base/oceanbase-database-20000000156) ![有帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*y6ocSqN8cqsAAAAAAAAAAAAAARQnAQ)![无帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*BG9IQJyLHF8AAAAAAAAAAAAAARQnAQ)![反馈](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*eTWdQKCRKHwAAAAAAAAAAAAAARQnAQ)[AI](https://www.oceanbase.com/obi) 咨询热线
