Dynamic query in Cursor for %rowtype - stored-procedures

I need to create a record type to contain the result set of the above query of
V_SQL VARCHAR2(200) := 'SELECT T1.'||record_id||',T1.'||card_no||',T2.TEST_CARD_NO, T1.'||type_cc||' FROM '|| table_name||' T1
INNER JOIN TEST T2 ON
T2.ID = T1.'||record_id;
So that I can use t1 C_BRNGB_EXTRACT%ROWTYPE (plus another column for t2.col4%TYPE inside l_BRNGB_EXTRACT) inside loop execute statement
CREATE OR REPLACE PROCEDURE "GCCPMAINT"."FUNCTION_CURSOR"(table_name VARCHAR2,record_id VARCHAR2,card_no VARCHAR2,type_cc VARCHAR2)
AS
limit_in NUMBER:=100;
V_SQL VARCHAR2(200) := 'SELECT T1.'||record_id||',T1.'||card_no||',T2.TEST_CARD_NO, T1.'||type_cc||' FROM '|| table_name||' T1
INNER JOIN TEST T2 ON
T2.ID = T1.'||record_id;
TYPE BRNGB_EXTRACT IS REF CURSOR;
C_BRNGB_EXTRACT BRNGB_EXTRACT;
TYPE BRNGB_EXTRACT1 IS TABLE OF C_BRNGB_EXTRACT%ROWTYPE;
l_BRNGB_EXTRACT BRNGB_EXTRACT1;
BEGIN
OPEN C_BRNGB_EXTRACT FOR V_SQL;
LOOP
FETCH C_BRNGB_EXTRACT BULK COLLECT INTO l_BRNGB_EXTRACT LIMIT limit_in;
EXIT WHEN l_BRNGB_EXTRACT.COUNT = 0;
FORALL indx IN 1 .. l_BRNGB_EXTRACT.COUNT
EXECUTE IMMEDIATE 'UPDATE table_name SET CARD_NO=:1 WHERE CARD_NO=:2 AND RECORD_ID=:3' USING l_BRNGB_EXTRACT(indx).test_card_no,l_BRNGB_EXTRACT(indx).CARD_NO,l_BRNGB_EXTRACT(indx).RECORD_ID;
END LOOP;
CLOSE C_BRNGB_EXTRACT;
COMMIT;
END;
For a above stored procedure I'm getting the following error
PLS-00320: the declaration of the type of this expression is
incomplete or malformed PL/SQL: Item ignored PLS-00597: expression
'L_BRNGB_EXTRACT' in the INTO list is of wrong type PL/SQL: SQL
Statement ignored PLS-00487: Invalid reference to variable
'C_BRNGB_EXTRACT%ROWTYPE'
Please help me to solve this.

i got the solution
TYPE BRNGB_EXTRACT IS REF CURSOR;
C_BRNGB_EXTRACT BRNGB_EXTRACT;
-- TYPE BRNGB_EXTRACT1 IS TABLE OF C_BRNGB_EXTRACT%ROWTYPE;
TYPE BRNGB_EXTRACT1 IS RECORD (
record_id varchar(30),
card_no varchar(30),
TEST_CARD_NO varchar(30)
);
type test_rec_arr is table of BRNGB_EXTRACT1 index by pls_integer;
l_BRNGB_EXTRACT test_rec_arr;

Related

Set a variable value in stored procedure and use in following query

How can I set a variable in stored procedure and use it in following query to be executed.
create or replace procedure sp1()
returns table (dealer_id varchar, dealershipgroup_name varchar)
language sql
as
$$
declare
create_query varchar;
res resultset;
MISSING_DEALER NUMBER(38,0) default 0;
begin
MISSING_DEALER := 100;
select_query := 'WITH CTE AS(
SELECT dealer_id,
CASE WHEN dealer_id=:MISSING_DEALER then \'Abc\'
WHEN dealershipgroup IS NULL then \'\'
ELSE dealershipgroup end as dealershipgroup FROM TBL )
select * from CTE';
res:= (execute immediate : select_query);
return table(res);
end;
$$;
call sp1();
Could someone please suggest how can I use MISSING_DEALER in the query. I am currently getting the following error
Uncaught exception of type 'STATEMENT_ERROR' on line 28 at position 9 : SQL compilation error: error line 8 at position 26 Bind variable :MISSING_DEALER not set
You need to concatenate the string parts of your SQL statement with the variable. This is covered in the documentation if you look at the end of the section here

Stored procedure is not getting complied

