When I tried to compile this procedure:
PROCEDURE GET_NUMBER_PROPOSED_TRADES(p_client_id NUMBER, p_curr_id NUMBER, p_cursor OUT sys_refcursor)
IS
BEGIN
OPEN p_cursor FOR
IF (p_client_id = 0 AND p_curr_id = 0)
THEN
SELECT COUNT (DISTINCT NVL(id,0)) AS "Proposed_Trade_Number" FROM proposed_trade;
ELSE
SELECT COUNT (DISTINCT NVL(id,0)) AS "Proposed_Trade_Number" FROM proposed_trade WHERE client_id = p_client_id AND ccy_id = p_curr_id;
END IF;
END GET_NUMBER_PROPOSED_TRADES;
this error occurred:
Error(9,11): PLS-00103: Encountered the symbol "IF" when expecting one of the following: ( - + case mod new not null select with continue avg count current exists max min prior sql stddev sum variance execute forall merge time timestamp interval date pipe
Error(18,1): PLS-00103: Encountered the symbol "PROCEDURE" when expecting one of the following: end not pragma final instantiable order overriding static member constructor map The symbol "static" was substituted for "PROCEDURE" to continue.
Error(25,1): PLS-00103: Encountered the symbol "PROCEDURE" when expecting one of the following: end not pragma final instantiable order overriding static member constructor map The symbol "static" was substituted for "PROCEDURE" to continue.
But the thing is, if I just remove IF condition, I can compile it easily, so this kind of a new for me
You can't nest an if condition inside a cursor declaration, it's just not valid syntax. One way around this would be to have a single query, and use the where clause conditions to get the same logic you were trying to get from the if:
OPEN p_cursor FOR
SELECT COUNT (DISTINCT NVL(id,0)) AS "Proposed_Trade_Number"
FROM proposed_trade
WHERE (p_client_id = 0 AND p_curr_id = 0) OR
(client_id = p_client_id AND ccy_id = p_curr_id);
Related
SQL Worksheet
create or replace FUNCTION checkSubmitted (pStudentId IN VARCHAR, pCourseCode IN VARCHAR, pAssignmentNumber IN NUMBER)
RETURN VARCHAR
IS output VARCHAR(20);
DECLARE mark_result NUMBER;
BEGIN
SELECT assignment.mark INTO mark_result FROM Assignment WHERE student_id = pStudentId AND course_code = pCourseCode AND assignment_number = pAssignmentNumber;
IF (mark_result IS NOT NULL) THEN
RETURN 'Submitted';
ELSE
RETURN 'Not Submitted';
END IF;
END;
Compiler - Log
LINE/COL ERROR
--------- -------------------------------------------------------------
4/5 PLS-00103: Encountered the symbol "DECLARE" when expecting one of the following: begin function pragma procedure subtype type <an identifier> <a double-quoted delimited-identifier> current cursor delete exists prior The symbol "begin" was substituted for "DECLARE" to continue.
12/4 PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: ( begin case declare end exception exit for goto if loop mod null pragma raise return select update while with <an identifier> <a double-quoted delimited-identifier> <a bind variable> << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge
Errors: check compiler log
I want to create a SQL check function and i need to declare a variable in the function.
Please advise how can i achieve this.
As commented, your current problem is easy to fix.
However, I'd like to point you to another possible "mistake" - what will happen if there's no data for combination of those parameters' values? SELECT won't return NULL - it will raise NO_DATA_FOUND exception, and you should handle it. (I presume that it isn't likely for query to return more than a single value; if it is, handle it as well).
Also, try to avoid multiple RETURN statements. Procedures can be long and you might get lost in where you return and what you return. Set output variable's value (which you declared, but never used), and return it at the end of the procedure.
Therefore, consider something like this:
create or replace function checksubmitted
(pstudentid in varchar, pcoursecode in varchar, passignmentnumber in number)
return varchar
is
mark_result number;
output varchar(100);
begin
select a.mark
into mark_result
from assignment a
where a.student_id = pstudentid
and a.course_code = pcoursecode
and a.assignment_number = passignmentnumber;
if mark_result is not null then
output := 'Submitted';
else
output := 'Not Submitted';
end if;
return output;
exception
when no_data_found then
output := 'There is no data for these input parameters';
return output;
end;
/
I am facing this error:
LINE/COL ERROR
-------- -----------------------------------------------------------------
4/11 PLS-00103: Encountered the symbol "IS" when expecting one of the
following:
:= . ( # % ; not null range default character
My package is:
CREATE OR REPLACE PACKAGE BODY EMP_PK AS
PROCEDURE INSERT_TR(EMPNO EMP_20171250.EMPNO%TYPE,ENAME EMP_20171250.ENAME%TYPE,SAL EMP_20171250.SAL%TYPE) IS
INSERT_ERROR EXCEPTION;
CRUSOR C1 IS INSERT INTO EMP_20171250(EMPNO,ENAME,SAL) VALUES(EMPNO,ENAME,SAL);
BEGIN
IF(C1%FOUND) THEN
DBMS_OUTPUT.PUT_LINE('RECORD INSERTED');
ELSE
RAISE INSERT_ERROR;
END IF;
EXCEPTION
WHEN INSERT_ERROR THEN
DBMS_OUTPUT.PUT_LINE('ERROR WHILE RECORD INSERTION');
END INSERT_TR;
END EMP_PK;
it is not CRUSOR but CURSOR
cursor can't contain INSERT statement; it is a SELECT
you are checking whether cursor returned something, but - you never opened it nor fetched from it so it is pretty much useless
insert_error looks like there was an error while inserting a row, but - you are actually raising it if cursor didn't return anything
Basically, you don't need a cursor at all. Such a procedure would do:
PROCEDURE insert_tr (p_empno emp_20171250.empno%TYPE,
p_ename emp_20171250.ename%TYPE,
p_sal emp_20171250.sal%TYPE)
IS
BEGIN
INSERT INTO emp_20171250 (empno, ename, sal)
VALUES (p_empno, p_ename, p_sal);
END insert_tr;
If insert fails, Oracle will raise an exception anyway so - yes, you can handle it/them if you want.
Also, it is good to distinguish parameter names from column names; for example, precede their names with a p_ (as I did).
I'm trying to write a cursor. I try to match the syntax of examples but always getting compile failure on the FETCH statement.
CREATE OR REPLACE PROCEDURE IFSAPP.CLEAR_OLD_PURCHASE_ORDERS (cPlannedDelDate in varchar2) IS
-- cursor to get all the purchase orders's that have lines in released state that
CURSOR c1 IS
SELECT DISTINCT PO.ORDER_NO
FROM PURCHASE_ORDER PO, PURCHASE_ORDER_LINE_NOPART POLN
WHERE PO.ORDER_NO = POLN.ORDER_NO
AND POLN.STATE = 'Released'
AND POLN.PLANNED_DELIVERY_DATE < TO_DATE(cPlannedDelDate, 'DD/MM/YYYY');
BEGIN
DECLARE corder_no varchar2(12);
OPEN c1;
LOOP
FETCH c1 INTO corder_no;
EXIT WHEN c1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(corder_no);
END LOOP;
CLOSE c1;
END CLEAR_OLD_PURCHASE_ORDERS;
/
LINE/COL ERROR
-------- -----------------------------------------------------------------
17/9 PLS-00103: Encountered the symbol "FETCH" when expecting one of the following:
constant exception <an identifier>
<a double-quoted delimited-identifier> table columns long
double ref char time timestamp interval date binary national
character nchar
21/5 PLS-00103: Encountered the symbol "CLOSE" when expecting one of the following:
end not pragma final instantiable order overriding static
member constructor map
Can anyone see where I am going wrong?
The problem is actually where you're declaring your local variable, and the use of the DECLARE keyword. That's starting a new inner PL/SQL block, but you then have the OPEN etc. without continuing that pattern with a new BEGIN.
You don't need a sub-block though, just move the local variable declaration up before the existing BEGIN, and lose the extra DECLARE:
CREATE OR REPLACE PROCEDURE IFSAPP.CLEAR_OLD_PURCHASE_ORDERS (cPlannedDelDate in varchar2) IS
-- cursor to get all the purchase orders's that have lines in released state that
CURSOR c1 IS
SELECT DISTINCT PO.ORDER_NO
FROM PURCHASE_ORDER PO, PURCHASE_ORDER_LINE_NOPART POLN
WHERE PO.ORDER_NO = POLN.ORDER_NO
AND POLN.STATE = 'Released'
AND POLN.PLANNED_DELIVERY_DATE < TO_DATE(cPlannedDelDate, 'DD/MM/YYYY');
corder_no varchar2(12);
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO corder_no;
EXIT WHEN c1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(corder_no);
END LOOP;
CLOSE c1;
END CLEAR_OLD_PURCHASE_ORDERS;
/
Incidentally, you should consider using ANSI join syntax, not the ancient comma-separated-FROM clause syntax. And it would be simpler to use an implicit cursor loop:
CREATE OR REPLACE PROCEDURE IFSAPPCLEAR_OLD_PURCHASE_ORDERS (cPlannedDelDate in varchar2) IS
BEGIN
FOR r1 IN (
SELECT DISTINCT PO.ORDER_NO
FROM PURCHASE_ORDER_LINE_NOPART POLN
JOIN PURCHASE_ORDER PO
ON PO.ORDER_NO = POLN.ORDER_NO
WHERE POLN.STATE = 'Released'
AND POLN.PLANNED_DELIVERY_DATE < TO_DATE(cPlannedDelDate, 'DD/MM/YYYY')
) LOOP
DBMS_OUTPUT.PUT_LINE(r1.order_no);
END LOOP;
END CLEAR_OLD_PURCHASE_ORDERS;
/
I'd also generally prefer to have the procedure argument declared as the data type you need, i.e. as a DATE, so you can use that in your query without converting it; and make it the caller's problem to pas the correct data type in.
I am a beginner in oracle and following is my function definition and invocation part. I am unable to understand the error that I get when I call the function. Please help me rectify my code.
ORA-06550: line 4, column 56: PLS-00103: Encountered the symbol ")" when expecting one of the following: (
create or replace function totalcustomers
RETURN number
IS
total number:=0;
BEGIN
select count(*) into total from customers;
RETURN total;
END;
/
declare sum number;
BEGIN
sum := totalcustomers();
dbms_output.put_line('Total number of customers '||sum);
END;
/
Do not use sum as a variable which is a reserved keyword in Oracle.
Sum is a function, so it's expecting the open paren. Rename the variable.
The function invocation part was throwing the mentioned error, because "sum" might be a pre-defined keyword in oracle. Changing the variable as follows helped.
declare x number;
BEGIN
x:=totalcustomers();
dbms_output.put_line(' Total number of customers: '||x);
END;
/
Output :
Statement processed.
Total number of customers: 6
Im trying to create a stored procedure that calls another a number of times. This is done so by using a for each loop. All the development is under oracle sql developer Version 3.0.04.
CREATE OR REPLACE PROCEDURE Z_INBILLABILITYSERV
IS BEGIN
DECLARE
ano VARCHAR2(4);
BEGIN
select EXTRACT(YEAR FROM sysdate) into ano from dual;
FOR dat IN (SELECT * FROM Z_FECHOMES WHERE MES <=
(select EXTRACT(MONTH FROM sysdate) from dual )and ANO = ano)
LOOP
call z_insertbillability(dat.periodo_inicio,dat.periodo_fim,
dat.ano,dat.mes);
END LOOP;
END;
END;
Im having the following error:
Error(9,12): PLS-00103: Encountered the symbol "Z_INSERTBILLABILITY" when expecting one of the following: := . ( # % ; The symbol ":=" was substituted for "Z_INSERTBILLABILITY" to continue.
If anyone have an idea or a tip i would be glad to now and would appreciate a lot.
You do not need the word call; just do:
LOOP
z_insertbillability(dat.periodo_inicio,dat.periodo_fim,
dat.ano,dat.mes);
END LOOP;
The error message is perhaps a bit unhelpful, but it's to be trying to show all the ways it could try to interpret the word call, since it doesn't recognise it as a keyword. And showing what it would expect to see next for each: as a variable name (which would be followed by := for assignment; or a schema name (which would be followed by .); or a function/procedure name (which would be followed by ( for the parameter list), etc.