SELECT from Collection denies to accept to_number/to_char - sql

refering to my other post i decided to store numbers with leading zero in my database.
I'm selecting the imported data from a collection and insert it into a database table. The problem is that if I try to use the function to_char(c001,'009') on a column out of the collection it says "ORA-01722: invalid number".
I don't understand this because the datasource is varchar2 and the destination is varchar2, too.
Did anyone solve this issue?

Oracle's to_char function takes a number as it's first parameter. So the value you provide will be converted to number. If coo1 does not contain a valid number you will get that error message.
One of you numbers is not a number. You can use plsql code to find out what record contains an invalid number:
declare
l_number number;
cursor c is select ID,column_name from table;
begin
for r in c loop
begin
l_number := to_number(r.column_name);
exception
when others then dbms_output.put_line(r.id||' '||r.column_name);
end;
end loop;
end;

Related

PL/SQL equivalent of SELECT statement

What would be the PL/SQL equivalent of this SQL query:
SELECT * FROM table(OWNER.PACKAGE.get_exam('123456789'));
This is the Function that I am trying to call:
FUNCTION get_exam(id IN VARCHAR2)
RETURN ab_assign_v1
IS
CURSOR c_exams(cid VARCHAR2) IS
SELECT t_api_exam_v1(
sei.person_id, --unique id
l.description --loc description
)
FROM my_view sei
JOIN loc l
ON sei.loc_code = l.loc_code
v_collection ab_assign_v1;
BEGIN
OPEN c_exams(id);
FETCH c_exams BULK COLLECT INTO v_collection;
CLOSE c_exams;
RETURN v_collection;
EXCEPTION
WHEN OTHERS THEN
error_a1.raise_error(SQLCODE, SQLERRM);
END get_exam;
Hope this helps.
DECLARE
lv <COLLECTION_NAME>;
BEGIN
lv:= OWNER.PACKAGE.get_exam('123456789');
dbms_output.put_line(lv.COUNT);
END;
/
Assuming that you want to return the result of a function :
select owner.package.get_exam('123456789') from table
Your function returns a nested table type. You simply need to declare a variable of that type, and assign to it as you would if it were a scalar:
declare
l_coll your_collection_type;
begin
l_coll := OWNER.PACKAGE.get_exam('123456789');
end;
/
In this example your_collection_type is a placeholder for whatever object your function actually returns.
" I am getting this error: PLS-00201: identifier 'ab_assign_v1' must be declared "
ab_assign_v1 is the type used by your function. From the code posted in your revised question it seems that type is in the same schema which owns the package with the function. However your original pseudo-code prefixes the call with the schema name. So, putting two and two together, you need to revise the variable declaration to include the schema too. (You may need to grant EXECUTE on it too, if you haven't done this already).
declare
l_coll OWNER.ab_assign_v1;
begin
l_coll := OWNER.PACKAGE.get_exam('123456789');
end;
/

Error using Oracle Ref Cursor

I'm trying to do something fairly simple, I am trying to automate the removal and back up of tables from my personal table space. I have about 100 tables and want to get rid of all of them (except a table that I'm using to store the table names), but want to keep the data from the tables in case I need them sometime in the future. Below is the code I am trying to use to accomplish this. I am getting an error on the ref cursor, which I'll include below my code. I half expect somebody to tell me I'm an idiot and explain an easier way to do this. If not, please tell me what I'm doing wrong with the way that I am doing it, thanks.
DECLARE
v_folder_name VARCHAR2(100) := 'MY_FOLDER';
TYPE QRY_CURSOR IS REF CURSOR;
v_qry_cursor QRY_CURSOR;
v_file_name VARCHAR2(320);
v_file sys.utl_file.file_type;
v_max_buffer_length CONSTANT BINARY_INTEGER := 32767;
v_qry_str VARCHAR2(4000); --I've tried this with 32767, made no difference
v_drop_string VARCHAR2(4000);
v_dynamic_record VARCHAR2(4000); --tried this with 32767 also
CURSOR GET_TABLE_NAMES IS
SELECT * FROM TEMP_BACKUP_TABLE WHERE TABLE_NAME <> 'TEMP_BACKUP_TABLE';
FUNCTION startFile(file_name VARCHAR2)
--working function, used with many procedures, left out for brevity
END startFile;
FUNCTION closeFile(file_name VARCHAR2)
--working function, used with many procedures, left out for brevity
END closeFile;
BEGIN
INSERT INTO TEMP_BACKUP_TABLE SELECT DISTINCT TABLE_NAME FROM ALL_TAB_COLS WHERE OWNER = 'ME';
COMMIT;
FOR REC IN GET_TABLE_NAMES LOOP
v_file_name := REC.TABLE_NAME;
v_file := startFile(v_file_name);
v_qry_str := 'SELECT * FROM ' || v_file_name;
v_drop_string := 'DROP TABLE ' || v_file_name;
OPEN v_qry_cursor FOR v_qry_str; -- this is the line that returns an error
LOOP
FETCH v_qry_cursor INTO v_dynamic_record;
EXIT WHEN v_qry_cursor%NOTFOUND;
sys.utl_file.put_line(v_file, v_dynamic_record);
END LOOP;
CLOSE v_qry_cursor;
EXECUTE IMMEDIATE v_drop_string;
COMMIT;
v_file := closeFile(v_file_name);
END LOOP;
DELETE FROM TEMP_BACKUP_TABLE;
END;
The error I'm getting is as follows:
Error report:
ORA-00932: inconsistent datatypes: expected - got -
ORA-06512: at line 73
00932. 00000 - "inconsistent datatypes: expected %s got %s"
*cause:
*action:
Thanks for any help.
At a minimum, utl_file.put_line does not take arbitrary record and you can't fetch an arbitrary list of columns into a varchar2.
You could iterate over each column and construct a SQL statement that concatenates the values from each column into a single string. That would include doing things like putting a to_char with an explicit format mask on your date or timestamp columns, adding a delimiter, escaping any delimiters that exist in your data, etc. This is generally a rather tedious and error-prone process. And then you'll need to write a SQL*Loader control file to load the data back in the future.
It sounds like you'd be better off exporting the table using the Oracle export utility. That's a command-line utility (exp or expdp depending on whether you want to use the classic version or the DataPump version) that lets you export the table definition and data to a file that you can load later using the Oracle import utility.

