Insert the output of a refcursor procedure into a table
HelloI met a scenario in which I need to put the insert records returned by a procedure using refcursor, at a table.
I followed the instructions in PL/SQL 101: understanding Refcursor (PL/SQL 101: understanding Ref Cursor am unfortunately still not able to do so.)
Here is my sample codes. (Copied here as advised by the new Member)
create or replace PROCEDURE TEST_PROCEDURE1 ( p_cursor OUT SYS_REFCURSOR)
AS
BEGIN
OPEN p_cursor FOR
SELECT C1,C2
FROM TEST_USER.test_table4procedure;
END;
I check the result using the following statement, which gives results.variable rc refcursor;
exec TEST_USER.TEST_PROCEDURE1 (:rc)
print rc;
Now, I want to be able to use the output and insert the data into a table. That's how I came across this thread.I created the types and function...
create or replace type test_user.type_table1 as object(var1 varchar2(50),var2 varchar2(50));
create or replace type test_user.type_table2 as table of test_user.type_table1;
create or replace function test_user.test_function1 (rc in sys_refcursor )
return test_user.type_table2 is
v_emptype test_user.type_table2 := test_user.type_table2(); -- Declare a local table structure and initialize it
v_cnt number := 0;
v_rc sys_refcursor;
v_var1 varchar2(20);
v_var2 varchar2(20);
begin
v_rc := rc;
loop
fetch v_rc into v_var1, v_var2;
exit when v_rc%NOTFOUND;
v_emptype.extend;
v_cnt := v_cnt + 1;
v_emptype(v_cnt) := test_user.type_table1(v_var1, v_var2);
end loop;
close v_rc;
return v_emptype;
end;
After that, I want to be able to view the records by using the function... so I used the instructions below...variable rc refcursor;
exec TEST_USER.TEST_PROCEDURE1 (:rc)
SELECT * FROM TABLE(test_user.test_function1(:rc));
However, it fails with the errorError from line 3 in order:
SELECT * FROM TABLE (test_user.test_function1 (:rc))
Error report:
SQL error: Missing a setting IN or OUT to index: 1
Help, please...
Your code does not work for a simple reason. SYS_REFCURSOR parameters must be in IN OUT mode. Check if the RC is open when he switched mode:
create or replace type type_table1 as object(var1 varchar2(50),var2 varchar2(50))
/
create or replace type type_table2 as table of type_table1
/
create or replace PROCEDURE TEST_PROCEDURE1 (p_cursor IN OUT SYS_REFCURSOR)
AS
BEGIN
OPEN p_cursor FOR
SELECT ENAME,JOB FROM EMP;
END;
/
create or replace function test_function1 (rc in sys_refcursor )
return type_table2 is
v_emptype type_table2 := type_table2(); -- Declare a local table structure and initialize it
v_cnt number := 0;
v_var1 varchar2(20);
v_var2 varchar2(20);
begin
if rc%isopen
then
dbms_output.put_line('rc is open');
else
dbms_output.put_line('rc is not open');
end if;
loop
fetch rc into v_var1, v_var2;
exit when rc%NOTFOUND;
v_emptype.extend;
v_cnt := v_cnt + 1;
v_emptype(v_cnt) := type_table1(v_var1, v_var2);
end loop;
close rc;
return v_emptype;
end;
/
variable rc refcursor
exec TEST_PROCEDURE1(:rc)
set serveroutput on
SELECT * FROM TABLE(test_function1(:rc))
/
SELECT * FROM TABLE(test_function1(:rc))
*
ERROR at line 1:
ORA-01001: invalid cursor
ORA-06512: at "SCOTT.TEST_FUNCTION1", line 15
rc is not open
SQL>
Now IN OUT parameter edit mode:
set serveroutput off
create or replace function test_function1 (rc in out sys_refcursor )
return type_table2 is
v_emptype type_table2 := type_table2(); -- Declare a local table structure and initialize it
v_cnt number := 0;
v_var1 varchar2(20);
v_var2 varchar2(20);
begin
if rc%isopen
then
dbms_output.put_line('rc is open');
else
dbms_output.put_line('rc is not open');
end if;
loop
fetch rc into v_var1, v_var2;
exit when rc%NOTFOUND;
v_emptype.extend;
v_cnt := v_cnt + 1;
v_emptype(v_cnt) := type_table1(v_var1, v_var2);
end loop;
close rc;
return v_emptype;
end;
/
variable rc refcursor
exec TEST_PROCEDURE1(:rc)
set serveroutput on
declare
v_emptype type_table2 := type_table2();
begin
v_emptype := test_function1(:rc);
end;
/
rc is open
PL/SQL procedure successfully completed.
SQL>
Problem is that you can't call the procedure/function with parameters in/out OUT of SQL.
SY.
Tags: Database
Similar Questions
-
Insert the result of a query select in another table
Hello
I have a strange problem when I try to insert all the results of a query select in another table, using the declaration of the order.
With the declaration of COMMAND it works fine
Work:
------------------------------
INSERT INTO ADART01 (SELECT (codart)
"STOCK". "" CODART_STO ".
Of
"DB". ' ' 'ACTIONS');
------------------------------
But if I try to sort the result using the declaration of the ORDER, I have the following error:
Error: ORA-00907 missing right parenthesis
------------------------------
INSERT INTO ADART01 (SELECT (codart)
"STOCK". "" CODART_STO ".
Of
"DB". "" "ACTIONS"
ORDER BY
"STOCK". ("' CODART_STO ASC ');
------------------------------
Any idea?
Thank you for your help,
Angel.delete "()" to select
create table test1 (a number, b varchar2(100)); insert into test1 (a, b) select level, 'level ' || level from dual connect by level < 101; insert into test1 (a, b) select level, 'level ' || level from dual connect by level < 101 order by to_char(sysdate - level, 'D'); select * from test1; drop table test1;
-
Storage of the outputs digital of an element in a table 1 d
Hi all
It must be simple and easy, but I don't seem to get it. A sample VI is attached.
I have a slider control which I do a manipulation to generate an output of the digital item. I can keep on sliding and get that the output generated, but now I need to store all the possible exits in a 1 d table.
Your help will be very appreciated.
Concerning
Yes quite simple and easy. You missed a few basics about LabVIEW. See the note to the modified VI.
(Also presented as snippette for those who don't mind the commitment of the DL
-
How to insert the output in the Oracle database table records
Nice day
I have two tables (table A and table B) in my oracle database I created a process for mapping from Table A duplicate records to Table B, and the result (reports) must be inserted into the Table B. I can't make this process. Can someone help me please.
Thank you and best regards,
Muhammed Faraz
Hi Muhammed,
You will need create an export job and set up a new job as a result. Take a look in the help topic "export data" as he will explain the different options [Add/Remove and insert/etc.].
Thank you
Nick
-
See the output of a stored procedure on APEX
Hello
I have a PL/SQL stored procedure that will be executed when you press "SUBMIT" button.
Inside of this procedure, it executes a DDL statement and keep the result of the DOF in a variable on each LOOP. The value of this variable will be replaced with the next run.
For example.
For i in 1.ULIMIT loop
run immediately 'alter var2 (i) blah blah... ". » ;
result message: = "works ok";
EXCEPT
WHILE OTHERS THEN
result_message: = "error";
END LOOP;
I guess I can create a large number of hidden objects to store the value of the variable of my procedure, but I don't think it's a good idea? Because the ULIMIT value can be changed. Is it possible that I can keep the value of this variable in each loop and display in a report of the APEX?If you plan to get answer to your questions in the future, you must mark responses as useful or correct if they indeed helped you solve your problems.
Denes Kubicek
-------------------------------------------------------------------
http://deneskubicek.blogspot.com/
http://www.Opal-consulting.de/training
http://Apex.Oracle.com/pls/OTN/f?p=31517:1
------------------------------------------------------------------- -
Load the data from a text file into a table using pl/sql
Hi Experts,
I want to load the data from a text file (sample1.txt) to a table using pl/sql
I used the pl/sql code below
***********************************
declare
f utl_file.file_type;
s varchar2 (200);
c number: = 0;
Start
f: = utl_file.fopen('TRY','sample1.txt','R');
loop
UTL_FILE.get_line (f, s);
insert into sampletable (a, b, c) values (s, s, s);
c: = c + 1;
end loop;
exception
When NO_DATA_FOUND then
UTL_FILE.fclose (f);
dbms_output.put_line('No. deles de lignes insérées: ' || c);
end;
***************************************
and my sample1.txt file looks like
***************************************
1
2
3
***************************************
Gets the data inserted, with way below
Select * from sampletable;
A, B AND C
1-1-1
2-2-2
3 3 3
I want that data to get inserted as
A, B AND C
1 2 3
The text file I have is to have three lines, and the first value of each line should go to each column
Help, please...
Thank youdeclare f utl_file.file_type; s1 varchar2(200); s2 varchar2(200); s3 varchar2(200); c number := 0; begin f := utl_file.fopen('TRY','sample1.txt','R'); utl_file.get_line(f,s1); utl_file.get_line(f,s2); utl_file.get_line(f,s3); insert into sampletable (a,b,c) values (s1,s2,s3); c := c + 1; utl_file.fclose(f); exception when NO_DATA_FOUND then if utl_file.is_open(f) then utl_file.fclose(f); ens if; dbms_output.put_line('No. of rows inserted : ' || c); end;
SY.
-
Cannot change the output of the application
I use a small set-up at home to record electric guitar. I got my release of guitar through an audio interface and in my computer via a USB cable, the guitar output then goes through a low latency driver and effects software called "amplitube 3.
I would like to follow the sound coming from the software, however, the application does not appear in my sound mixer. Instead, the output of it goes back into the audio interface (it is recognized as a playback device) for the follow-up of its headphone output. There is a problem such as the headphone output of the interface also monitors the sound of the guitar until it reaches my computer and so the two sounds play together leaving a chorus - like junk. the sound of the guitar before it reaches the computer cannot be resolved by the volume which also adjusts the other output.It is also worth noting as the input audio and output to "amplitube 3" can be changed in the program itself however, when the interface is selected as the input, the output is defined as both interface and cannot be changed as long as the interface is at the entrance.It may sound complicated so simply, I would like to be able to edit the output of amplitube 3.Thank you for taking the time to read thisJoeHello
You can contact the manufacturer of the application for support or you can use your favorite search engine to find an app that suits your requirement. -
Insert the select statement result in CLOB
Hi, I would like to insert the result of a statement select into a CLOB with a trigger.
Whevner a number is registered my research to trigger how often the number is used in several objects and now my problem I also would like to know the record ID where the number is used.
SELECT 'ITEXT '.
OF 'CHECK_INR '.
WHERE 'USE' > 1
This select records of results 2 How can I insert the result into a CLOB with my trigger?
Thanks in advanceSteven,
It's really a question better suited for another forum because it is not really associated with ApEx. Also, something tells me that you really want to do an UPDATE not an insert. However, here's a quick example...
DECLARE l_orders VARCHAR2(4000); BEGIN FOR x IN ( SELECT * FROM my_orders_table ) LOOP l_orders := l_orders || ', ' || x.order_data_column; END LOOP; l_orders := l_trim(l_orders, ', '); UPDATE some_table SET some_column = l_orders WHERE id = some_id; END;
That's the key. Use VARCHAR2 unless you need CLOB.
Kind regards
Danhttp://danielmcghan.us
http://sourceforge.NET/projects/tapigen -
[Insert values in table with the output values of the procedure]
Hello
I wrote the following procedure
<>PROCEDURE
create or replace procedure experience is
cursor curexp is e.employee_id, trunc (months_between (jh.end_date, jh.start_date)) select exp, e.first_name, e.last_name, d.department_name, l.city, c.country_name, l.street_address
e employees, job_history jh, departments d, places the country c
where e.employee_id = jh.employee_id
and e.department_id = d.department_id
and d.location_id = l.location_id
and l.country_id = c.country_id;
v_result curexp % rowtype;
Start
Open curexp;
loop
extract the curexp in v_result;
When the output curexp % notfound;
dbms_output.put_line (v_result.employee_id |) ' '|| v_result.exp | ' '|| v_result.first_name | v_result.last_name);
end loop;
end;
< / procedure >
to get the values as below
< output >
101 NeenaKochhar 49
101 NeenaKochhar 40
102 66 LexDe Haan
114 DenRaphaely 21
122 PayamKaufling 11
176 helenethibaut 9
176 helenethibaut 11
200 JenniferWhalen 69
200 JenniferWhalen 53
201 MichaelHartstein 46
< / output >
After that, I created a table as below
< table >
create table exp_recored (employee_id number, number of exp_mon, name varchar2 (20))
< /table >
Now I want to insert values in the table above
I tried like below
< Insert >
Start
insert into exp_recored (employe_id, exp_mon, name) values (experience);
end;
< / insetr >
but I got error like below
PL/SQL: ORA-00947: not enough values
Could someone tell me please on the above point
Thank you
Thelakthelakbe wrote:
HelloI tried too
During this operation
Start
insert into exp_recored (employe_id, exp_mon, name) values (v_result.employee_id, v_result.exp, v_result.first_name | v_result.last_name);
end;I got the below error
PL/SQL: ORA-00984: column not allowed here
You can try this?
CREATE OR REPLACE PROCEDURE experience IS CURSOR curexp IS SELECT e.employee_id, TRUNC(months_between(jh.end_date,jh.start_date)) exp, e.first_name, e.last_name, d.department_name, l.city, c.country_name, l.street_address FROM employees e, job_history jh, departments d, locations l, countries c WHERE e.employee_id=jh.employee_id AND e.department_id=d.department_id AND d.location_id =l.location_id AND l.country_id =c.country_id; v_result curexp%rowtype; BEGIN OPEN curexp; LOOP FETCH CUREXP INTO V_RESULT; EXIT WHEN CUREXP% NOTFOUND; INSERT INTO EXP_RECORED (EMPLOYEE_ID,EXP_MON,NAME) VALUES (V_RESULT.EMPLOYEE_ID,V_RESULT.EXP,V_RESULT.FIRST_NAME||V_RESULT.LAST_NAME); END LOOP; COMMIT; END;
-
CAPTURE THE OUTPUT IN INSERT INTO SELECT AND DELETE
Hi all
INSERT into employee_history_arch select * from employee_history where employee_status = 'CP' and action_dt < (sysdate-32);
commit;
delete from employee_history where employee_status = 'CP' and action_dt < (sysdate-32);
commit;
I must capture both the output of the insert and delete the sql query and compare the two values for validation. I shouldn't use select count (*) before the delete statement. I shud exactly capture the output... How to do? Please help meWe will have ' 30 deleted rows' or ' 30 inserted rows"in our tool... I need to capture that 'put out' in a variable and validate against the other
This should be easy.
SQL> set serverout on SQL> DECLARE 2 v_rows_inserted PLS_INTEGER; 3 v_rows_deleted PLS_INTEGER; 4 BEGIN 5 INSERT INTO emp_temp 6 SELECT * FROM emp WHERE deptno = 20; 7 v_rows_inserted := SQL%ROWCOUNT; 8 dbms_output.put_line('Rows Inserted: ' || v_rows_inserted); 9 DELETE FROM EMP WHERE deptno = 20; 10 v_rows_deleted := SQL%ROWCOUNT; 11 dbms_output.put_line('Rows Deleted: ' || v_rows_deleted); 12 IF v_rows_inserted = v_rows_deleted THEN 13 dbms_output.put_line('They are same!'); 14 ELSE 15 dbms_output.put_line('They are NOT same!'); 16 END IF; 17 END; 18 / Rows Inserted: 7 Rows Deleted: 7 They are same! PL/SQL procedure successfully completed.
-
Insert the object into the table
Hi all
I would insert my type of object in a table without specifying the name of the variable.
That's my bad example:
create or replace type MY_TYPE_OBJ as object ( val1 number, val2 number, val3 number ); / CREATE TABLE MY_TABLE of MY_TYPE_OBJ; / declare myType MY_TYPE_OBJ; begin myType := new MY_TYPE_OBJ(1, 2, 3); insert into MY_TABLE values (myType.val1, myType.val2, myType.val3); end; /
As you can see, on my last procedure, I had to use the insert into statement with val1, val2, val3 name filed.
Is there a quicker way to insert an object into a table? In the production environment of the object type that almost 100 filed is not so friendly to write each one by one.
Thank you
Federico
Example:
SQL> create or replace type TFoo as object( 2 attr1 integer, 3 attr2 varchar2(10), 4 attr3 date 5 ); 6 / Type created. SQL> SQL> create table footab of TFoo( 2 attr1 primary key, 3 attr2 not null 4 ) organization index 5 / Table created. SQL> SQL> declare 2 foo TFoo; 3 begin 4 foo := new TFoo( 1, 'test1', trunc(sysdate) ); 5 insert into footab values foo; 6 end; 7 / PL/SQL procedure successfully completed. SQL> SQL> select * from footab; ATTR1 ATTR2 ATTR3 ---------- ---------- ------------------- 1 test1 2014/08/27 00:00:00 SQL>
I agree though - be careful with tables of objects and now their technical implementation and advantages and disadvantages.
-
Simple procedure to insert data into a table
Hello
I am trying to create a simple procedure to insert the data into the emp table.
He throws after WARNING:
The procedure that is created with compilation errors.
CREATE or REPLACE procedure ins_emp (empno emp.empno%type,ename emp.ename%type,deptno emp.deptno%type)
IS
Emp.empno%type,P_ename emp.ename%type,P_deptno emp.deptno%type P_empno;
BEGIN
Insert into emp values (empno, ename, deptno);
Ins_emp END;
And when I try to run it
execute ins_emp(1111,'abcd',20);
He is to launch another error:
PLSQL - 00905:OBJECT SCOTT. INS_EMP is not valid.
can someone help me with this procedure?
Thank you
Hi Frank,.
I thank very you much for your comments.
I have a question:
unless the procedure or function created with compilation errors, we cannot run them right.
-
How to see the output of an associative array in procedure
Hello
I tried the following code
And confused about the research at the exit of the associative array.
In the above code, I want to see the output of the test_tab located in the search_obj procedure.CREATE OR REPLACE PACKAGE test_pak1 AS FUNCTION map_object (obj_typ_in VARCHAR2) RETURN VARCHAR2; CURSOR c_c1 IS SELECT * FROM emp; TYPE test_ttyp IS TABLE OF c_c1%ROWTYPE INDEX BY PLS_INTEGER; PROCEDURE search_obj (obj_type VARCHAR2); END; CREATE OR REPLACE PACKAGE BODY test_pak1 AS FUNCTION map_object (obj_typ_in VARCHAR2) RETURN VARCHAR2 IS BEGIN dopl ('Hello'); RETURN abc; END; PROCEDURE search_obj (obj_type VARCHAR2) IS test_tab test_ttyp; BEGIN DOPL (test_tab); end; END;
could you please help me in this
Thank youHi, smile,
If you want to see records, you must put the lines in your table. Here's a solution: in this solution, I only edit the search_obj procedure of your package like this:
PROCEDURE search_obj (obj_type VARCHAR2) IS test_tab test_ttyp; test_tab_r c_c1%ROWTYPE; BEGIN --test_tab; --Here we put something in test_tab OPEN c_c1; LOOP Fetch c_c1 into test_tab_r; exit when c_c1%NOTFOUND; test_tab(test_tab.COUNT + 1) := test_tab_r; END LOOP; CLOSE c_c1; FOR i IN 1 .. test_tab.COUNT LOOP dbms_output.put_line(test_tab(i).empno); END LOOP; END;
Here is the result:
SQL> exec test_pak1.search_obj('parameter_not_used_in_the_procedure'); 7369 7499 7521 7566 7654 7698 7782 7788 7839 7844 7876 7900 7902 7934 PL/SQL procedure successfully completed. SQL>
-
I wrote an insert procedure. It shows some errors.
This is the query.
create or replace PROCEDURE SP_MEMBERS_IN)
P_MEM_ID IN MEMBERS. MEM_ID,
P_MEM_NAME IN MEMBERS. MEM_NAME,
P_B_DAY IN MEMBERS. B_DAY,
p_address in MEMBERS. ADDRESS
*)*
AS
BEGIN
INSERT INTO MEMBERS (MEM_ID, MEM_NAME, B_DAY, ADDRESS)
VALUES (P_MEM_ID, P_MEM_NAME, P_B_DAY, P_ADDRESS);
COMMIT;
END SP_MEMBERS_IN;
Here are the errors.
Error (1): PL/SQL: analysis of completed Compilation unit
Error (2.1): PLS-00488: invalid variable declaration: object ' MEMBERS. MEM_ID' must be a type or subtype
Can someone help me fix it please?create or replace PROCEDURE SP_MEMBERS_IN)
P_MEM_ID IN MEMBERS. MEM_ID % TYPE,
P_MEM_NAME IN MEMBERS. MEM_NAME % TYPE,
P_B_DAY IN MEMBERS. B_DAY % TYPE,
p_address in MEMBERS. ADDRESS TYPE %)
AS
BEGIN
INSERT INTO MEMBERS (MEM_ID, MEM_NAME, B_DAY, ADDRESS)
VALUES (P_MEM_ID, P_MEM_NAME, P_B_DAY, P_ADDRESS);
COMMIT;END SP_MEMBERS_IN;
you forget to put % TYPE in the settings variable declaration
-
Hello again,
during a migration script written in plsql, we print several status information on-screen using dbms_output.put_line.
for your information:
the script commits each lines x during a massive update. A commit is carried out whenever I want to have the output: Timestamp + number of lines committed themselves.
Unfortunately the output by dbms_output.put_line is emptied to sqldeveloper once the procedure is complete.
So I have all the time after the migration is complete, but if possible I need the information when it is put into the stack of the dbms_output.
Is it possible to flush the output while the process is still ongoing? Are the alternatives to dbms_output.put_line who could help?
Oracle is 10.2.0.4
Thank you very much
Andreas
Published by: Andreas s. the 11.03.2011 01:38Note:
Messages sent using the DBMS_OUTPUT are not actually sent until full subprogramme of the shipment or the relaxation. > There is no mechanism to flush the output during execution of a procedure.http://download.Oracle.com/docs/CD/B19306_01/AppDev.102/b14258/d_output.htm
you could just write your output to a table?
Maybe you are looking for
-
Apple remote activated iTunes on my computer while I'm sailing in Apple TV
Apple remote activated iTunes on my computer while I'm sailing in Apple TV So when I try to navigate the Apple TV using my apple remote, my computer meets her while she sleeps, and the screen is closed. Is it possible that I can get the computer stop
-
How to use DAY. E TOUCH as an e-reader?
Greetings from the Greece! I just bought the new Journ.E Touch Tablet.https://www.toshibatouch.EU/index2.php Although I am happy with different equipment, I have not yet found the way to read my e-books (pdf files), or any other docs 'office'... that
-
driver search for Hp envy 17 j108tx running win7 64-bit
Hello I just changed the OS on my new Hp Envy win8.1 for win7 by following the instructions provided on other threads but cannot locate the last three drivers. The hardware id is the following BCM20702A0 USB\VID_0A5C & PID_21FB & REV_0112USB\VID_0A5C
-
Cannot install Windows Update error number 80070641 update
original title: little Question on the error 80070641 update number Hi, I was wondering if someone help cam - I had a windows update to install the-(KB2596785) next, (KB2596843), (KB2596912), (KB2596789) all for microsoft office (2007) and powerpoint
-
Backup error 0 x 80070003-Windows
Win7 Home Premium 64 ceased to make backups and the only message I get is "Windows backup - Windows cannot disable the task of automatic backup for the following reason: The system does not have the specified path. (0 x 80070003) Try again. Would be