简体   繁体   English

Oracle绑定变量给出错误

[英]Oracle Bind Variable giving error

SET SERVEROUTPUT ON

VARIABLE dept_id NUMBER

DECLARE

  max_deptno NUMBER(3);

  dept_name departments.department_name%TYPE :='Education';

BEGIN

  SELECT MAX(department_id)

  INTO max_deptno 

  FROM departments;

  DBMS_OUTPUT.PUT_LINE ('The maximum department no is : '  || max_deptno);

  :dept_id:=(max_deptno+10);

  INSERT INTO departments (department_name, department_id,location_id)

  VALUES(dept_name,  :dept_id, NULL);

  DBMS_OUTPUT.PUT_LINE ('The number of rows affected : '  || SQL%ROWCOUNT);


END;

/

Error report: ORA-01400: cannot insert NULL into ("SYSTEM"."DEPARTMENTS"."DEPARTMENT_ID") ORA-06512: at line 10 01400. 00000 - "cannot insert NULL into (%s)" *Cause: 错误报告:ORA-01400:无法将NULL插入(“ SYSTEM”。“ DEPARTMENTS”。“ DEPARTMENT_ID”)ORA-06512:在第10 01400行。00000-“无法将NULL插入(%s)” *原因:
*Action: The maximum department no is : 190 *操作:最大部门编号为:190

I am getting this error while trying to execute the bind variable in oracle statment. 我在尝试执行oracle语句中的绑定变量时遇到此错误。 But if i put some value instead of bind variable, i get this insert statement right. 但是,如果我放置一些值而不是绑定变量,则此插入语句正确。 What am I doing wrong here? 我在这里做错了什么?

I think the value of the bind variable is only set when the pl/sql block is finished. 我认为bind变量的值仅在pl / sql块完成时设置。 And it probably has to terminate normally. 它可能必须正常终止。

One solution is to use max_deptno+10 in the insert insead of :dept_if . 一种解决方案是在:dept_if的插入片段中使用max_deptno+10 A better solution is to create another pl/sql variable and use that in the insert statement. 更好的解决方案是创建另一个pl / sql变量,然后在insert语句中使用它。

new_dept_id := max_deptno+10;
:dept_id := new_dept_id;

You also have to change the INSERT statement: 您还必须更改INSERT语句:


INSERT INTO departments (department_name,department_id,location_id)
    VALUES(dept_name, new_dept_id, NULL);

我认为是由于使用绑定变量而不在启动程序中使用set autoprint on了此错误。

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

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