How to execute a stored procedure with cursor and table OUT parameters through SQL Developer?

I’m having trouble testing, in SQL Developer 3.2.20.09, an Oracle stored procedure that contains 2 specificities:
a user defined "cursor type" output parameter
a user defined "TABLE OF VARCHAR type" output parameter.
Stored procedure signature:
TYPE ref_cursor_tst IS REF CURSOR;
TYPE arrWarningCode_tst IS TABLE OF VARCHAR2 (4000)
INDEX BY BINARY_INTEGER;
PROCEDURE SP_ITF_CU_DOCUMENT_Test (
p_projectNumber IN VARCHAR2,
p_tag IN VARCHAR2,
p_title IN VARCHAR2,
out_document_curs OUT ref_cursor_tst,
out_errorCode OUT VARCHAR2,
out_arrWarningCode OUT arrWarningCode_tst);
My actual best test code I could end up with:
set serveroutput on size 100000
DECLARE
docRef VARCHAR2(200);
outDocCurs PD360BADMIN.PKG_ITF_GENERAL_TST.ref_cursor_tst;
outErrorCode VARCHAR2(2000);
arrWarningCodes PD360BADMIN.PKG_ITF_GENERAL_TST.arrWarningCode_tst;
i PLS_INTEGER;
doc TBL_OBJECT%ROWTYPE;
BEGIN
dbms_output.put_line('debut de procedure');
docRef:= 'DOC-012';
arrWarningCodes.DELETE;
--call SP
PKG_ITF_GENERAL_TST.SP_ITF_CU_DOCUMENT_TEST (
p_projectNumber => 'XXX',
p_tag => docRef,
p_title => 'Doc title',
out_document_curs => outDocCurs,
out_errorCode => outErrorCode,
out_arrWarningCode => arrWarningCodes);
--print error code
dbms_output.put_line('out_errorCode=' || outErrorCode);
--print output cursor
--dbms_output.put_line(outDocCurs);
LOOP
FETCH outDocCurs INTO doc;
EXIT WHEN outDocCurs%NOTFOUND;
dbms_output.put_line(doc.OBJ_ID||','||doc.OBJ_TAG);
END LOOP;
--print warnings array
IF arrWarningCodes.count > 0 THEN
FOR i IN arrWarningCodes.FIRST .. arrWarningCodes.LAST LOOP
dbms_output.put_line('warning code=' || arrWarningCodes(i) );
END LOOP;
ENd IF;
dbms_output.put_line('fin de procedure');
END;
/
The error I get:
Error report:
ORA-06504: PL/SQL: Return types of Result Set variables or query do not match
ORA-06512: at line 30
06504. 00000 - "PL/SQL: Return types of Result Set variables or query do not match"
*Cause: Number and/or types of columns in a query does not match declared
return type of a result set variable, or declared types of two Result
Set variables do not match.
*Action: Change the program statement or declaration. Verify what query the variable
actually refers to during execution.
debut de procedure
out_errorCode=
I've been testing various solutions and syntaxes for days as well as digging the net and requiring help from different sources with no success.
Any clue would be much appreciated.
Assuming TBL_OBJECT is a table of some object type which has the two fields obj_id and obj_tag; and the procedure is currently doing something like:
open out_document_curs for select * from tbl_object;
... then there are two ways to make this work. The first is to change the variables you're fetching into to match the object fields, rather than the object itself:
DECLARE
...
-- doc TBL_OBJECT%ROWTYPE;
doc_obj_id TBL_OBJECT.OBJ_ID%TYPE;
doc_obj_tag TBL_OBJECT.OBJ_TAG%TYPE;
BEGIN
...
and then change the fetch and display:
LOOP
FETCH outDocCurs INTO doc_obj_id, doc_obj_tag;
EXIT WHEN outDocCurs%NOTFOUND;
dbms_output.put_line(doc_obj_id||','||doc_obj_tag);
END LOOP;
If the object has more fields then you'd need to define them all and specify them in the fetch too.
The other is to modify the procedure so that it returns an object type:
open out_document_curs for select value(t) from tbl_object t;
Then your calling anonymous block will work as it is, as the simple query will return the object itself rather than the fields within it.
Which you do will depend on how the procedure will really be used, rather than your test call.
With some dummy set-up:
create type doc_obj as object (obj_id number, obj_tag varchar2(10));
/
create table tbl_object of doc_obj;
insert into tbl_object values (doc_obj(1, 'Test'));
And a dummy package body with the procedure simplified as:
PROCEDURE SP_ITF_CU_DOCUMENT_Test (
p_projectNumber IN VARCHAR2,
p_tag IN VARCHAR2,
p_title IN VARCHAR2,
out_document_curs OUT ref_cursor_tst,
out_errorCode OUT VARCHAR2,
out_arrWarningCode OUT arrWarningCode_tst)
IS
BEGIN
open out_document_curs for select value(o) from tbl_object o;
out_errorCode := 'OK';
out_arrWarningCode(1) := 'Danger!';
END SP_ITF_CU_DOCUMENT_Test;
Then calling your code exactly as you have it in the question (minus the schema name) gives:
anonymous block completed
debut de procedure
out_errorCode=OK
1,Test
warning code=Danger!
fin de procedure
Using the other approach, with the individual variables for the object fields, gives the same result too.

