Insert values with PL/SQL Tables and Record - sql

I have done a procedure in a package to enter data into a table using a PL / SQL table.
I have put some data in the code to execute from the package and verify that the procedure works, but I don't know how to execute it from the worksheet with the data that i enter.
CREATE TABLE TB_CRUD_MAC
( "K_CODIGO" NUMBER(10,0),
"A_NUMNIT" VARCHAR2(11 BYTE),
"N_NOMBRE" VARCHAR2(11 BYTE),
"N_APELLI" VARCHAR2(11 BYTE),
"F_FECHA" DATE,
"I_ESTADO" VARCHAR2(1 BYTE),
"K_CLASIF" VARCHAR2(1 BYTE)
)
create or replace PACKAGE PK_CRUD_MAC AS
TYPE R_REGISTRO IS RECORD (
codigo TB_CRUD_MAC.K_CODIGO%TYPE := K_CODIGO.nextval,
numnit TB_CRUD_MAC.A_NUMNIT%TYPE,
nombre TB_CRUD_MAC.N_NOMBRE%TYPE,
apelli TB_CRUD_MAC.N_APELLI%TYPE,
fecha TB_CRUD_MAC.F_FECHA%TYPE,
estado TB_CRUD_MAC.I_ESTADO%TYPE,
clasif TB_CRUD_MAC.K_CLASIF%TYPE
);
TYPE T_REGISTRO IS TABLE OF R_REGISTRO;
PROCEDURE PR_INSERT_LISTA (PT_REGISTRO IN OUT T_REGISTRO);
END;
create or replace PACKAGE BODY PK_CRUD_MAC AS
PROCEDURE PR_INSERT_LISTA (PT_REGISTRO IN OUT T_REGISTRO)
IS
BEGIN
PT_REGISTRO := T_REGISTRO();
PT_REGISTRO.extend(3);
PT_REGISTRO(1).nombre := 'Andres';
PT_REGISTRO(2).nombre := 'Martinez';
INSERT INTO TB_CRUD_MAC VALUES (K_CODIGO.nextval, '123', PT_REGISTRO(1).nombre, 'AAA', '28/03/00', '1', '1');
INSERT INTO TB_CRUD_MAC VALUES (K_CODIGO.nextval, '123', PT_REGISTRO(2).nombre, 'AAA', '28/03/00', '1', '1');
END;
END;
Edit:
I try this but dont work:
DECLARE
reg PK_CRUD_MAC.T_REGISTRO := PK_CRUD_MAC.T_REGISTRO();
BEGIN
reg().extend;
reg(1).nombre := 'AAA';
PK_CRUD_MAC.PR_INSERT_LISTA(reg);
END;
Error: PLS-00355: use of pl/sql table not allowed in this context

a cursor like you had in mind, does not work. try this:
DECLARE
reg PK_CRUD_MAC.T_REGISTRO := PK_CRUD_MAC.T_REGISTRO();
BEGIN
reg.extend;
reg(1).numnit := 'Aaa';
reg(1).nombre := 'aaa';
PK_CRUD_MAC.PR_INSERT_LISTA(reg);
END;
/

Well running your procedure is simply a matter of declaring a variable and calling the procedure, say in an anonymous PL/SQL block:
declare
l_table pk_crud_mac.t_registro;
begin
pk_crud_mac.pr_insert_lista(l_table);
end;
/
However, this doesn't get us very far. Probably you don't want to have those values hard-coded in the procedure. Instead you should populate the values in the calling program and pass them to the procedure.
Oh, and defaulting R_REGISTRO.codigo to a sequence value may seem like a neat idea but it can cause problems with initialisation. It's probably better to handle K_CODIGO.nextval in the internals of the code.
So, re-writing your package like this ...
create or replace package pk_crud_mac as
type r_registro is record (
codigo tb_crud_mac.k_codigo%type,
numnit tb_crud_mac.a_numnit%type,
nombre tb_crud_mac.n_nombre%type,
apelli tb_crud_mac.n_apelli%type,
fecha tb_crud_mac.f_fecha%type,
estado tb_crud_mac.i_estado%type,
clasif tb_crud_mac.k_clasif%type
);
type t_registro is table of r_registro;
procedure pr_insert_lista (pt_registro in out t_registro);
end;
/
create or replace package body pk_crud_mac as
procedure pr_insert_lista (pt_registro in out t_registro)
is
begin
for idx in pt_registro.first()..pt_registro.last loop
pt_registro(idx).codigo := k_codigo.nextval;
end loop;
forall idx in pt_registro.first()..pt_registro.last
insert into tb_crud_mac values pt_registro(idx);
end;
end;
/
...means we call it like this:
declare
l_record pk_crud_mac.r_registro;
l_table pk_crud_mac.t_registro := new pk_crud_mac.t_registro();
begin
l_record.nombre := 'Andres';
l_record.numnit := '123';
l_record.apelli := 'AAA';
l_record.fecha := date '2000-03-28';
l_record.estado := '1';
l_record.clasif := '1';
l_table.extend();
l_table(l_table.count()) := l_record;
l_record.nombre := 'Martinez';
l_table.extend();
l_table(l_table.count()) := l_record;
pk_crud_mac.pr_insert_lista(l_table);
for idx in 1..l_table.count() loop
dbms_output.put_line(l_table(idx).codigo ||':' || l_table(idx).nombre);
end loop;
end;
/
If you enable SERVEROUTPUT the call to dbms_output.put_line will show you that the sequence value has been assigned to each record.

