Error is occurring during execution of procedure in oracle - oracle

I am getting the error during executing the below procedure.
CREATE OR REPLACE PROCEDURE P_SUMIT (P_FEED IN FEED.FEED_ID%TYPE, P_OPCO_ID IN FEED.OPCO_ID%TYPE)
AS
BEGIN
DECLARE V_PCF_PATTERN FEED.PCF_PATTERN%TYPE;
DECLARE CURSOR C_FEED FOR SELECT PCF_PATTERN FROM FEED WHERE FEED_ID=P_FEED AND OPCO_ID=P_OPCO_ID;
OPEN C_FEED;
LOOP
FETCH C_FFED INTO V_PCF_PATTERN;
EXIT WHEN C_FEED%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(V_PCF_PATTERN);
END LOOP;
CLOSE C_FEED;
END;
Error logs :
3/1 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 external language
The symbol "begin" was substituted for "DECLARE" to continue.
4/1 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
Please assist.

DECLARE statements should be after AS keyword.
CREATE OR REPLACE PROCEDURE P_SUMIT (P_FEED IN FEED.FEED_ID%TYPE, P_OPCO_ID IN FEED.OPCO_ID%TYPE)
AS
V_PCF_PATTERN FEED.PCF_PATTERN%TYPE;
CURSOR C_FEED IS SELECT PCF_PATTERN FROM FEED WHERE FEED_ID=P_FEED AND OPCO_ID=P_OPCO_ID;
BEGIN
OPEN C_FEED;
LOOP
FETCH C_FEED INTO V_PCF_PATTERN;
EXIT WHEN C_FEED%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(V_PCF_PATTERN);
END LOOP;
CLOSE C_FEED;
END P_SUMIT;
/

There's no need for an explicit cursor here, or for parameters to be prefixed with "p_". Implicit cursors are quicker and more robust to write, and perform better.
create or replace procedure p_sumit (
feed_id in feed.feed_id%type,
opco_id in feed.opco_id%type)
as
begin
for c_feed in (
select pcf_pattern
from feed
where feed_id = p_sumit.feed_id and
opco_id = p_sumit.opco_id)
loop
dbms_output.put_line(c_feed.pcf_pattern);
end loop;
end;

Related

Error when declaring a number variable in a function block in Oracle SQL

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;
/

oracle update stored procedure with user defined exception

