creating pl/sql procedure to get different columns from different tables - oracle

I'm trying to create a procedure to get the data from different columns of different tables. I have written this code but I get error PLS-00103: Encountered the symbol "DECLARE" when expecting one of
the following:
begin function package pragma procedure subtype type use
form
current cursor external language
The symbol "begin" was substituted for "DECLARE" to continue. can anybody
create or replace procedure List_of_mfi
(
cq out sys_refcursor
)
as
begin
open cq for SELECT a.mfi_name,
b.mfi_type_name,
c.mfi_tier_name,
d.mfi_state_name,
e.mfi_district_name,
a.mfi_phone_no
FROM mfi_master a,
mfi_type_master b,mfi_tier_new c,mfi_state_master d,mfi_district_master e
WHERE a.mfi_type_id = b.mfi_type_id(+)
and a.mfi_tier_id = c.mfi_tier_id(+)
and a.mfi_state_id = d.mfi_state_id(+)
and a.MFI_DISTRICT_ID=e.MFI_DISTRICT_ID(+);
end;
declare
cq sys_refcursor;
mfi_name varchar2(150);
mfi_type_name varchar2(150);
mfi_tier_name varchar2(150);
mfi_state_name varchar2(150);
mfi_district_name varchar2(150);
mfi_phone_no varchar2(150);
--SELECT a.mfi_name, b.mfi_type_name, c.mfi_tier_name, d.mfi_state_name,e.mfi_district_name, a.mfi_phone_no
begin
List_of_mfi(cq);
loop
fetch cq into mfi_name,mfi_type_name,mfi_tier_name,mfi_state_name,mfi_district_name,mfi_phone_no;
EXIT WHEN cq%NOTFOUND;
--print
dbms_output.put_line('mfi_name:'||mfi_name);
dbms_output.put_line('mfi_type_name:'||mfi_type_name);
dbms_output.put_line('mfi_tier_name:'||mfi_tier_name);
dbms_output.put_line('mfi_state_name:'||mfi_state_name);
dbms_output.put_line('mfi_district_name:'||mfi_district_name);
dbms_output.put_line('mfi_phone_no:'||mfi_phone_no);
end loop;
end;
Since I'm a beginner I do not know if this code is correct or wrong. I want to find out how to edit this and make it work using oracle reports builder.Thanks everyone.

end;
declare
You must put forward slash "/" after the END keyword to terminate the procedure block before you execute another PL/SQL anonymous block.
Put "/" after each END:
END;
/

Related

How to call a stored procedure in pl/sql developer