Procedures or Functions (Pl/sql)

I am currently learning pl/sql using oracle 10g
I have a certain confusion
When should I use stored procedures and when should i go for functions?
Please help me out with some real world example.
Thank you.
A function returns a value, although that could actually be an object like a cursor.
Also only a function can be defined with the following (as of 11.1):
DETERMINISTIC option, which helps the optimizer avoid redundant function calls.
PARALLEL_ENABLED option, which allows the function to be used safely in slave sessions of parallel DML evaluations.
PIPELINED option, which returns the results of a table function iteratively.
RESULT_CACHE option, which stores function results in the PL/SQL function result cache.
RESULT_CACHE clause, which specifies the data sources on which the results of a function.
So if you need to return multiple values, use a procedure. However be aware that the above five features are then not available.
If you want to include a PL/SQL subprogram in a SQL statement then you probably want a function.
http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/subprograms.htm#CHDBEJGF
DECLARE
l_user_id VARCHAR2(1);
l_received_user VARCHAR2(30);
PROCEDURE print_user_name(user_name_in IN VARCHAR2)
AS
BEGIN
DBMS_OUTPUT.PUT_LINE('The user''s name is: ' || INITCAP(user_name_in));
END print_user_name;
FUNCTION get_user_name(user_id_in IN VARCHAR2) RETURN VARCHAR2
AS
l_user_name VARCHAR2(30);
BEGIN
SELECT 'Amanda'
INTO l_user_name
FROM dual
WHERE dummy = user_id_in;
RETURN l_user_name;
END get_user_name;
BEGIN
-- excute an action --
print_user_name('John');
l_user_id := 'X';
-- hold action's result in a variable --
l_received_user := get_user_name(l_user_id);
-- work with the received result/variable --
DBMS_OUTPUT.PUT_LINE('The received user''s name is: ' || INITCAP(l_received_user));
IF l_received_user = 'John' THEN
DBMS_OUTPUT.PUT_LINE('The received user''s name is John');
ELSE
DBMS_OUTPUT.PUT_LINE('The received user''s name is not John');
END IF;
END;
/*
The user's name is: John
The received user's name is: Amanda
The received user's name is not John
*/
Difference is that sored procedure do something, while functions do something and return result (variable or table).

dynamic declaration/query in oracle 9i

In Oracle, given a list of table names, I want to perform 'select column1 into var1 from table' statements on a mass number of tables. And I want to do this for all columns of a table. I cannot declare the type of var1 until the query with user_tab_columns returns the column's type. I tried to declare var1 as sys.anytype but got ORA-00932 with error message such as "inconsistent datatypes: expected CHAR got CHAR".
So how can I get past this error or how can I dynamically declare a variable? Many thanks.
Most datatypes will implicitly convert into a VARCHAR. Obviously there are exceptions, but if your tables are just varchars, dates, and numbers then you should be fine.
Craig is right you should probably declare it as a VARCHAR2 instad of an anytype.
This article by Jeff Hunter has a nice function that makes this easy to populate your variable in way that won't break if your data can't be converted.
CREATE OR REPLACE FUNCTION getData(data IN SYS.ANYDATA)
RETURN VARCHAR2
AS
l_varchar2 VARCHAR2(4000);
l_rc NUMBER;
BEGIN
CASE data.getTypeName
when 'SYS.NUMBER' then
l_rc := data.getNumber(l_varchar2);
when 'SYS.DATE' then
l_rc := data.getDate(l_varchar2);
when 'SYS.VARCHAR2' then
l_rc := data.getVarchar2(l_varchar2);
else
l_varchar2 := '** unknown **';
END CASE;
RETURN l_varchar2;
END;