Assinging a number to binding variable pl/sql

不羁岁月 提交于 2019-12-24 19:47:40

问题


I am trying to assign a number value to a binding variable for a pl/sql assignment and it is giving me the error :

Error report:  
ORA-01403: no data found
ORA-06512: at line 15
01403. 00000 -  "no data found"
Cause: 
Action:
b_emp_id
------
b_emp_id

And the code

VARIABLE b_emp_id NUMBER

DECLARE

v_emp_id employees.employee_id%TYPE;
v_FIRST_NAME employees.first_name%TYPE;
v_LAST_NAME employees.last_name%TYPE;
v_JOB_ID employees.job_id%TYPE;
v_HIRE_DATE employees.hire_date%TYPE;
v_message VARCHAR2(30);
v_difference NUMBER(3);

BEGIN

:b_emp_id:=110;

 SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, job_id, HIRE_DATE 
 INTO v_emp_id, v_FIRST_NAME, v_LAST_NAME, v_JOB_ID, v_HIRE_DATE
 FROM employees
 WHERE EMPLOYEE_ID = :b_emp_id;

Thank you all in advance!!!


回答1:


The "ORA-01403: no data found" means that your SELECT returned no rows. Most likely EMPLOYEE_ID 110 doesn't exist in the table. Try this:

SELECT COUNT(*) FROM Employees WHERE Employee_ID = 110;

It will probably return zero.




回答2:


It might be possible that your bind variable is not being assigned the value correctly. Try the following code :-

VARIABLE b_emp_id NUMBER;
EXEC :b_emp_id := 110;

DECLARE

v_emp_id employees.employee_id%TYPE;
v_FIRST_NAME employees.first_name%TYPE;
v_LAST_NAME employees.last_name%TYPE;
v_JOB_ID employees.job_id%TYPE;
v_HIRE_DATE employees.hire_date%TYPE;
v_message VARCHAR2(30);
v_difference NUMBER(3);

BEGIN

 SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, job_id, HIRE_DATE 
 INTO v_emp_id, v_FIRST_NAME, v_LAST_NAME, v_JOB_ID, v_HIRE_DATE
 FROM employees
 WHERE EMPLOYEE_ID = :b_emp_id;
END;
/

print b_emp_id

try the above in sqlplus.




回答3:


This seems to be a problem with SQL Developer, which I noticed on a recent answer. The code you have will work in SQL*Plus. This shows the problem too:

variable b_emp_id number
set serveroutput on
begin
    :b_emp_id := 110;
    if :b_emp_id is null then
        dbms_output.put_line('b_emp_id is null');
    else
        dbms_output.put_line('b_emp_id is not null: ' || :b_emp_id);
    end if;
end;
/
print b_emp_id;

anonymous block completed
b_emp_id is null

B_EMP_ID
---
110

In SQL*Plus that gives:

b_emp_id is not null: 110

PL/SQL procedure successfully completed.

B_EMP_ID
---
110

... as you'd expect. The select 110 into :b_emp_id from dual trick doesn't work here either. Either way the value assigned to the bind variable isn't usable inside the block, but is visible outside, which is weird. I assume it's an SQL Developer bug, though I guess it might just be undefined behaviour.

It seems your only options are to use a separate exec block, since the bind value set within that is then in scope when you hit your real block (as Max suggested); or to use substitution variables instead; or to use a local variable (in the declare block) rather than an externally-declared variable - depends why you're doing it quite like this and using an explicit bind variable in the first place.



来源:https://stackoverflow.com/questions/15058529/assinging-a-number-to-binding-variable-pl-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!