I am new at this and have a simple question.
I have created a procedure like so in pl/sql developer
CREATE OR REPLACE PROCEDURE myproc2 AS
BEGIN
SELECT cd_desc des, cd_value cd FROM v_codes WHERE cd_type='CVS02'
END;
Now I want to call the procedure and see the output however when I run this
BEGIN
myproc2;
END;
in Pl/sql I am getting an error saying object myproc2 is invalid
How do I call a stored procedure in PL/SQL?
You're calling it right, but the procedure is wrong. If you check its status, it is invalid.
In PL/SQL, a SELECT requires INTO:
CREATE OR REPLACE PROCEDURE myproc2 AS
l_cd_desc v_codes.cd_desc%type;
l_cd_value v_codes.cd_value%type;
BEGIN
SELECT v.cd_desc, v.cd_value
INTO l_cd_desc, l_cd_value
FROM v_codes v
WHERE v.cd_type = 'CVS02';
END;
Beware of possible NO_DATA_FOUND or TOO_MANY_ROWS exception.
Also, although it'll now run OK (I guess), you won't see anything because it is unknown what you'll do next. You could, for example, choose to display values you fetched. In that case, add
<snip>
WHERE v.cd_type = 'CVS02';
dbms_output.put_line(l_cd_desc ||', '|| l_cd_value);
END;
Don't forget to enable serveroutput.
As you commented, you got too_many_rows. How to handle it? It depends on what you want to do. One option is to switch to a cursor FOR loop; now you don't need local variables and - as there's no SELECT statement itself - no INTO clause either:
CREATE OR REPLACE PROCEDURE myproc2
AS
BEGIN
FOR cur_r IN (SELECT v.cd_desc, v.cd_value
FROM v_codes v
WHERE v.cd_type = 'CVS02')
LOOP
DBMS_OUTPUT.put_line (cur_r.cd_desc || ', ' || cur_r.cd_value);
END LOOP;
END;
One great thing about Oracle SQL Developer is the GUI and that it does things for you.
You can open a sheet and run it the traditional way:
BEGIN
PROCEDURENAME(PARAM);
END;
or you can use the GUI, find the object with the (View->) Find DB object, find it, click on it and use the green arrow in the toolbar. It will open a UI for any parameters you used within the procedure.
In SQL Developer, if you want to see the output then you can return a cursor:
CREATE OR REPLACE PROCEDURE myproc2(
o_cursor OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN o_cursor FOR
SELECT cd_desc AS des,
cd_value AS cd
FROM v_codes
WHERE cd_type='CVS02'; -- You need a ; statement terminator here.
END;
/
Then you can use:
-- Declare a cursor bind variable
VARIABLE cur SYS_REFCURSOR;
BEGIN
-- Call the cursor outputting into the bind variable.
myproc2(:cur);
END;
/
-- Print the cursor
PRINT :cur;
And run it as a script (using F5).

Execute PROCEDURE on Oracle PL/SQL with DECLARE variable

I just currently learning about Oracle PL/SQL. I wanna create store procedure with variable and then call it with another script. Is it possible?
I tried use simple script without variable and it works:
CREATE OR REPLACE PROCEDURE testmyproc AS
BEGIN
INSERT INTO tes_table(dt)
VALUES (sysdate);
commit;
END testmyproc;
Then I call it with another script abc.sql
begin
testmyproc;
end;
It works successfully.
But, unfortunately if I use DECLARE (variable) at my PROCEDURE, it show error when I execute (but it success in create procedure).
Here's my PROCEDURE (no error):
CREATE OR REPLACE PROCEDURE sp_testmyproc AS
DECLARE
job_name varchar(100);
status_key number;
status_desc varchar(100);
notes varchar(250);
BEGIN
status_key := 1;
status_desc := 'SUCCESS';
notes := 'Process Completed';
INSERT INTO automation_log(job_name, dt, status_key, status_desc, notes)
VALUES (job_name, sysdate, status_key, status_desc, notes);
commit;
END sp_testmyproc;
Here's my execure script abc.sql (show error when i execute it)
-without DECLARE
begin
sp_testmyproc;
end;
-I tried to execute it with DECLARE
DECLARE
job_name varchar(100);
status_key number;
status_desc varchar(100);
notes varchar(250);
begin
status_key := 1;
status_desc := 'SUCCESS';
notes := 'Process Completed';
sp_testmyproc;
end;
It show error like this:
> ORA-06550: line 8, column 11:
> PLS-00905: SP_TESTMYPROC is invalid
> ORA-06550: line 8, column 3:
> PL/SQL: Statement ignored
Can I call Procedure for another script? Is It best practice?
I just think PROCEDURE can be used for many cases (something like function in programming).
Thank you!
You need to learn the syntax of the procedure.
In Procedure, You should not use the keyword DECLARE. Any variables you want to declare must be between AS and BEGIN in the procedure.
Your procedure should look like follows:
CREATE OR REPLACE PROCEDURE sp_testmyproc AS
--DECLARE
job_name varchar(100);
status_key number;
status_desc varchar(100);
notes varchar(250);
BEGIN
status_key := 1;
.....
.....
.....
Refer to this document for the syntax of the oracle procedure as it is very easy to follow.
Please note the difference when creating a stored procedure in SQL*Plus:
SQL> create or replace procedure test_ok as
2 v number;
3 begin
4 v:=0;
5 end;
6 /
Procedure created.
SQL> show errors
No errors.
SQL> create or replace procedure test_ko as
2 declare
3 v number;
4 begin
5 v:=0;
6 end;
7 /
Warning: Procedure created with compilation errors.
SQL> show errors
Errors for PROCEDURE TEST_KO:
LINE/COL ERROR
-------- -----------------------------------------------------------------
2/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
SQL>
If you have compilation errors you get at least Warning: Procedure created with compilation errors. If you have compilation errors and use show errors you get all error messages.

How to write an Oracle procedure with a select statement (Specifically on SQL Developer)?

I want to create a simple Oracle Stored procedure on SQL Developer that will return some records on a simple select query. I do not want to pass in any parameter, but I just want the Records to be returned back from the procedure into a result set -> a suitable variable.
I have been trying to use the following syntax :
create or replace PROCEDURE Getmarketdetails2(data OUT varchar2)
IS
BEGIN
SELECT *
into data
from dual;
END Getmarketdetails2;
But it gives me an error while I try to execute with the following exec statement -->
Declare a Varchar2;
exec Getmarketdetails2(a);
Error: PLS-00103: Encountered the symbol "end-of-file" when expecting "something else".
Cause: Usually a PL/SQL compilation error.
Appreciate if anyone can help me out of this long pending situation! I have tried enough to find a basic guide to create a simple Oracle stored procedure and execute it in SQL Developer, but none of them answer to the point!!
You want:
DECLARE
a VARCHAR2(4000); -- Give it a size
BEGIN -- Begin the anonymous PL/SQL block
Getmarketdetails2(a); -- Call the procedure
DBMS_OUTPUT.PUT_LINE( a ); -- Output the value
END; -- End the anonymous PL/SQL block
/ -- End the PL/SQL statement
or:
VARIABLE a VARCHAR2(4000); -- Create a bind variable
EXEC Getmarketdetails2(:a); -- Execute the procedure using the bind variable
PRINT a -- Print the bind variable
Assuming an up-to-date Oracle version, you can use dbms_sql.return_result()
create or replace PROCEDURE Getmarketdetails2
IS
c1 SYS_REFCURSOR;
BEGIN
OPEN c1 FOR
SELECT *
from dual;
DBMS_SQL.RETURN_RESULT(c1);
END Getmarketdetails2;
/
Then simply run
exec Getmarketdetails2
The only drawback is that SQL Developer only displays the result as text, not as a proper result grid.
This is how I return a cursor in Oracle
PROCEDURE GetAllData (P_CURSOR OUT SYS_REFCURSOR)
IS
BEGIN
OPEN P_CURSOR FOR
SELECT *
FROM TABLE ;
END GetAllData ;
Declare a Varchar2;
exec Getmarketdetails2(a);
Your procedure is ok;
Instead of above query, use below query to run sp:
Declare
a Varchar2(10);
Begin
Getmarketdetails2(a);
End;

Can I execute procedure after select?

I am trying to make a script, which will verify my stored procedure's function. However, once I put execute store procedure there, I get this kind of error: PLS-00103: Encountered the symbol "NEW_PRODUCT" when expecting one of the following:
:= . ( # % ; immediate
The symbol ":=" was substituted for "NEW_PRODUCT" to continue.
My script looks like this:
DECLARE
id_unit number;
id_unit_old number;
var_materialid number;
var_processid number;
serial_no varchar(30);
product_name_new varchar(30);
BEGIN
SELECT max(id) INTO id_unit_old
FROM Unit;
execute new_product('NTB', 'Diablo', 'CPU-I7', 'GPU-M800', 'RAM-K2');
SELECT max(id) INTO id_unit
FROM Unit;
IF id_unit > id_unit_old THEN BEGIN
SELECT id_material, id_process, serial_number INTO var_materialid, var_processid, serial_no
FROM Unit
WHERE id = id_unit;
SELECT name INTO product_name_new
FROM Material
WHERE id = var_materialid;
ELSE
RAISE_APPLICATION_ERROR(-20002, 'Insert to table Unit has failed!');
END IF;
END;
Any suggestions what is the problem and how can I solve it? Thanks
Get rid of the execute word right before the new_product word, when you call a PL/SQL procedure from another procedure or from PL/SQL anonymous block you must call it by its name.

TORA Execute Package Procedure with OUT REF CURSOR

I'm trying to execute a Package Procedure that has a couple in parameters and a REF CURSOR out parameter but can't seem to figure it out for TORA (my SQL IDE)
Here is a contrived example of a procedure I'd like to execute and see the cursor for:
PROCEDURE get_peeps_in_city ( pi_city IN varchar(100), po_recordset OUT REF CURSOR )
IS
BEGIN
OPEN po_recordset
FOR Select Id,
FName,
LName
FROM People
WHERE City like '%' || pi_city || '%'
END;
Here is what I've tried so far:
DECLARE
v_cursor REF CURSOR;
BEGIN
execute PKG_PEEPS.get_peeps_in_city('Chicago', v_cursor);
END
The error that I get is something along the lines of:
PLS-00103: Encountered the symbol "END" when expecting one of the
following:
begin function package pragma procedure subtype type use form current
cursor
You're probably looking for something like this:
DECLARE
v_cursor SYS_REFCURSOR;
v_Id NUMBER;
v_FName VARCHAR2(200);
v_LName VARCHAR2(200);
BEGIN
PKG_PEEPS.get_peeps_in_city('Chicago', v_cursor);
LOOP
FETCH v_cursor INTO v_Id, v_FName, v_LName;
EXIT WHEN v_cursor%NOTFOUND;
-- do something with v_Id, v_FName, v_LName
END LOOP;
CLOSE v_cursor;
END;
/
Update:
You probably would like that the result of your query is displayed in your IDE as if you had run the SELECT statement directly. This is not going to happen with the above code and I don't know of any code that could achieve that (unless you install a specific table function).
The best thing you can do is output the retrieved data in the loop (using DBMS_OUTPUT.PUT_LINE).

Resources