How to convert pls_number to varchar2 in oracle? - oracle

I tried to convert pls_integer to varchar2 using to_char but it is not working.
I also tried pls_integer to number and number to varchar2 using to_number and to_char methods, but that also not working. I am getting an error :
Pls00306 - wrong number or types of arguments in call 'to_number'.
Can you please help.

There's no need to do anything special just assign. Oracle allows implicit data type conversion between pls_integer and varchar2. See table 3-10 at the bottom of this page
declare
l_number pls_integer;
l_varchar varchar2(1);
begin
l_number := 8;
l_varchar := l_number;
end;

Related

Using DBMS_CRYPTO.ENCRYPT safely - NLS error detected

I am trying to use DBMS_CRYPTO encrypt, but no matter how I choose some of the parameters, I encounter values for which the code throws
ORA-01890: NLS error detected ORA-06512: at "SYS.UTL_I18N", line 72 ORA-06512: at "SYS.UTL_I18N", line 353 ORA-06512: at line 26
Here is an example
declare
-- v_value VARCHAR2 (4000) := '9Ab2Ov1Bd4'; -- works
-- v_value VARCHAR2 (4000) := '7Md4Mt7Gk0'; -- works
-- v_value VARCHAR2 (4000) := '3Vf8Fi2Pa5'; -- works
-- v_value VARCHAR2(4000) := '5Vq2Dc4Cq9'; -- works as well
v_value VARCHAR2(4000) := '2Cq0Yh3Vb2'; --this does not work?
v_result VARCHAR2 (4000);
v_raw_row RAW (2000); -- stores ec binary text
v_enc_type PLS_INTEGER
:=
DBMS_CRYPTO.ENCRYPT_AES256 +
DBMS_CRYPTO.CHAIN_CBC +
DBMS_CRYPTO.PAD_ZERO; -- total encryption type
v_def_k VARCHAR2 (32) := 'mMbSmSMr_!_uAlns9asG5_a_AfhS4_3a';
begin
v_raw_row :=
DBMS_CRYPTO.ENCRYPT (
src => UTL_I18N.STRING_TO_RAW (v_value, 'AL32UTF8'),
typ => v_enc_type,
key => UTL_RAW.CAST_TO_RAW (v_def_k));
v_result := UTL_I18N.RAW_TO_CHAR (v_raw_row, 'AL32UTF8');
dbms_output.put_line(v_result);
end;
Changing the encryption type and key "solves" the issue for the one value which does not work, but it I always encounter another value which will then throw this exact exception.
I tried this in Oracle 12.2.0.1.0 as well as in 19.0.0.0.0. Exactly the same behaviour.
I guess that something I am doing is completely wrong. Any help is appreciated.
Instead of AL32UTF8 as source character set, can you please try using WE8ISO8859P1 or null.
From Oracle doc:
UTL_I18N.RAW_TO_CHAR is a function that converts raw to char data type conversion can be implemented, but not different character sets can be converted.
The second parameter should specify which characteret is passed by the raw data, ie the source character set.
Specify the character set that the RAW data was derived from. If src_charset is NULL, then the database character set is used.
so if using DB characterset or NULL, no error will be reported.

VARCHAR2(32767) not able to handle strings in stored procedure

