I need to write a reference cursor in Oracle SQL developer and I have no clue how its written. I just know basic queries.
I have written following cursor to best of my knowledge after looking up into various example. I am getting an compile error "Missing or Invalid option".
Can anyone help me out?
Step 1: I created a new procedure in Oracle sql developer version 4.
Step 2: I wrote the following cursor
DECLARE
routeid VARCHAR2(10);
cursor c1 IS
SELECT shipment_id FROM SHIPMENT
WHERE shipment_id = 20;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO routeid;
EXIT WHEN c1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(routeid);
END LOOP;
END;
/
Thank you in advance for the help.
Better way to do simple cursor
CREATE OR REPLACE PROCEDURE Test_cursor (Out_Pid OUT VARCHAR2) AS
cursor c1 IS
SELECT shipment_id,p_id FROM test
WHERE shipment_id = 99;
c1_rec c1%rowtype;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO c1_rec;
EXIT WHEN c1%NOTFOUND;
Out_Pid := c1_rec.p_id;
DBMS_OUTPUT.PUT_LINE('Result from query '||c1_rec.p_id );
DBMS_OUTPUT.PUT_LINE('Result from out parameter '||Out_Pid );
END LOOP;
END Test_cursor;
Related
Hi i'm creating a database with DB2. I use IBM Data Client. I want to use try catch into my stored procedure but it seems that are not supported by DB2, can any one help me? I need to handle sql errors and to return its. How can i do that?
DB2 LUW supports exception handlers (continue handlers, or exit handlers) for SQL PL procedures. Look in the DB2 Knowledge Center for your version for all the details. You can use them alongside conditions. You can have multiple handlers if you need specific processing. There are plenty of sample SQL PL procedures in both the Knowledge Center and in the DB2 LUW installed product directories.
CREATE OR REPLACE PROCEDURE sp_Applicazione_Aggiorna
(
IN #VAR1 INT,
IN #VAR2 INT,
IN #VAR3 VARCHAR(16),
OUT #ReturnCode INTEGER,
)
LANGUAGE SQL
P1: BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION, SQLWARNING, NOT FOUND
SET #ReturnCode = SQLCODE;
IF not exists (select VAR1 from DB.TABLEA where VAR1 = #VAR1)
THEN
set #ReturnCode = 3011;
ELSE
UPDATE DB.TABLEA SET
VAR2=#VAR2,
VAR3=#VAR3
WHERE VAR1=#VAR1;
END IF;
END P1
I am trying to retrieve multiple rows from table using stored procedure and it doesn't work for me. My stored procedure is as follows,
CREATE OR REPLACE PROCEDURE E_Enquiry
(IN SourceQueue1 VARCHAR(30) ) LANGUAGE SQL
BEGIN
DECLARE C1 CURSOR FOR
select CreationTime
from ms.Exception_Message
where SourceQueue = SourceQueue1;
open c1;
END;
I am trying to call the stored procedure from Mule Anypoint Studio using the Database connector and have seen the result as null when I print the payload with a logger. If I try to modify the query as it returns a single row it works (without using cursor).
Please help to resolve this. Thanks.
What platform and version of DB2?
Try the adding
DYNAMIC RESULT SETS 1
WITH RETURN TO CLIENT
Like so:
CREATE OR REPLACE PROCEDURE E_Enquiry
(IN SourceQueue1 VARCHAR(30) )
LANGUAGE SQL
DYNAMIC RESULT SETS 1
BEGIN
DECLARE C1 CURSOR WITH RETURN TO CLIENT FOR
select CreationTime
from ms.Exception_Message
where SourceQueue = SourceQueue1;
open c1;
END;
I'm starting with PL/SQL, this is my first Procedure and I find it difficult to compile; I have tried so many different versions, I carry the last attempt. I don't understand why SQLDEVELOPER say me: "procedure compiled (with errors)".
The compiler say me:" Errore(10,1): PLS-00103: Trovato il simbolo (find) "DECLARE" instead: begin function pragma procedure subtype type current cursor delete exists prior "
If there are other errors (also logical) please tell me. I would like to improve.
Thank you all for the answers
My Procedure:
create or replace PROCEDURE calcola_giorn (giornata IN INTEGER) is
-- si tenga presente che in realtà giornata=idPartita
somma NUMBER;
idcal NUMBER;
nometorn VARCHAR2;
idformaz NUMBER;
nomesquadr VARCHAR2;
DECLARE;
SELECT idcalendario INTO idcal FROM partita WHERE id= giornata;
SELECT nometorneo INTO nometorn FROM calendario WHERE id= idcal;
CURSOR formazioni_di_giornata IS
SELECT id, nomesquadra FROM formazione where idpartita= giornata;
CURSOR giocatori_di_giornata IS
SELECT votogiocatore FROM schiera WHERE idformazione= idformaz;
Begin
OPEN formazioni_di_giornata;
FOR tupla_formazione IN formazioni_di_giornata LOOP
somma:=0;
FETCH formazioni_di_giornata INTO idformaz, nomesquadr;
OPEN giocatori_di_giornata;
FOR tupla_giocatore IN giocatori_di_giornata LOOP
somma:= somma + tupla_giocatore.votogiocatore;
END LOOP;
CLOSE giocatori_di_giornata;
UPDATE partecipa SET punti= somma WHERE ( (nomesquadra= nomesquadr) AND (nometorneo= nometorn));
END LOOP;
CLOSE formazioni_di_giornata;
EXCEPTION WHEN OTHERS THEN raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
END calcola_giorn;
Varchar limit should be defined, like varchar2(100)
Replace DECLARE; with BEGIN
All your cursors should go before BEGIN
Put END; for the procedure
You can try compiling after making these changes.
Below is the procedure I have created. When I execute it I'm getting "Procedure created with compilation errors" I don't understand where I did mistake in below procedure code, someone help me by finding error in the code
create or replace PROCEDURE newprocedur(inMerid IN VARCHAR2,outCount OUT NUMBER) AS
CURSOR c1 IS
select CLIENT_COUNT
from OP_TMER_CONF_PARENT
where MER_ID = inMerid
FOR UPDATE OF CLIENT_COUNT;
BEGIN
Open c1
loop
fetch c1 into outCount;
exit when c1%NOTFOUND;
outCount:=outCount+1;
update OP_TMER_CONF_PARENT
set CLIENT_COUNT = outCount
where current of c1;
end loop;
close c1;
END;
Thanks in advance
1- check for errors and see where error is
See here
2- You missed a semicolon in 5th line-
should be
Open c1;
In SQL*Plus you can use the command
show errors
to show you the errors in the PL/SQL code that was just compiled. See http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12041.htm
I'm not familiar with PL/SQL Developer, but TOAD does this automatically and show you the errors allowing you to quickly jump to the line(s) containing the error(s).
In this link IBM they explain how to use an Oracle stored procedure with COGNOS.
They are doing :
create or replace package body project_pk as
procedure project_sp (tproj IN numeric, result1 in out project_type1) is
begin
open result1 for
select projects.projectid, projects.projecttype
from projects
where projects.projecttype=tproj;
end;
end;
/
With open result1 for they are opening the cursor explicitly. But, they do not seem to close it. Why is this?
You must return resuly set to Cognos. If you close the cursor then there are nor reuslts, right?
It's Cognos responsibility to close the cursor, once it finishes to pull the data from the SP.
In order to make you 100% sure that this is the case look at this link (totaly unrelated to Cognos):
Returning result Sets from SQL Server and Oracle
However, the sample you gave in your link, looks quite complex. Here is what I am using:
CREATE OR REPLACE PROCEDURE "COGNOS_SP" (
case_id in numeric,
po_refcur out sys_refcursor) is
BEGIN
open po_refcur for
select * FROM CASES WHERE CASE_ID = case_id;
END COGNOS_SP;