Related

How to execute stored function in Oracle PL/SQL

I want to execute this stored function, and insert data in table t
I tried to find solution, but unsuccessfully
CREATE TABLE t (id number
, name varchar2(32)
, time date);
CREATE OR REPLACE PACKAGE t_api AS
FUNCTION func_ins (
p_row IN t%rowtype
) RETURN t.id%TYPE;
END t_api;
/
CREATE OR REPLACE PACKAGE BODY t_api AS
FUNCTION func_ins (
p_row IN t%rowtype
) RETURN t.id%TYPE
IS
l_id t.id%TYPE;
BEGIN
INSERT INTO t VALUES p_row RETURNING id INTO l_id;
RETURN l_id;
END func_ins;
END t_api;
/
declare
p_row t%rowtype;
begin
p_row.id := 1;
p_row.name := 'name';
p_row.time := sysdate;
t_api.func_ins(p_row);
end;
/
I got
PLS-00221
Thanks in advance
It works perfectly ,however i wouldn't recommend this design as its not a good practice to perform a DML within a function. Rather create a procedure instead of function and retrieve the Id using the out parameter.
Anonymous block to test the function when table is empty.You assign values for %ROWTYPE variable and insert.
declare
t_row t%rowtype;
x t.id%type;
begin
t_row.id := 2;
t_row.name := 'Test2';
t_row.time := sysdate;
x := t_api.func_ins(t_row);
dbms_output.put_line('x '||x);
end;
Output is
x 2
Modified code with Procedure to achieve the same result is below,
CREATE OR REPLACE PACKAGE t_api AS
FUNCTION func_ins (
p_row IN t%rowtype
) RETURN t.id%TYPE;
PROCEDURE proc_ins (
p_row IN t%rowtype,
l_id out t.id%TYPE
);
END t_api;
/
CREATE OR REPLACE PACKAGE BODY t_api AS
FUNCTION func_ins (
p_row IN t%rowtype
) RETURN t.id%TYPE
IS
l_id t.id%TYPE;
BEGIN
INSERT INTO t VALUES p_row RETURNING id INTO l_id;
RETURN l_id;
END func_ins;
PROCEDURE proc_ins (
p_row IN t%rowtype,
l_id out t.id%TYPE
)
IS
BEGIN
INSERT INTO t VALUES p_row RETURNING id INTO l_id;
END proc_ins;
END t_api;
/
Anonymous block to test the procedure,
declare
t_row t%rowtype;
x t.id%type;
begin
t_row.id := 3;
t_row.name := 'Test3';
t_row.time := sysdate;
t_api.proc_ins(t_row,x);
dbms_output.put_line('x '||x);
end;
Output is
x 3

How to export result of SQL query into csv-style formatted string, that will be later saved into clob in Oracle?

