Comment utiliser lier des variables dans plsql?

Tâche Simple

variable dept_id NUMBER

DECLARE
    max_deptno NUMBER;
    dept_name departments.department_name%TYPE := 'Education';
BEGIN
    select max(department_id)
      into max_deptno
      from departments;
    :dept_id := max_deptno + 10;
    insert into departments (department_id, department_name, location_id)
      values (:dept_id, dept_name, null);
    DBMS_OUTPUT.PUT_LINE('The maximum department id is ' || max_deptno);
    DBMS_OUTPUT.PUT_LINE('Rows made by insert: ' || SQL%ROWCOUNT);
END;

max_deptno n'est pas NULLE. Pourquoi dept_id est NULLE après la cession? Ce que je fais mal?

script de sortie:

MAX(DEPARTMENT_ID)
------------------
               520 

Error starting at line 10 in command:
DECLARE
max_deptno NUMBER;
dept_name departments.department_name%TYPE := 'Education1';
BEGIN
select max(department_id)
into max_deptno
from departments;
:dept_id := max_deptno + 10;
insert into departments (department_id, department_name, location_id)
values (:dept_id, dept_name, null);
DBMS_OUTPUT.PUT_LINE('The maximum department id is ' || max_deptno);
DBMS_OUTPUT.PUT_LINE('Rows made by insert: ' || SQL%ROWCOUNT);
END;

Rapport d'erreur:

ORA-01400: невозможно вставить NULL в ("ANDKOM"."DEPARTMENTS"."DEPARTMENT_ID")
ORA-06512: на  line 9
01400. 00000 -  "cannot insert NULL into (%s)"
*Cause:    
*Action:
  • Comment savez-vous que MAX_DEPTNO n'est PAS NULLE? Pouvez-vous nous montrer les résultats de SELECT MAX(department_id) FROM andkom.departments?
  • MAX(DEPARTMENT_ID) 520
  • OK. Vous pouvez couper et coller à partir d'un seul SQL*Plus de la session où vous 1) définir la variable hôte DEPT_ID, 2) set serveroutput on, 3) SELECT MAX(department_id) from andkom.departments, et puis 4) exécuter le code PL/SQL bloc que vous avez posté? Quelque chose n'a pas de sens.
  • J'utilise SQL Developer
  • OK. Ensuite, exécutez ces déclarations comme un script (F5) et de copier et coller le script de sortie.
  • J'ai mis à jour la question avec cette info (à la fin)

InformationsquelleAutor bluebyte | 2011-11-18