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
Related
I'm trying to create a function to get a list of values from my database. After some researches I found that I need to use the PIPELINE function and I found some examples. I did my function but I somehow got 2 errors that I don't understand.
Here's my code :
CREATE OR REPLACE TYPE LISTE_VALUES AS TABLE OF VARCHAR2(2000);
/
CREATE OR REPLACE FUNCTION F_GET_VAL(
PI_1 IN VARCHAR2,
PI_2 IN NUMBER,
PI_3 IN VARCHAR2)
RETURN LISTE_VALUES PIPELINED
IS
W_ROW_COUNT NUMBER := 0;
BEGIN
FOR CUR IN (SELECT VALUE FROM TABLE
WHERE ...
...
)
LOOP
PIPE ROW (CUR);
W_ROUNT_COUNT := W_ROW_COUNT + 1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('There were '
|| W_ROW_COUNT
|| ' rows selected' );
END F_GET_VAL;
/
And These are the errors I get :
[Error] PLS-00382 : PLS-00382: expression is of wrong type (at the
line : PIPE ROW (CUR);)
[Error] PLS-00201 : PLS-00201: identifier 'W_ROUNT_COUNT' must be
declared
(at the line : W_ROUNT_COUNT := W_ROW_COUNT + 1;)
For the first error I triple checked and VALUE in my table has a type VARCHAR2(2000), exactly as I declared my type at the beginning (a table of VARCHAR2(2000)).
And for the second, I don't understand because I declared the variable W_ROW_COUNT in my IS statement.
If someone could help me it would be nice !
Thanks
A PIPE ROW can be created for a single row and not the cursor's name variable, which contains the entire recordset.
Just use
PIPE ROW ( cur.value );
instead of PIPE ROW ( cur );
You may also store the query output into a collection and then pipe each element.
Regarding the error due to W_ROW_COUNT, it is a typo. You have wrongly used it as W_ROUNT_COUNT while adding it.
Demo
I get this error when I compile my function.
PLS-00103: Encountered the symbol "*" when expecting one of the following:
:= . ( # % ;
Here is the code:
CREATE OR REPLACE FUNCTION IncreaseSalary
(para_empid IN employee.employeeid%TYPE, para_increase IN NUMBER)
RETURN NUMBER
IS
v_SalaryOut NUMBER(10,2);
v_salary2 NUMBER;
BEGIN
SELECT Salary INTO v_Salary2
FROM Employee
WHERE employeeid = para_empid;
--this is the area that pertains to the error
(v_salary2 * para_increase) + v_salary2 = v_salaryout;
RETURN v_SalaryOut;
EXCEPTION
WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee not found.');
END IncreaseSalary;
/
This next part wouldn't be part of the function and is not part of the error
but probably has errors.
DECLARE
v_SalaryOutput NUMBER := IncreaseSalary;
BEGIN
IncreaseSalary('01885', '20%');
DBMS_OUTPUT.PUT_LINE ('Increased Salary: ')
|| TO_CHAR(v_SalaryOutput));
END;
/
The point is to take in two numbers (employeeid and the percentage to increase whatever salary is already listed in the table) and return the updated salary. I don't understand why I can't multiply.
You cannot assign value to variable like that. The target variable should come first. It should be,
v_salaryout := (v_salary2 * para_increase) + v_salary2;
You have declared para_increase as NUMBER. But while calling the function, you are passing '20%' as the value. This will result in numeric or value error: character to number conversion error.
There is an extra bracket in you dbms_output statement.
DBMS_OUTPUT.PUT_LINE ('Increased Salary: ' || TO_CHAR(v_SalaryOutput));
I tried to create a procedure in pl/sql developer like,
create or replace procedure insert_muh_fis(birim_id in number(15)) is
begin
insert into muh_Fis_d013
select * from muh_fis mf where mf.fk_birim_id = birim_id;
--delete from muh_fis mf where mf.fk_birim_id = birim_id;
--commit;
end;
But it gives me Compilation error.
Error: PLS-00103: Encountered the symbol "(" when expecting one of the following: := . ) , # % öndeğer karakterThe symbol ":=" was substituted for "(" to continue.
Line: 1
I would appreciate any idea to help me out solve this problem. Thank you very much.
You don't need to specify the datatype precision in IN parameter, your parameter declaration should be like birim_id IN NUMBER.
Try,
CREATE OR REPLACE
PROCEDURE insert_muh_fis(birim_id IN NUMBER)
IS
BEGIN
INSERT INTO muh_fis_d013
SELECT * FROM muh_fis mf WHERE mf.fk_birim_id = birim_id;
--delete from muh_fis mf where mf.fk_birim_id = birim_id;
--commit;
END;
I'm trying to create a user defined function in Oracle that will return a DATE when given a text argument containing a date substring. I've tried a couple ways of writing this, and all seem to throw the same error:
CREATE OR REPLACE FUNCTION lm_date_convert (lm_date_in IN VARCHAR2(50))
RETURN DATE DETERMINISTIC IS
BEGIN
RETURN(TO_DATE(REGEXP_REPLACE(lm_date_in, '([[:digit:]]{2})[-/.]*([[:digit:]]{2})[-/.]*([[:digit:]]{4})','\3-\1-\2'), 'YYYY-MM-DD'));
END;
the error:
FUNCTION lm_date_convert Compiled. 1/46
PLS-00103: Encountered
the symbol "(" when expecting one of
the following:
:= . ) , # % default character The
symbol ":=" was substituted for "(" to
continue.
Any thoughts on this, and general UDF writing tips (and good references) are welcome! Thanks.
We cannot restrict the datatype when specifying parameters in stored procedures. That is, just use VARCHAR2 rather than VARCHAR2(50).
Just to prove I'm reproducing your problem ...
SQL> CREATE OR REPLACE FUNCTION lm_date_convert (lm_date_in IN VARCHAR2(50))
2 RETURN DATE DETERMINISTIC IS
3 BEGIN
4 RETURN(TO_DATE(REGEXP_REPLACE(lm_date_in, '([[:digit:]]{2})[-/.]*([[:digit:]]{2})[-/.]*([[:digit:]]{4})','\3-\1-\2'), 'YYYY-MM-DD'));
5 END;
6 /
Warning: Function created with compilation errors.
SQL> sho err
Errors for FUNCTION LM_DATE_CONVERT:
LINE/COL ERROR
-------- -----------------------------------------------------------------
1/49 PLS-00103: Encountered the symbol "(" when expecting one of the
following:
:= . ) , # % default character
The symbol ":=" was substituted for "(" to continue.
SQL>
Now to fix it:
SQL> ed
Wrote file afiedt.buf
1 CREATE OR REPLACE FUNCTION lm_date_convert (lm_date_in IN VARCHAR2)
2 RETURN DATE DETERMINISTIC IS
3 BEGIN
4 RETURN(TO_DATE(REGEXP_REPLACE(lm_date_in, '([[:digit:]]{2})[-/.]*([[:digit:]]{2})[-/.]*([[:digit:]]{4})','\3-\1-\2'), 'YYYY-MM-DD'));
5* END;
SQL> r
1 CREATE OR REPLACE FUNCTION lm_date_convert (lm_date_in IN VARCHAR2)
2 RETURN DATE DETERMINISTIC IS
3 BEGIN
4 RETURN(TO_DATE(REGEXP_REPLACE(lm_date_in, '([[:digit:]]{2})[-/.]*([[:digit:]]{2})[-/.]*([[:digit:]]{4})','\3-\1-\2'), 'YYYY-MM-DD'));
5* END;
Function created.
SQL>
"If you really do want a VARCHAR2(50)
then declare a type of VARCHAR2(50)
and use the type."
Declaring a SQL TYPE to enforce sizing is a bit of overkill. We can declare SUBTYPEs in PL/SQL but their sizes are not actually enforced in stored procedure signatures. However there are workarounds as I discuss in this other thread.
As an aside, why are you using Regex to solve this problem? Or rather, what problem are you trying to solve which cannot be solved with TO_CHAR and TO_DATE? Oracle's pretty forgiving with format masks.
i'm new to pl/sql and trying to print even numbers up till 100 using the following code:
Declare
i number;
sum number:=0;
x number:=2;
n number;
Begin
for i in 1..100 loop
if (i%x=0) then
n:=i;
sum:=sum+n;
end if;
end loop;
dbms_output.put_line(sum);
end;
i get this error
ERROR at line 10:
ORA-06550: line 10, column 12:
PLS-00103: Encountered the symbol "+" when expecting one of the following:
(
ORA-06550: line 13, column 26:
PLS-00103: Encountered the symbol ")" when expecting one of the following:
(
Help? :(
There is no % operator in PL/SQL; use the mod(i,x) function instead. Anyway, you don't need the n variable. sum:=sum+i will do. sum might be a reserved word, use s instead.
\
Now:
Declare
i number;
sum number:=0;
Begin
for i in 1..100 loop
if (mod(i,2)=0) then
sum:=sum+i;
end if;
end loop;
dbms_output.put_line(sum);
end;
sameproblem :(
There is no % operator in PL/SQL; use the mod(i,x) function instead. Anyway, you don't need the n variable. sum:=sum+i will do. But: sum is a PL/SQL keyword, use s instead.
Declare
i number;
sum1 number:=0;
x number:=2;
n number;
Begin
for i in 1..100 loop
if(i mod x =0)then
n:=i;
sum1:=sum1+n;
end if;
end loop;
dbms_output.put_line(sum1);
end;
/
Reason :
The "sum" is one of the aggregate function ,so we can't use this as variable name.