I have the following problem:
I have a table Source_Data(actually, quite many different source tables), and I need to export some data from them into Result_Table. For each source table I have some sql that returns the data, and it needs to be transformed into csv-like string.
For example, Source_Data1 table has PK - ID, some foreign keys - A_ID, B_ID, CREATION_DATE date, and some other columns.
I need to export the result of SQL query that selects data from Source_Data1 table, it usually returns the primary key, some foreign keys which are not null and a date which is also not null, and transform it into cvs-like string, which should be saved later in some other table as a clob.
CREATE TABLE Source_Data1 (
ID NUMBER(3) NOT NULL,
A_ID NUMBER(10) NOT NULL,
B_ID NUMBER(10) NOT NULL,
CREATION_DATE DATE NOT NULL,
some other columns );
and I have SQL query:
select ID, A_ID, B_ID, CREATION_DATE
from Source_Data1
where <expression>
Based on this query, I need to generate string like this:
'ID,A_ID,B_ID,CREATION_DATE
1,200,10,2018-03-01 00:00:00.0
7,202,11,2018-03-02 00:00:00.0
8,205,11,2018-03-02 00:00:00.0'
and I need to save it into the Result table as a clob:
Insert into Result_Table (ID, SOURCE_NAME, DATA) values
(result_seq.nextval, 'Source_Data1', result of sqlquery 1);
And this needs to be done for many source tables.
I was wondering if there is some general way to do it? Because for each source table I have its own sql query, different set of selected columns, and generating this csv string manually looks not very nice.
Thank you for your advice.
Step 1 should be to download SQLcl, you can easily save the output of SQL select statements in CSV format via "SET SQLFORMAT CSV" and SPOOL, it does the work for you. So you can use that to loop through a bunch of table names / select statements, spool and save the output of each to a directory via a shell script.
Next, here's a package that has a bunch of file utilities that all work with oracle DIRECTORY objects and the files within them. With this and some PL/SQL, you could easily pull those files that you've saved off into a table. There may be easier ways to do what you're trying to do, and if there is, I look forward to hearing about them for others. It's kind of a big problem to solve.
1) file_into_blob - To get the file into the database
2) convert_blob_to_clob - To convert to clob
3) You can then insert that into your table.
Package spec
Create or replace package fileutils as
--
-- This procedure deletes a file, and depends on an Oracle DIRECTORY object being passed
--
Procedure delete_os_file (i_directory varchar2, i_filename varchar2);
--
-- This procedure moves and optionally renames a file,
-- and depends on an Oracle DIRECTORY object being passed
--
Procedure move_os_file ( i_source_directory in varchar2, i_source_file in varchar2, i_target_directory in varchar2, i_target_file in varchar2);
--
-- This procedure takes a blob variable and writes it to a file,
-- and depends on an Oracle DIRECTORY object being passed
--
Procedure blob_into_file (i_directory in varchar2, i_file_name in varchar2, i_blob in blob);
--
-- This procedure takes a file and uploads it into a blob variable
-- and depends on an Oracle DIRECTORY object being passed
--
Procedure file_into_blob(i_directory in varchar2, i_file_name in varchar2, o_blob out blob);
--
-- This procedure converts a clob to a blob
--
Procedure convert_clob_to_blob (i_clob in clob, o_blob out blob);
--
-- This procedure converts a blob to a clob
--
Procedure convert_blob_to_clob (i_blob in blob, o_clob out clob);
--
-- This one checks for file existence without Java
--
Function file_exists (i_directory in varchar2, i_filename in varchar2) return boolean;
--
-- Returns the basename of a filename
-- Works with Windows and UNIX pathnames
--
Function basename (i_filename in varchar2) return varchar2;
--
-- This takes a Base64 string and converts it to a binary BLOB
--
Procedure base64_string_to_blob (i_clob in clob, o_blob out blob);
Function base64_string_to_blob (i_clob in clob) return blob;
--
-- This takes a binary BLOB and converts it to a Base64 string
--
Procedure blob_to_base64_string (i_blob in blob, o_clob out clob);
Function blob_to_base64_string (i_blob in blob) return clob;
End fileutils;
/
Show error;
Package body
Set define off;
Create or replace package body fileutils as
Procedure delete_os_file (i_directory varchar2, i_filename varchar2)
is
Begin
utl_file.fremove(i_directory,i_filename);
End;
Procedure move_os_file
(
i_source_directory in varchar2,
i_source_file in varchar2,
i_target_directory in varchar2,
i_target_file in varchar2
)
is
srcdir varchar2(255) := upper(i_source_directory);
tgtdir varchar2(255) := upper(i_target_directory);
begin
--
-- NOTE: If you're getting the all-too-familiar
-- ORA-29292: file rename operation failed
-- and you're SURE that your directory names are correct,
-- and you're SURE that your privileges are correct, both at the
-- OS level, and within the database, there's one last thing that
-- can get you. I learned the hard way that this command will NOT
-- work successfully renaming a file from one filesystem to another,
-- at least when those filesystems are NFS mounted. That is all.
--
utl_file.frename(srcdir,i_source_file,tgtdir,i_target_file,TRUE);
end move_os_file;
Procedure blob_into_file (i_directory in varchar2, i_file_name in varchar2, i_blob in blob)
is
l_file utl_file.file_type;
l_buffer raw(32767);
l_amount binary_integer := 32767;
l_pos integer := 1;
i_blob_len integer;
Begin
i_blob_len := dbms_lob.getlength(i_blob);
l_pos:= 1;
-- Open the destination file.
l_file := utl_file.fopen(i_directory,i_file_name,'wb', 32767);
-- Read chunks of the BLOB and write them to the file
-- until complete.
while l_pos < i_blob_len loop
dbms_lob.read(i_blob, l_amount, l_pos, l_buffer);
utl_file.put_raw(l_file, l_buffer, TRUE);
l_pos := l_pos + l_amount;
end loop;
-- Close the file.
utl_file.fclose(l_file);
End blob_into_file;
Procedure file_into_blob(i_directory in varchar2, i_file_name in varchar2, o_blob out blob)
is
src_loc bfile := bfilename(i_directory, i_file_name);
Begin
-- Initialize the dest blob
o_blob := empty_blob();
-- Open source binary file from OS
dbms_lob.open(src_loc, dbms_lob.lob_readonly);
-- Create temporary LOB object
dbms_lob.createtemporary(
lob_loc => o_blob
, cache => true
, dur => dbms_lob.session
);
-- Open temporary lob
dbms_lob.open(o_blob, dbms_lob.lob_readwrite);
-- Load binary file into temporary LOB
dbms_lob.loadfromfile(
dest_lob => o_blob
, src_lob => src_loc
, amount => dbms_lob.getLength(src_loc));
-- Close lob objects
dbms_lob.close(o_blob);
dbms_lob.close(src_loc);
End file_into_blob;
Function basename (i_filename in varchar2) return varchar2
is
v_basename varchar2(1024);
Begin
--
-- If the regex's below don't match, then it's already at its base name
-- Return what was passed.
--
v_basename := i_filename;
if regexp_like(i_filename,'^.*\\') then
dbms_output.put_line('This is a Windows file');
v_basename := regexp_substr(i_filename,'[^\]*$');
dbms_output.put_line('Basename is : '||v_basename);
end if;
if regexp_like(i_filename,'^/') then
dbms_output.put_line('This is a UNIX file');
v_basename := regexp_substr(i_filename,'[^/]*$');
dbms_output.put_line('Basename is : '||v_basename);
end if;
return v_basename;
End basename;
Function file_exists (i_directory in varchar2, i_filename in varchar2) return boolean
is
v_exists boolean;
v_file_length number;
v_block_size number;
Begin
utl_file.fgetattr(upper(i_directory), i_filename, v_exists, v_file_length, v_block_size);
if (v_exists) then
dbms_output.put_line('File '||i_filename||' exists, '||v_file_length||' bytes');
else
dbms_output.put_line('File '||i_filename||' does not exist');
end if;
return v_exists;
end file_exists;
Procedure convert_clob_to_blob (i_clob in clob, o_blob out blob)
is
v_in pls_Integer := 1;
v_out pls_Integer := 1;
v_lang pls_Integer := 0;
v_warning pls_Integer := 0;
Begin
dbms_lob.createtemporary(o_blob,TRUE);
dbms_lob.converttoblob(o_blob,i_clob,DBMS_lob.getlength(i_clob),v_in,v_out,dbms_lob.default_csid,v_lang,v_warning);
End convert_clob_to_blob;
Procedure convert_blob_to_clob (i_blob in blob, o_clob out clob)
is
v_in pls_Integer := 1;
v_out pls_Integer := 1;
v_lang pls_Integer := 0;
v_warning pls_Integer := 0;
Begin
dbms_lob.createtemporary(o_clob,TRUE);
dbms_lob.converttoclob(o_clob,i_blob,DBMS_lob.getlength(i_blob),v_in,v_out,dbms_lob.default_csid,v_lang,v_warning);
End convert_blob_to_clob;
Procedure blob_to_base64_string (i_blob in blob, o_clob out clob)
is
v_out_cl clob;
file_len pls_integer;
modulo pls_integer;
pieces pls_integer;
amt binary_integer := 23808;
buf raw (32767);
buf_tx varchar2(32767);
pos pls_integer := 1;
filepos pls_integer := 1;
counter pls_integer := 1;
Begin
dbms_lob.createtemporary (v_out_cl, true, dbms_lob.call);
file_len := dbms_lob.getlength (i_blob);
modulo := mod (file_len, amt);
pieces := trunc (file_len / amt);
while (counter <= pieces) loop
dbms_lob.read (i_blob, amt, filepos, buf);
buf_tx:=utl_raw.cast_to_varchar2 (utl_encode.base64_encode (buf));
dbms_lob.writeappend (v_out_cl,length(buf_tx),buf_tx);
filepos := counter * amt + 1;
counter := counter + 1;
end loop;
if (modulo <> 0) THEN
dbms_lob.read (i_blob, modulo, filepos, buf);
buf_tx:=utl_raw.cast_to_varchar2 (utl_encode.base64_encode (buf));
dbms_lob.writeappend (v_out_cl,length(buf_tx),buf_tx);
end if;
o_clob := v_out_cl;
End blob_to_base64_string;
Function blob_to_base64_string (i_blob in blob) return clob
is
v_out_cl clob;
file_len pls_integer;
modulo pls_integer;
pieces pls_integer;
amt binary_integer := 23808;
buf raw (32767);
buf_tx varchar2(32767);
pos pls_integer := 1;
filepos pls_integer := 1;
counter pls_integer := 1;
Begin
dbms_lob.createtemporary (v_out_cl, true, dbms_lob.call);
file_len := dbms_lob.getlength (i_blob);
modulo := mod (file_len, amt);
pieces := trunc (file_len / amt);
while (counter <= pieces) loop
dbms_lob.read (i_blob, amt, filepos, buf);
buf_tx:=utl_raw.cast_to_varchar2 (utl_encode.base64_encode (buf));
dbms_lob.writeappend (v_out_cl,length(buf_tx),buf_tx);
filepos := counter * amt + 1;
counter := counter + 1;
end loop;
if (modulo <> 0) THEN
dbms_lob.read (i_blob, modulo, filepos, buf);
buf_tx:=utl_raw.cast_to_varchar2 (utl_encode.base64_encode (buf));
dbms_lob.writeappend (v_out_cl,length(buf_tx),buf_tx);
end if;
return v_out_cl;
End blob_to_base64_string;
Procedure base64_string_to_blob (i_clob in clob, o_blob out blob)
is
v_out_bl blob;
clob_size number;
pos number;
charBuff varchar2(32767);
dBuffer RAW(32767);
v_readSize_nr number;
v_line_nr number;
begin
dbms_lob.createTemporary (v_out_bl, true, dbms_lob.call);
v_line_nr:=greatest(65, instr(i_clob,chr(10)), instr(i_clob,chr(13)));
v_readSize_nr:= floor(32767/v_line_nr)*v_line_nr;
clob_size := dbms_lob.getLength(i_clob);
pos := 1;
while (pos < clob_size) loop
dbms_lob.read (i_clob, v_readSize_nr, pos, charBuff);
dBuffer := UTL_ENCODE.base64_decode (utl_raw.cast_to_raw(charBuff));
dbms_lob.writeAppend (v_out_bl,utl_raw.length(dBuffer),dBuffer);
pos := pos + v_readSize_nr;
end loop;
o_blob := v_out_bl;
end base64_string_to_blob;
Function base64_string_to_blob (i_clob in clob) return blob
is
v_out_bl blob;
clob_size number;
pos number;
charBuff varchar2(32767);
dBuffer RAW(32767);
v_readSize_nr number;
v_line_nr number;
begin
dbms_lob.createTemporary (v_out_bl, true, dbms_lob.call);
v_line_nr:=greatest(65, instr(i_clob,chr(10)), instr(i_clob,chr(13)));
v_readSize_nr:= floor(32767/v_line_nr)*v_line_nr;
clob_size := dbms_lob.getLength(i_clob);
pos := 1;
while (pos < clob_size) loop
dbms_lob.read (i_clob, v_readSize_nr, pos, charBuff);
dBuffer := UTL_ENCODE.base64_decode (utl_raw.cast_to_raw(charBuff));
dbms_lob.writeAppend (v_out_bl,utl_raw.length(dBuffer),dBuffer);
pos := pos + v_readSize_nr;
end loop;
return v_out_bl;
end base64_string_to_blob;
end fileutils;
/
Show error;
I think you can use LISTAGG function
select listagg(ONE_LINE) WITHIN GROUP (ORDER BY ROW_NUM)
from (
select 'ID,A_ID,B_ID,CREATION_DATE' || CHR(10) as ONE_LINE,
-1 as ROW_NUM
from dual
union all
select ID ||','|| A_ID ||','|| B_ID ||','|| CREATION_DATE || CHR(10) as ONE_LINE,
ROWNUM as ROW_NUM
from Source_Data1
where <expression>
);
The following code will create a CSV version of a table as a CLOB row, using DATA_DUMP.SQL and the function DBMS_XSLPROCESSOR.READ2CLOB.
First, install the DATA_PUMP procedure on your schema. The program is a single command in a single file, you can run it through SQL*Plus or just copy and paste it into your IDE.
Next, create a directory on the server to temporarily store the files. Most existing utilities are meant to create files. It's easier to write a file and read it as a CLOB, than to modify the utility to write directly into a table.
create or replace directory temp_dir as 'C:\temp';
Then this PL/SQL block will read and write the data:
--Store a table as a single CSV clob.
--TODO: Delete the files from the server when done.
declare
v_clob clob;
begin
for tables in
(
--Query that returns the table names.
--(Doesn't have to be hard-coded, maybe a query on DBA_TABLES would help?)
select column_value table_name
from table(sys.odcivarchar2list('source_data'))
) loop
data_dump
(
query_in => 'select * from source_data1',
file_in => 'source_data1.csv',
directory_in => 'temp_dir',
nls_date_fmt_in => 'YYYY-MM-DD HH24:MI:SS',
delimiter_in => ',',
header_row_in => true
);
v_clob := dbms_xslprocessor.read2clob
(
flocation => 'TEMP_DIR',
fname => 'source_data1.csv'
);
insert into result_table (id, source_name, data) values
(result_seq.nextval, 'source_data1', v_clob);
--I wouldn't normally commit after each row, but when exporting large
--tables the script may run too long and there's a good chance of an
--error, so partial results may be helpful.
commit;
end loop;
end;
/
Reading and writing to CSV files is not rocket science but it's not as trivial as most people think. 99% of the CSV programs out there can't handle things like commas in the data, or adding a header row. So it's best to look for preexisting code instead of writing your own utility.

Invalid Data Type error while casting PL/SQL Array to Table

I have a PL/SQL stored proc which needs to return a RefCursor type object as output parameter.
PROCEDURE usp_appnt_stts_driver_wraper2
( in_req_src_system_id IN NUMBER,
in_req_user_info IN VARCHAR2,
out_response_rec1 OUT SYS_REFCURSOR)
At the end of the SP, I am able to return Hard Coded values to my front end by using a Select Statement.
OPEN out_response_rec1 FOR
SELECT 'data1', 'data2', 'data3', 'data 4' FROM DUAL;
This works fine. But I need to send the data which I am getting from an Array.
The Array is populated like this,
FOR g_index IN g_slsnetoutbndarr.FIRST..g_slsnetoutbndarr.LAST
LOOP
out_response_rec.EXTEND;
IF g_SlsnetOutbndArr(g_index).Rectypdesc IS NOT NULL THEN
out_response_rec(g_index).Rectypdesc := g_SlsnetOutbndArr(g_index).Rectypdesc ;
out_response_rec(g_index).Recdetltcode := g_SlsnetOutbndArr(g_index).Recdetltcode;
out_response_rec(g_index).RecDetlDesc := g_SlsnetOutbndArr(g_index).RecDetlDesc ;
END IF;
END LOOP;
So at the end of this code, the Array Object out_response_rec has all the values I need.
But How do I transfer these values in the RefCursor output parameter?
Update 1
I have tried to create a new data type in the Package specification.
TYPE SlsnetOutbndRec IS RECORD(
Rectypdesc VARCHAR2(30),
Recdetltcode NUMBER,
RecDetlDesc VARCHAR2(130));
TYPE SlsnetOutbndTabArr IS TABLE OF SlsnetOutbndRec;
Finally I have tried to Cast the Array to table in my SP as
OPEN out_response_rec_result FOR
SELECT * FROM TABLE (Cast(out_response_rec AS SlsnetOutbndTabArr));
But this is throwing an Invalid Data Type error. The SP does not recognize the new data types I created.
So at the end of this code, the Array Object out_response_rec has all
the values I need.
But How do I transfer these values in the RefCursor output parameter?
As I could understand, you wanted to use a SYS_REFCUSOR to get the output of a Query plus the values that are in collection which is being populated by another Procedure. I have come up with a solution for your requirement see below inline explaination:
--Create a Object in Place of Record since we cannot use a PLSQL scope compnenet in SQL scope in Oracle 11g and below
CREATE OR REPLACE TYPE SlsnetOutbndRec IS OBJECT
(
Rectypdesc VARCHAR2 (30),
Recdetltcode NUMBER,
RecDetlDesc VARCHAR2 (130)
);
--Create a table type of the Object
CREATE OR REPLACE TYPE SlsnetOutbndTabArr IS TABLE OF SlsnetOutbndRec;
/
--Procedure
CREATE OR REPLACE PROCEDURE combined_rslt (var OUT SYS_REFCURSOR)
AS
v_var SlsnetOutbndTabArr := SlsnetOutbndTabArr ();
l_var SlsnetOutbndTabArr := SlsnetOutbndTabArr ();
BEGIN
--Populating the collection
v_var.EXTEND;
v_var (1) := SlsnetOutbndRec ('ABC', 1, 'A');
OPEN VAR FOR
SELECT 'CDE', 2, 'B' FROM DUAL
UNION ALL -- Combining the result of collection with the result of query
SELECT *
FROM TABLE (v_var) t;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (SQLERRM);
END;
Execution :
DECLARE
x SYS_REFCURSOR;
a VARCHAR2 (30);
b NUMBER;
c VARCHAR2 (130);
BEGIN
combined_rslt (var => x);
LOOP
FETCH x INTO a, b, c;
EXIT WHEN x%NOTFOUND;
DBMS_OUTPUT.put_line (a || ' ' || b || ' ' || c);
END LOOP;
END;
Results:
SQL> /
CDE 2 B
ABC 1 A
PL/SQL procedure successfully completed.
You need to create the types in the SQL scope (not the PL/SQL scope) if you want to use them in SQL statements (in versions prior to Oracle 12):
CREATE TYPE SlsnetOutbndRec IS OBJECT(
Rectypdesc VARCHAR2(30),
Recdetltcode NUMBER,
RecDetlDesc VARCHAR2(130)
)
/
CREATE TYPE SlsnetOutbndTabArr IS TABLE OF SlsnetOutbndRec
/
Then you can use it in your procedure (assuming your 3rd party data type is a collection or a VARRAY):
PROCEDURE usp_appnt_stts_driver_wraper2(
in_req_src_system_id IN NUMBER,
in_req_user_info IN VARCHAR2,
out_response_rec_result OUT SYS_REFCURSOR
)
IS
out_response_rec SlsnetOutbndTabArr := SlsnetOutbndTabArr();
g_slsnetoutbndarr ThirdPartyDataType := Get_From_3rd_party_Package();
BEGIN
FOR i IN 1 .. g_slsnetoutbndarr.COUNT LOOP
IF g_SlsnetOutbndArr(i).Rectypdesc IS NOT NULL THEN
out_response_rec.EXTEND;
out_response_rec := SlsnetOutbndRec(
g_SlsnetOutbndArr(i).Rectypdesc,
g_SlsnetOutbndArr(i).Recdetltcode,
g_SlsnetOutbndArr(i).RecDetlDesc
);
END IF;
END LOOP;
OPEN out_response_rec_result FOR
SELECT *
FROM TABLE( out_response_rec );
END;
If the 3rd party data type is an associative array then:
...
IS
out_response_rec SlsnetOutbndTabArr := SlsnetOutbndTabArr();
g_slsnetoutbndarr ThirdPartyDataType := Get_From_3rd_party_Package();
i ThirdPartyIndexType := g_slsnetoutbndarr.FIRST;
BEGIN
WHILE i IS NOT NULL LOOP
IF g_SlsnetOutbndArr(i).Rectypdesc IS NOT NULL THEN
out_response_rec.EXTEND;
out_response_rec := SlsnetOutbndRec(
g_SlsnetOutbndArr(i).Rectypdesc,
g_SlsnetOutbndArr(i).Recdetltcode,
g_SlsnetOutbndArr(i).RecDetlDesc
);
END IF;
i := g_slsnetoutbndarr.NEXT(i);
END LOOP;
...

incorrect integer value '' for column cuit at row 1

I have a webservice, with the next code in the implementation:
procedure Tferreteria.inserpersona(persona:Tpersona);
begin
//insertar persona
dm:=Tconsultas.Create(nil);
with dm.SQLQuery1 do
begin
Close;
SQL.Add('insert into persona (nombreyape, domicilio)values (:nombreyape,:domicilio);');
ParamByName('nombreyape').AsString:=persona.nombreyape;
ParamByName('domicilio').AsString:=persona.domicilio;
ExecSQL();
end;
end;
procedure Tferreteria.insercliente(cliente:Tcliente);
begin
//insertar cliente
with dm.SQLQuery1 do
begin
SQL.Clear;
SQL.Add ('insert into clientes(idcliente,idpersona,cuit,cuil) values (null,(select last_insert_id()),:cuit,:cuil);');
ParamByName('cuit').AsInteger:=cliente.cuit;
ParamByName('cuil').AsInteger:=cliente.cuil;
ExecSQL();
end;
end;
As you see are 2 procedures which insert into tables personas and clientes. Clientes have a column idpersona which references personas
In the button click event I have this code
with Persona do
begin
nombreyape:=Form2.Edit1.Text;
domicilio:=Form2.Edit8.Text;
idlocalidad:=StrToInt(Form2.Edit3.Text);
end;
with Cliente do
begin
cuit:=StrToInt(Edit6.Text);
cuil:=StrToInt(Edit7.Text);
end;
//llamar al servicio
GetIferreteria.inserpersona(Persona);
GetIferreteria.insercliente(Cliente);
and I obtain this error
incorrect integer value 'aaa' for column cuit at row 1,
when aaa is written in edit1, which is not assigned to clientes.cuit,
Now I have another problem:
I have to insert idpersona in clientes, but I have to insert idpersona in telper, telper is a table which makes a relation among telephones and people who have these telephones.
If I do the same with other query, the last insert id would be an id from clientes (idcliente), not idpersona.
What can I do to make the idpersona stand for each moment I need to use it, in each table I mean?
I strongly recommend you re-write this code. Especially since you are using last_insert_id() (which you do not need to select to retreive), so you should execute the two statements together, preferably in a transaction (or better, in a stored procedure).
Try something more like this:
procedure Tferreteria.inserPersonaUnCliente(persona: Tpersona; cliente: Tcliente);
var
Query: TSQLQuery;
Conn: TSQLConnection;
SQL: TStrings;
Trans: TDBXTransaction;
begin
dm := Tconsultas.Create(nil);
Query := dm.SQLQuery1;
Conn := Query.SQLConnection;
SQL := Query.SQL;
//insertar persona un cliente
Query.Close;
Trans := Conn.BeginTransaction;
try
SQL.BeginUpdate;
try
SQL.Clear;
SQL.Add('insert into persona (nombreyape, domicilio) values (:nombreyape, :domicilio);';
SQL.Add('insert into clientes(idcliente, idpersona, cuit, cuil) values (null, last_insert_id(), :cuit, :cuil);');
finally
SQL.EndUpdate;
end;
Query.ParamByName('nombreyape').AsString := persona.nombreyape;
Query.ParamByName('domicilio').AsString := persona.domicilio;
Query.ParamByName('cuit').AsInteger := cliente.cuit;
Query.ParamByName('cuil').AsInteger := cliente.cuil;
Query.ExecSQL;
Conn.CommitFreeAndNil(Trans);
except
Conn.RollbackFreeAndNil(Trans);
raise;
end;
end;
with Persona do
begin
nombreyape := Form2.Edit1.Text;
domicilio := Form2.Edit8.Text;
idlocalidad := StrToInt(Form2.Edit3.Text);
end;
with Cliente do
begin
cuit := StrToInt(Edit6.Text);
cuil := StrToInt(Edit7.Text);
end;
//llamar al servicio
GetIferreteria.inserPersonaUnCliente(Persona, Cliente);

Error in stored procedure execution in oracle

create procedure proc_insert_salary(salary_emp in NUMBER,
empid in NUMBER(10),
desig in varchar2(20))
begin
DECLARE
gr_sal,hr,da,pf number;
BEGIN
set hr:= salary_emp*(15/100);
set da:= salary_emp*(8/100);
set pf := salary_emp*(35/100);
set gr_sal := salary_emp+hr+da-pf;
insert into emp_salary_details values (empid,desig, salary_emp, gr_sal);
end;
call proc_insert_salary (45000,10100,'C.E.O.')
when I call this procedure it gives error its in invalid state.
While #Guneli's answer is adequate, there's really no reason for the second block in this case. The following would be equivalent (and slightly simpler).
CREATE OR REPLACE PROCEDURE proc_insert_salary (salary_emp IN NUMBER,
empid IN NUMBER,
desig IN VARCHAR2) AS
hr NUMBER := salary_emp * (15 / 100);
da NUMBER := salary_emp * (8 / 100);
pf NUMBER := salary_emp * (35 / 100);
gr_sal NUMBER := salary_emp + hr + da - pf;
BEGIN
INSERT INTO emp_salary_details
VALUES (empid,
desig,
salary_emp,
gr_sal);
END;
/
Also, you should not that if you're going to have any other SQL in the same script (such as the call) then you need to end the procedure definition with a slash (/). This tells Oracle that procedure is finished and that it should compile it. Really, it's a good practice to always include it after procedural code.
Well, try this instead:
CREATE OR REPLACE PROCEDURE proc_insert_salary(
salary_emp IN NUMBER,
empid IN NUMBER,
desig IN VARCHAR2)
AS
BEGIN
DECLARE
gr_sal NUMBER;
hr NUMBER;
da NUMBER;
pf NUMBER;
BEGIN
hr := salary_emp*(15/100);
da := salary_emp*(8/100);
pf := salary_emp*(35/100);
gr_sal := salary_emp+hr+da-pf;
INSERT INTO emp_salary_details VALUES (empid,desig, salary_emp, gr_sal);
END;
END;
The things to note are that:
1) You can not show the size for the parameters of subprograms in PL/SQL, so NUMBER(10) or VARCHAR2(20) are incorrect.
2)The one line declaration of variables is not supported in PL/SQL, so instead of gr_sal,hr,da,pf number; you should use
gr_sal NUMBER;
hr NUMBER;
da NUMBER;
pf NUMBER;
3)To assign a value to variable you should not use SET, the ':=' is enough.
4)Also you have missed the 'end' clause for your second 'begin'.