V
V
Vladimir Vladimirovich2017-04-26 21:40:54
Oracle
Vladimir Vladimirovich, 2017-04-26 21:40:54

How to eliminate empty query error in ORACLE?

The procedure contains the following query:

AS 
  INSERTED VARCHAR(200);
  ID_ NUMBER;
...
SELECT ID_MATERIAL INTO INSERTED FROM SOURCE WHERE SHORTNAME_MATERIAL=SHORT_NAME;

In the case when the select should return an empty record, oracle gives an error
ORA-01403: no data found
ORA-06512: at "********************", line 11
ORA-06512: at line 12
how to fix? the procedure should work further after this request, and depending on the result (null or non-null) perform other operations.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton, 2017-05-04
@leha2148

The simplest way, however, is to handle the no_data_found error.
The reference script is

AS 
  INSERTED VARCHAR(200);
  ID_ NUMBER;
BEGIN
...
BEGIN
SELECT ID_MATERIAL INTO INSERTED FROM SOURCE WHERE SHORTNAME_MATERIAL=SHORT_NAME;
EXCEPTION
WHEN NO_DATA_FOUND THEN NULL ; (или INSERTED := NULL)
END;
...
IF INSERTED IS NULL THEN
...
ELSE
...
END IF ;
...
END <PROCEDURE_NAME> ;

In general, according to the above example, we can say that it is necessary to work on the design of the code (design standard). Names of variables and tables are not customary. Use prefixes VAR_, V_, GV_, LV_ for variables and prefixes TBL_, T_ or suffix _TBL for tables. Instead of VARCHAR, it should be VARCHAR2. Be sure to specify the dimension type for strings - BYTE or CHAR. Table aliases are good practice even in such simple queries. It is impossible to understand the condition SHORTNAME_MATERIAL=SHORT_NAME is a comparison of the fields of a tuple, two scalar internal or external variables, or constants, or a comparison of a field with a variable/constant.

D
Draconian, 2017-04-27
@Draconian

The simplest way is to make this query a cursor and check for the presence or absence of rows there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question