I am trying to create this stored procedure which should take customer no and email address as input. Then update the email address for that customer. if new email address is same as old then exception should be raised.
CREATE OR REPLACE PROCEDURE UpdateEmail
(CUSTOMERID IN CUSTOMER.CUSTOMERNO%TYPE,
N_EMAIL IN CUSTOMER.EMAIL%TYPE)
IS
DECLARE
e_same EXCEPTION;
V_ROWCOUNT NUMBER;
BEGIN
SELECT COUNT(EMAIL)
INTO V_ROWCOUNT
FROM CUSTOMER#FIT5148B
WHERE CUSTOMER.EMAIL = N_EMAIL
AND CUSTOMER.CUSTOMERNO = CUSTOMERID;
IF V_ROWCOUNT > 0
THEN RAISE e_same;
ELSE
UPDATE CUSTOMER
SET EMAIL = N_EMAIL
WHERE CUSTOMER.CUSTOMERNO = CUSTOMERID;
END IF;
EXCEPTION
WHEN e_same THEN dbms_output.put_line ('email address exist');
END;
/
But it is throwing error. Not sure if I am doing it right. I am using the latest Oracle SQL Developer.
Error(6,1): PLS-00103: Encountered the symbol "DECLARE" when expecting
one of the following: begin function pragma procedure subtype type
current
cursor delete exists prior external language The symbol "begin" was
substituted for "DECLARE" to continue.
Error(28,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 << continue close current delete fetch lock insert open
rollback savepoint set sql execute commit forall merge pipe purge
Remove DECLARE .
You should not use Declare in a Procedure. If you are writing a PLSQL block then only you should use Declare. Not in an actual Procedure body.

Warning: Package Body created with compilation errors

Please check my package and procedures.
My package:
create or replace package transaction1 as
procedure enter_transaction(acc number, kind varchar2, amount number);
procedure apply_transaction;
end;
/
This is my body:
create or replace package body transaction1 as
procedure enter_transaction(acc in number, kind in varchar2, amount in number)
is
begin
end;
procedure apply_transaction
is
begin
end;
end;
/
What is the warning? Why?
If you see a warning: Errors: check compiler log
then run show errors command and you will see the error log :
7/5 PLS-00103: Encountered the symbol "END" when expecting one of the following:
( begin case declare 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
14/5 PLS-00103: Encountered the symbol "END" when expecting one of the following:
( begin case declare 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
In case of your package body Oracle complains because BEGIN END blocks don't contain any commands.
In Oracle the BEGIN-END block must contain at least one command. Could be NULL, if you don't want to run anything (and don't forget to place a semicolon after NULL command):
PROCEDURE ......
IS
BEGIN
NULL;
END;

How to return a table of values from a function in oracle

I have tried some of the answers and could not make my code work:
Here is what I want and here is what I did:
I have a table which is two column type and I want to query that table using a function to return exactly the same table to be routed to VB.NET so that I can display it in a DatagridView control.
I am a novice to PL/SQL which is the problem for me.
The first problem I intended to solve is to create the function.
-- DECLARE A RECORD TYPE
create or replace type shipper_type AS OBJECT
(
shipper_id number, shipper_name varchar2(7)
);
/
CREATE TYPE t_shipper as table of shipper_type;
/
create or replace function get_shipper return t_shipper is
temp_list t_shipper:= t_shipper();
is
-- shipper_record shipper_type;
begin
for i in ( (select shipper.shipper_id, shipper.shipper_name) list from shipper)
loop
temp_list.extend;
temp_list(temp_list.last):= t_shipper(list);
end loop;
return(temp_list);
end get_shipper;
/
When I try to compile this code I am getting the following errors:
Errors for FUNCTION GET_SHIPPER:
LINE/COL ERROR
-------- -----------------------------------------------------------------
3/1 PLS-00103: Encountered the symbol "IS" when expecting one of the
following:
begin function pragma procedure subtype type <an identifier>
<a double-quoted delimited-identifier> current cursor delete
exists prior
6/63 PLS-00103: Encountered the symbol ")" when expecting one of the
following:
. ( , * # % & - + / at mod remainder rem <an identifier>
<a double-quoted delimited-identifier> <an exponent (**)> as
from || multiset
You get those compilation messages because you have several syntax errors in your code. Two instances of IS, missing the type in the select, those unnecessary brackets in the return. You can correct those but you should simplify your code too.
The easiest way to populate a nested table is with a bulk collect.
create or replace function get_shipper return t_shipper is
temp_list t_shipper:= t_shipper();
begin
select shipper_type(shipper.shipper_id, shipper.shipper_name)
bulk collect into temp_list
from shipper;
return temp_list ;
end get_shipper;
/
Try it without () in the return statement and add an end; at the end.
return temp_list;
end;
Also you might want to take a look at so called pipelined functions:
http://www.oracle-base.com/articles/misc/pipelined-table-functions.php

PL/SQL UTL_FILE read trouble on anonymous block

DECLARE
data_line VARCHAR2(200); -- Data line read from input file
data_file UTL_FILE.FILE_TYPE; -- Data file handle
my_dir VARCHAR2(250); -- Directory containing the data file
my_filename VARCHAR2(50);
BEGIN
my_dir := 'c:\temp';
my_filename := 'Lab4AData.dat';
my_file := UTL_FILE.FOPEN(my_dir, my_filename, 'r');
LOOP;
UTL_FILE.GET_LINE(data_file, data_line);
EXCEPTION
WHEN no_data_found THEN
DBMS_OUTPUT.PUT_LINE('Finished');
exit;
END LOOP;
END;
/
The problem is I cannot even get this anonymous block of code started. To start, I'm just trying to open my data file and read it, then build from there. But I can't even get the file open.
SQL Developer Error Report starts right off with
Error starting at line 5 in command:
DECLARE
then repeats the block of code and adds this:
ORA-06550: line 12, column 8:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
( begin case declare 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
The symbol "exit" was substituted for ";" to continue.
ORA-06550: line 15, column 3:
PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following:
( begin case declare end exit for goto if loop mod null
pragma raise return select update while with <an identifier>
<a double-quoted delimited-i
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Try the following:
DECLARE
data_line VARCHAR2(200); -- Data line read from input file
data_file UTL_FILE.FILE_TYPE; -- Data file handle
my_dir VARCHAR2(250); -- Directory containing the data file
my_filename VARCHAR2(50);
BEGIN
my_dir := 'c:\temp';
my_filename := 'Lab4AData.dat';
data_file := UTL_FILE.FOPEN(my_dir, my_filename, 'r');
LOOP
UTL_FILE.GET_LINE(data_file, data_line);
-- add code to do something with data_line here
END LOOP;
EXCEPTION
WHEN no_data_found THEN
DBMS_OUTPUT.PUT_LINE('Finished');
UTL_FILE.FCLOSE(data_file);
END;
#ShannonSeverance's comments about using directory objects with UTL_FILE.FOPEN are appropriate, except in the instance where your DBA has not embraced their use and insists on sticking with the "tried and true" INIT.ORA parameter UTL_FILE_DIR. Don't ask me how I know... :-)
Share and enjoy.

Resources