Last active
November 20, 2018 11:29
-
-
Save robsonfaxas/f5f43ea5c4e95ab5d3c8576e9361e9f3 to your computer and use it in GitHub Desktop.
#oracle Select table into table of objects, change values and put into a sys_refcursor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Object in Oracle. Prefer this one for queries. | |
-- don't use Records, records are PL/SQL types and cannot be used in SQL queries | |
create or replace type OBJETO_OSSPP is object( | |
PORT_ID NUMBER(5), | |
OPP_DESCRIPTION VARCHAR2(50), | |
OPP_STATUS CHAR(1), | |
OPP_REGDATE DATE, | |
OPP_REGUSER VARCHAR2(20) | |
);/ | |
create or replace type TABLE_OBJETO_OSSPP as table of OBJETO_OSSPP; / | |
-- Source: https://stackoverflow.com/questions/19192162/pass-record-into-ref-cursor | |
-- And: https://stackoverflow.com/questions/30305161/is-it-possible-to-perform-a-select-into-table-of-object-type | |
CREATE OR REPLACE PROCEDURE PROC_PARA_TESTES | |
( | |
P_PROC_VERSION IN NUMBER , -- PARAMETRO PADR?O - N?O DEVE SER REMOVIDO | |
P_RESULT OUT NUMBER , -- PARAMETRO PADR?O - N?O DEVE SER REMOVIDO | |
P_RESULT_MSG OUT VARCHAR2 , -- PARAMETRO PADR?O - N?O DEVE SER REMOVIDO | |
P_ID_PORTA IN NUMBER, | |
P_CURR OUT SYS_REFCURSOR -- DESCRIC?O DA UTILIZAC?O DO P_CAMPO2 | |
) | |
IS | |
-- OBJETO_OSSPP -> MESMOS CAMPOS DA OSS_PORTPLACEMENT | |
-- TABLE_OBJETO_OSSPP | |
V_PROC_NAME VARCHAR2(50) DEFAULT 'PROC_PARA_TESTES'; | |
V_CURRENT_OBJECT_VERSION NUMBER(5) DEFAULT 18001; -- NUMERO DA VES?O NO FORMATO YYVVV ONDE YY = ANO E VVV = NUMERO SEQUENCIAL DA VERS?O | |
PORT_PLACES TABLE_OBJETO_OSSPP; | |
BEGIN | |
DBO_CHECKVERSION( V_PROC_NAME, V_CURRENT_OBJECT_VERSION, P_PROC_VERSION ); | |
P_RESULT := -1; | |
P_RESULT_MSG := ''; | |
SELECT OBJETO_OSSPP(p.port_id, p.opp_description, p.opp_status, p.opp_regdate, p.opp_reguser) | |
BULK COLLECT INTO PORT_PLACES | |
FROM OSS_PORTPLACEMENT p; | |
for i in 1 .. PORT_PLACES.COUNT LOOP | |
IF (PORT_PLACES(i).PORT_ID NOT IN (801) ) then | |
ins_oss_log(15001,port_places(i).port_id,sysdate,0,'A','PROC_PARA_TESTES'); | |
PORT_PLACES.delete(i); | |
end if; | |
END LOOP; | |
OPEN P_CURR FOR | |
SELECT * | |
FROM table(PORT_PLACES); | |
-- P_CURR = 801 record from oss_portplacement | |
EXCEPTION | |
-- ERRO EM CONSULTA ONDE NENHUM REGISTRO E RETORNADO | |
WHEN NO_DATA_FOUND THEN | |
P_RESULT := -2; | |
P_RESULT_MSG := V_PROC_NAME || ': ERROR - NO_DATA_FOUND - ' || SQLERRM; | |
RAISE; | |
-- OUTROS TIPOS DE ERROS N?O TRATADOS | |
WHEN OTHERS THEN | |
P_RESULT := -3; | |
P_RESULT_MSG := V_PROC_NAME || ': ERROR - ' || SQLERRM; | |
RAISE; | |
END PROC_PARA_TESTES; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment