4

i am trying to execute or run the following pl/sql script:

SET serveroutput on;

CREATE OR REPLACE PROCEDURE findAvg
(p_category IN products.category_id% TYPE, c OUT NUMBER)
AS
BEGIN
SELECT NVL ((SELECT AVG(LIST_PRICE) FROM products
WHERE p_category = category_id), -1) into p_category
from dual;
END findAvg;
DECLARE
 cat  products.category_id%TYPE;
 Price   products.List_price%TYPE;
BEGIN
cat := &p_category;
findAvg (cat, price); 
if (price = -1) then
    dbms_output.put_line('Wrong Category ');
ELSE
    dbms_output.put_line('the average price for category' || cat || ' is ' || price);
 END IF;
END;
/
show errors

but when i try to run it, i get this error message (i can see it only after show errors):

       PLS-00103: Encountered the symbol "DECLARE" 

what is wrong with this declare?

RD7
  • 628
  • 4
  • 9
  • 20
  • http://stackoverflow.com/questions/20334067/pls-00103-encountered-the-symbol-create/20334358#20334358 – Drumbeg Jan 06 '16 at 13:15

1 Answers1

8

You are missing a "/" between the creation of the procedure and the start of the anonymous block that runs it:

SET serveroutput on;

CREATE OR REPLACE PROCEDURE findAvg
(p_category IN products.category_id% TYPE, c OUT NUMBER)
AS
BEGIN
  SELECT NVL(AVG(LIST_PRICE),-1)
  INTO c
  FROM products
  WHERE p_category = category_id;
END findAvg;
/

show errors

DECLARE
 cat  products.category_id%TYPE;
 Price   products.List_price%TYPE;
BEGIN
cat := &p_category;
findAvg (cat, price); 
if (price = -1) then
    dbms_output.put_line('Wrong Category ');
ELSE
    dbms_output.put_line('the average price for category' || cat || ' is ' || price);
 END IF;
END;
/

Also, the "show errors" command should be run just after creating the procedure, as I have above, and the INTO clause should specify the OUT parameter.

Tony Andrews
  • 129,880
  • 21
  • 220
  • 259