I am concatenating string using cursor (to form query to execute later). Here, the query that will be formed is going to be way bigger that what VARCHAR2(32767) can handle. There fore, I am getting error on proc execution - ORA-06502: PL/SQL: numeric or value error: character string buffer too small.
I used CLOB data type as well bu got error ORA-06502: PL/SQL: numeric or value error.
My code is here below:
CREATE OR REPLACE PROCEDURE sp_Market
IS
Names VARCHAR2(32767);
BEGIN
DECLARE CURSOR cur IS ('Select ID, Order_of, field_name
FROM pld_medicare_config');
BEGIN
FOR i IN cur
LOOP
Names := Names || i.sqql;
END LOOP;
dbms_output.put_line(Names);
END;
END sp_Market;
How can I handle my string of queries and what data type is there to accomplish the task?
CLOB is OK (as far as I can tell); I doubt queries you store in there are that big.
Remove dbms_output.put_line call from the procedure; I suspect it is the one that raises the error.
I'm not sure how you got any runtime error, as your procedure won't compile.
The valid PL/SQL version would look something like this:
create or replace procedure sp_market is
names varchar2(32767);
begin
for r in (
select id, order_of, field_name
from pld_medicare_config
)
loop
names := names || ' ' || r.field_name;
end loop;
names := ltrim(names);
dbms_output.put_line(names);
end sp_market;
If names needs to be longer, change the datatype to clob.
Use the CLOB datatype and append data using the dbms_lob.writeappend procedure. This is the reference (Oracle 18c).
The error probably origins with the dbms_output.put_line call. The procedure is defined for varchar2 arguments only which means that an implicit conversion takes place during the call. It will fail for clob contents longer than 32767 chars/bytes.
Alternatively you may declare a collection over varchar2(4000) and fill the collection elements sequentially:
CREATE OR REPLACE PROCEDURE sp_Market
IS
TYPE tLongString IS TABLE OF VARCHAR2(4000) INDEX BY BINARY_INTEGER;
cNames tLongString;
BEGIN
DECLARE CURSOR cur IS Select ID, Order_of, field_name, sqql FROM pld_medicare_config;
BEGIN
FOR i IN cur
LOOP
cNames(cNames.COUNT+1) := i.sqql;
END LOOP;
END;
END sp_Market;
Note
Rectified code, will compile now.

Error executing DBMS_OUTPUT.PUT_LINE in Oracle

I am running DBMS_OUTPUT.PUT_LINE to send a message from a procedure and I am trying to display the debug information of what a type variable contains BFILE
create or replace PROCEDURE P_FILEUPLOAD_XML IS
v_dir gzvcatg.gzvcatg_desc11%TYPE;
l_dir VARCHAR2(35);
l_fil VARCHAR2(30) := 'ES0000251446027471.xml';
l_loc BFILE;
BEGIN
l_loc := BFILENAME(v_dir,l_fil);
DBMS_OUTPUT.PUT_LINE(l_loc);
END;
At the moment of executing my procedure and waiting for a response from the log:
Anyone know why the error is due and how to correct it.
UPDATE:
Following the recommendation in the MT0 response making use of DBMS_LOB.READ, try the following:
create or replace PROCEDURE P_FILEUPLOAD_XML IS
v_dir gzvcatg.gzvcatg_desc11%TYPE;
l_dir VARCHAR2(35);
l_fil VARCHAR2(30) := 'ES0000251446027471.xml';
l_loc BFILE;
BEGIN
l_loc := BFILENAME(v_dir,l_fil);
DBMS_LOB.READ(l_loc IN BFILE);
END;
But executing it generates the following error:
Anyone know why the error is due
l_loc is a BFILE.
DBMS_OUTPUT.PUT_LINE( item IN VARCHAR2 ) takes a VARCHAR2 data type as its argument.
You cannot implicitly cast a BFILE to a VARCHAR2 so the procedure call raise an exception as it is the wrong type of argument to call the function with.
and how to correct it.
Read the file using DBMS_LOB.READ and use UTL_RAW.CAST_TO_VARCHAR2 to convert the RAW value you get from the LOB to a string so you can print it.

How to pass record type value to function argument

May I know how to pass a record type value to a function argument?
Below is my attempt. I'm getting "wrong number or types of arguments in call..." error
I've searched but couldn't seem to find definite answer that will help me.
Hope someone can answer.
DECLARE
TYPE attribute_type_record IS RECORD
(
name VARCHAR2(100)
,value VARCHAR2(400)
,ind integer
);
type attribute_type is table of attribute_type_record index by binary_integer;
sid_att attribute_type;
ret varchar2(3000);
BEGIN
sid_att(1).name := 'SID_ATTRIBUTES.REFERENCE1';
sid_att(1).value := 'SID_ATTRIBUTES.REFERENCE1';
sid_att(1).ind := '1';
--v_row := sid_attributes_table('SID_ATTRIBUTES.REFERENCE1');
ret := SC$DATA_ENTRY.ins_sid_attributes(sid_att(1).name,
sid_att(1).value,
sid_att(1).ind,
'594191176',
'TEST');
END;
FUNCTION ins_sid_attributes (
p_sid_attributes_table sc$data_entry_utilities.attribute_table,
p_sid_id sid_attributes.sid_id%TYPE,
p_user IN VARCHAR2
)
RETURN VARCHAR2
IS
error_message VARCHAR2 (2000);
v_row sc$data_entry_utilities.attribute_type;
empty_row sc$data_entry_utilities.attribute_type;
v_order INTEGER := 0;
v_name VARCHAR2 (2000);
cat_table pkg_utilities.number_table;
-- I did not post the rest of the code of this function since i believe it is irrelevant to the error and to keep it short.
Thanks in advance!
noth
Assuming that the INS_SID_ATTRIBUTES procedure shown above is the one you're actually trying to call, the message seems reasonable to me. INS_SID_ATTRIBUTES, as shown above, wants three parameters:
A SC$DATA_ENTRY_UTILITIES.ATTRIBUTE_TABLE
A SID_ATTRIBUTES.SID_ID%TYPE
A VARCHAR2
But in the call you're passing it:
A VARCHAR2
A VARCHAR2
An INTEGER
A VARCHAR2
A VARCHAR2
So you're passing five arguments to a procedure that only accepts three, and the values you're passing are of the wrong types.
Share and enjoy.

PL/SQL numeric or value error when converting to number

I'm busy with a little PL/SQL program and i got stuck when i got the error "numeric or value error"
I'm trying to convert a varchar2 that consists of only numbers to a number using TO_NUMBER().
I'm relatively new to PL/SQL, so i'm thinking it's something really dumb :)
This is my code:
set serveroutput on
DECLARE
vRearranged varchar(50);
iNumber number(38);
BEGIN
vRearranged := 3214282912345698765432161100;
iNumber := To_Number(vRearranged, 50);
dbms_output.put_line(iNumber);
END;
could anyone help me?
The second argument of To_number() is the format picture, which should be a string.
http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions211.htm#SQLRF06140
DECLARE
vRearranged varchar2(50);
iNumber number(38);
BEGIN
vRearranged := 3214282912345698765432161100;
iNumber := To_Number(vRearranged);
dbms_output.put_line(iNumber);
END;

Resources