I am trying to run the below stored procedure in Oracle pl/sql. I am trying to fetch data from join in cursor using table 1 and table 2 and update the output in Table 3, however its giving me compliation error near declare. The queries are working fine.
create or replace PACKAGE BODY PKG_LOAD_BY_ROWID AS
PROCEDURE PRC_LOAD_BY_ROWID AS
DECLARE
N1 NUMBER;
VAR_ROWID_OBJECT VARCHAR2(255);
VAR_PRTY_FK VARCHAR2(255);
V_OUT_ERROR_MSG VARCHAR2(1000);
v_out_return_code number;
CURSOR C1 IS
SELECT PX.ROWID_OBJECT , A.PRTY_FK
FROM TABLE_1 PX
INNER JOIN
TABLE_2 A
ON
substr(PX.PKEY_SRC_OBJECT,8,INSTR(PX.PKEY_SRC_OBJECT,'|')+8)=A.ALT_ID_VAL
WHERE A.ALT_ID_TYP='DUMMY1' AND PX.ROWID_SYSTEM='SRC';
BEGIN
SELECT COUNT(1) INTO N1 FROM TABLE_3 WHERE SRC_SYSTEM='SRC2' AND ROWID_OBJECT IS NULL;
BEGIN
OPEN C1;
FOR i in 1..n1
LOOP
FETCH C1 INTO VAR_ROWID_OBJECT, VAR_PRTY_FK;
UPDATE TABLE3 SET ROWID_OBJECT= VAR_ROWID_OBJECT WHERE
SRC_KEY=VAR_PRTY_FK;
COMMIT;
END LOOP;
CLOSE C1;
v_out_return_code :=0;
DBMS_OUTPUT.put_line('Rowid_object updated successfully for VVA');
EXCEPTION
when others then
out_error_msg := 'Updation Error';
DBMS_OUTPUT.put_line (out_error_msg);
END;
END;
END PKG_LOAD_BY_ROWID;
However, I am getting compilation error:
Error(2,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
Oracle Version:
You don't need Declare keyword for the procedure, it is only for anonymous block. Just remove declare and try it.

Convert Procedure from Oracle to DB2, Error when try to update table on runtime

Hello DB2 Experts I need your assistance in converting below Procedure to something more dynamic.
We have to update multiple sequences with max of id column for each table.
CREATE PROCEDURE mySchema.UPDATE_SEQUENCE ( )
DYNAMIC RESULT SETS 1
MODIFIES SQL DATA
----------------------------------------------------------------------
-- SQL Stored Procedure
----------------------------------------------------------------------
P1: BEGIN
DECLARE counter BIGINT;
DECLARE q VARCHAR(500);
set (counter) = (select max(N_PRI_KEY) from mySchema.myTable);
set q = 'alter sequence mySchema.mySequence RESTART WITH ' || counter;
EXECUTE IMMEDIATE q;
END P1
#
This is what I have written from the above code:
Here I want N_PRI_KEY to be dynamic and mySchema.myTable to be updated on run time with the values from the table.
CREATE OR REPLACE PROCEDURE getText ()
LANGUAGE SQL
DYNAMIC RESULT SETS 1
BEGIN
DECLARE maxval INTEGER DEFAULT 0;
CALL DBMS_OUTPUT.PUT( 'a' );
FOR vrows AS
SELECT NAME, SEQUENCENAME, TBNAME FROM MAXSEQUENCE WHERE SEQUENCENAME='ASSETSEQ'
DO
SELECT MAX(vrows.NAME) INTO maxval FROM vrow.TBNAME; -- This is where I am getting error.
EXECUTE IMMEDIATE 'ALTER SEQUENCE '||vrows.SEQUENCENAME||' RESTART WITH '|| maxval;
END FOR;
END#
This is the error that I am getting when I try to create the Procedure.
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0204N "VROW.TBNAME" is an undefined name. LINE NUMBER=18. SQLSTATE=42704
When I run this line it works and inserts the max value in the TEMPOUTPUT table.
execute immediate 'INSERT INTO TEMPOUTPUT VALUES (select max('||vrow.NAME||') from '||vrow.TBNAME||')';
I have tried doing this but it did not work.
execute immediate 'ALTER SEQUENCE '||SEQUENCENAME||' RESTART WITH select max('||vrow.NAME||') from '||vrow.TBNAME;
Just for Reference - This is a procedure which is written in Oralce which is doing something similar.
declare
maxval int;
seqval int;
begin
for i in ( select ucc.column_name, s.sequence_name, uc.table_name
from user_cons_columns ucc,
user_constraints uc,
user_sequences s
where uc.constraint_name = ucc.constraint_name
and uc.constraint_type = 'P'
and ucc.position = 1
and s.sequence_name = 'SEQ_'||uc.table_name
)
loop
execute immediate 'select max('||i.column_name||') from '||i.table_name into maxval;
execute immediate 'select '||i.sequence_name||'.nextval from dual' into seqval;
dbms_output.put_line(maxval||','||seqval);
if maxval > seqval then
execute immediate 'alter sequence '||i.sequence_name||' increment by '|| ( maxval - seqval );
execute immediate 'select '||i.sequence_name||'.nextval from dual' into seqval;
execute immediate 'alter sequence '||i.sequence_name||' increment by 1';
execute immediate 'select '||i.sequence_name||'.nextval from dual' into seqval;
dbms_output.put_line(maxval||','||seqval);
end if;
end loop;
end;
Try the following instead of the row with SELECT MAX(...) where you get the error:
PREPARE S1 FROM 'SET ? = (SELECT MAX(' || vrows.NAME || ') FROM ' || vrows.TBNAME || ')';
EXECUTE S1 INTO maxval;

Create table with stored procedure in Teradata

I want to create a stored procedure where I can pass in variable to the WHERE clause below.
DROP TABLE fan0ia_mstr.Store_List;
CREATE TABLE fan0ia_mstr.Store_List AS(
SELECT
a11.ANA_Code,
a11.Premise_Name_Full,
a11.Store_Code,
a11.Estates_Segment,
a12.Post_Code
FROM Store_Dimension_Hierarchy a11
JOIN Location a12
ON a11.ANA_Code = a12.ANA_Code
WHERE a11.Area_Desc = 'VARIABLE' ) WITH DATA
PRIMARY INDEX (ANA_Code)
The VARIABLE will be a character string. I don't need to display the results, I just want the table to be created.
Also how do I trap any errors e.g. if the table doesn't exist for some reason I still want it to be created
thanks
As you don't have variable database/table/column names you simply need to wrap your existing code (slightly modified) into a Stored Procedure:
replace procedure myproc(IN variable varchar(100))
begin
BEGIN
-- simply try dropping the table and ignore the "table doesn't exist error"
DECLARE exit HANDLER FOR SQLEXCEPTION
BEGIN -- 3807 = table doesn't exist
IF SQLCODE <> 3807 THEN RESIGNAL; END IF;
END;
DROP TABLE fan0ia_mstr.Store_List;
END;
CREATE TABLE fan0ia_mstr.Store_List AS(
SELECT
a11.ANA_Code,
a11.Premise_Name_Full,
a11.Store_Code,
a11.Estates_Segment,
a12.Post_Code
FROM Store_Dimension_Hierarchy a11
JOIN Location a12
ON a11.ANA_Code = a12.ANA_Code
WHERE a11.Area_Desc = :variable ) WITH DATA
PRIMARY INDEX (ANA_Code);
end;
Of course a DELETE/INSERT or a Temporary table might be more efficient.
Edited... second option needs a execute immediate too...
CREATE PROCEDURE PROCEDURE1(
V_AREA_DESC IN VARCHAR2 )
AS
BEGIN
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE fan0ia_mstr.Store_List';
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
EXECUTE IMMEDIATE 'CREATE TABLE fan0ia_mstr.Store_List AS
(SELECT a11.ANA_Code,
a11.Premise_Name_Full,
a11.Store_Code,
a11.Estates_Segment,
a12.Post_Code
FROM Store_Dimension_Hierarchy a11
JOIN Location a12
ON a11.ANA_Code = a12.ANA_Code
WHERE a11.Area_Desc = ''' || v_area_desc || '''
) WITH DATA PRIMARY INDEX (ANA_Code)';
END PROCEDURE1;
but you can avoid drop / create with truncate / insert
CREATE PROCEDURE PROCEDURE1(
V_AREA_DESC IN VARCHAR2 )
AS
BEGIN
execute immediate 'truncate TABLE fan0ia_mstr.Store_List';
insert into fan0ia_mstr.Store_List (SELECT a11.ANA_Code,
a11.Premise_Name_Full,
a11.Store_Code,
a11.Estates_Segment,
a12.Post_Code
FROM Store_Dimension_Hierarchy a11
JOIN Location a12
ON a11.ANA_Code = a12.ANA_Code
WHERE a11.Area_Desc = v_area_desc
);
commit;
END PROCEDURE1;

check the null values for input parameters with oracle stored procedure

I am preparing stored procedure with oracle .I m using multiple inner joins with the same table where an input parameter value is checked within each inner join. I want to eliminate particular inner join if the input parameter is null
You would need to use dynamic SQL to construct the appropriate query for the parameters like this:
PROCEDURE myproc (p1 VARCHAR2) IS
l_sql LONG;
l_cursor SYS_REFCURSOR;
BEGIN
l_sql := 'SELECT a, b, c FROM table1';
IF p1 IS NOT NULL THEN
l_sql := l_sql || ' JOIN table2 ON table2.x = table1.x';
END IF;
l_sql := l_sql || ' WHERE table1.y = :bind1';
OPEN l_cursor FOR l_sql USING 123;
...
END;

Resources