Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle Query - Missing Defines

I created a very simple table:

CREATE TABLE TMP ("ID" VARCHAR2(20 BYTE));

Then tried to do this:

DECLARE
  whatever varchar2(20) := :bananas;
BEGIN
  MERGE INTO tmp t USING 
    (SELECT whatever AS this_id FROM DUAL) d 
  ON (t.id = d.this_id) 
    WHEN NOT MATCHED THEN 
      INSERT (id) VALUES (d.this_id);
END;

And then enter binds

enter image description here

And get this error:

Error starting at line : 1 in command -
DECLARE
  whatever varchar2(20) := :bananas;
BEGIN
  MERGE INTO tmp2 t USING 
    (SELECT whatever AS this_id FROM DUAL) d 
  ON (t.id = d.this_id) 
    WHEN NOT MATCHED THEN 
       INSERT (id) VALUES (d.this_id);
END;
Error report -
Missing defines

I've had no luck figuring out what it wants. If I replace ':bananas' with a value like 'a' it works, but not when I use a variable and bind the value. Anyone know whats wrong with my query? Thanks.

Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
"CORE   11.2.0.4.0  Production"
TNS for IBM/AIX RISC System/6000: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production

edit: I've just noticed that the error is not preventing the data from being merged correctly... The error is still concerning though

like image 854
this-Mathieu Avatar asked Jun 13 '15 02:06

this-Mathieu


People also ask

What does != Mean in Oracle?

It (<>) is a function that is used to compare values in database table. != (Not equal to) functions the same as the <> (Not equal to) comparison operator.

What is missing left parenthesis in Oracle?

ORA-00906: missing left parenthesis Cause: A required left parenthesis has been omitted. Certain commands, such as CREATE TABLE, CREATE CLUSTER, and INSERT, require a list of items enclosed in parentheses. Parentheses also are required around subqueries in WHERE clauses and in UPDATE table SET column = (SELECT...)

What is the difference between not in and not exists in Oracle?

not in can also take literal values whereas not exists need a query to compare the results with. EDIT: not exists could be good to use because it can join with the outer query & can lead to usage of index, if the criteria uses column that is indexed.


2 Answers

I would probable skip the DECLARE section all together and use everywhere :bananas instead of whatever. After BEGIN put :bananas := :bananas; and you'll get no error. Good luck.

like image 82
Don Pedro Avatar answered Nov 14 '22 00:11

Don Pedro


Try adding a slash after the statement on a line of its own. Then highlight the whole block and press F5.

like image 41
WW. Avatar answered Nov 14 '22 02:11

WW.