Oracle: How do I display DBMS_XMLDOM.DOMDocument for debugging? - oracle

Running Oracle 10g, Sqldeveloper 1.5.5
I want to view the contents of an DBMS_XMLDOM.DOMDocument as a string in the output or results window in sqldeveloper. or some other simple way to debug this thing...
Thanks,P

DBMS_XMLDOM.WRITETOBUFFER Writes the contents of the node to a buffer.
DBMS_XMLDOM.WRITETOCLOB Writes the contents of the node to a CLOB.
DBMS_XMLDOM.WRITETOFILE Writes the contents of the node to a file.
I have PL/SQL code that wites it to the file system using a DIRECTORY:
dbms_xmldom.writeToFile(dbms_xmldom.newDOMDocument( xmldoc)
,'DATAPUMPDIR/myfile.xml') ;
I have created a function using dbms_xmldom.writetoclob
create or replace function xml2clob (xmldoc XMLType) return CLOB is
clobdoc CLOB := ' ';
begin
dbms_xmldom.writeToClob(dbms_xmldom.newDOMDocument( xmldoc)
,clobdoc) ;
return clobdoc;
end;
/
Query:
SELECT xml2clob(Sys_Xmlagg(
Xmlelement(Name "dummy"
,dummy
),Xmlformat('dual')))
FROM dual;
Output:
<?xml version="1.0"?>
<dual>
<dummy>X</dummy>
</dual>
You could try using a function like this:
create or replace function dom2clob (domdoc DBMS_XMLDOM.DOMDocument) return CLOB is
clobdoc CLOB := ' ';
begin
dbms_xmldom.writeToClob(domdoc,clobdoc) ;
return clobdoc;
end;
/

You can accomplish the same as in:
"SELECT xml2clob(Sys_Xmlagg(Xmlelement(Name "dummy",dummy),Xmlformat('dual'))) FROM dual;"
with:
SELECT
Sys_Xmlagg(Xmlelement(Name "dummy",dummy)
,Xmlformat('dual')).Extract('/*').getClobVal() as "test"
FROM dual;
and you do not have to create the function "xml2clob"
.Extract('/*') is for "pretty printing"
output:
<dual>
<dummy>X</dummy>
</dual>

If you already have an xmltype simply use the function getClobVal()
xmldoc.getClobVal()
This returns your XMLType as a clob without the additional function overhead.

Related

Oracle database loading data from text file to database using UTL_file and dbms

I am trying to upload data from text file to my table using UTL_FILE from SQL PLUS.
the text file name testb.txt contain data:
10,mike
20,bill
30,kim
and in my the table in the database called mytable contain columns (empno, empname)
I tried this code on SQL plus:
declare
filehand utl_file.file_type;
line varchar2(4000);
vempno varchar2(50);
vempname varchar2(50);
begin
filehand := utl_file.fopen('c:\users\myuser\desktop', 'testb.txt', 'r');
loop
utl_file.get_line(filehand, line);
if line is not null then
vempno := substr(line, 1, instr(line, ',')-1);
vempname := substr(line, instr(line, ',')+1, length(line));
dbms_output.put_line(vempno);
dbms_output.put_line(vempname);
insert into mytable (empno, empname) values (vempno, vempname);
commit;
else
exit;
end if;
end loop;
commit;
utl_file.fclose(filehand);
exception
when others then
null;
end:
/
when I run this code I got PL/SQL procedure successfully completed.
but when I see my table nothing change and the data not loaded from the text file to my table.
???
whats wrong in my code and what to do to upload the data from the text file to my table using UTL_file and dbms ??
Please guys help me I really need it.
Thank you very much

Creating Oracle PL/SQL Stored procedure

I'm trying to convert the SQL Query to Oracle PL/SQL stored procedure.
Here is the query:
select * from table1 where DATE = " + s1 + " and TYPE='" + ty + "' and NAME='"+nm+"' Order by TYPE DEsc;
Here is the Stored Procedure:
CREATE PROCEDURE procedure1
(
s1 IN DATE,
ty IN CHAR DEFAULT 2,
nm IN VARCHAR2 DEFAULT 64
)
IS
d table1.DATE%TYPE;
C table1.TYPE%TYPE;
S table1.NAME%TYPE;
CURSOR tb IS select DATE,TYPE,NAME INTO d,c,s from table1;
BEGIN
FOR i IN tb
LOOP
DBMS_OUTPUT.PUT_LINE('DATE' ||i.DATE);
DBMS_OUTPUT.PUT_LINE('TYPE' ||i.TYPE);
DBMS_OUTPUT.PUT_LINE('NAME' ||i.NAME);
END LOOP;
END procedure1;
I do not see any output after Executing Stored procedure. I'm not sure if I have created the stored procedure correctly.
"I do not see any output after Executing Stored procedure"
Your "output" is DBMS_OUTPUT which is for displaying text to a screen. However, by default it writes the text to a buffer, and we need to enable the output to see the contents of the buffer.
How to do this varies depending on which client you're using. In SQL*Plus it's
SQL> set serveroutput on
In an IDE like TOAD, PLSQL Developer or Oracle SQL Developer there's a separate DBMS_OUTPUT tab: click on the tab and enable output (there's a button) - or set Preferences to always have it on.
DBMS_OUTPUT is rarely a useful means for returning data in an actual application. The normal approach is to use a Ref Cursor, which maps to JDBC and ODBC ResultSet classes. Something like this:
CREATE OR REPLACE PROCEDURE procedure1
(
s1 IN DATE,
ty IN CHAR DEFAULT 2,
nm IN VARCHAR2 DEFAULT 64,
rc out sys_refcursor
)
IS
BEGIN
open rc for
select * from table1
where d = s1
and c = ty
and s = nm;
END procedure1;
/
Incidentally, your parameters are defined with string datatypes but the defaults are numeric values. Please don't get into bad habits. Strong datatyping is a key defence against data corruption and broken code, so always use the correct data type.
try this;
CREATE PROCEDURE PROCEDURE1 (
S1 IN DATE,
TY IN CHAR DEFAULT 2,
NM IN VARCHAR2 DEFAULT 64
)
IS
BEGIN
FOR I IN (SELECT DATE, TYPE, NAME FROM TABLE1)--Easier way to use cursor
LOOP
DBMS_OUTPUT.PUT_LINE ('DATE' || I.DATE);
DBMS_OUTPUT.PUT_LINE ('TYPE' || I.TYPE);
DBMS_OUTPUT.PUT_LINE ('NAME' || I.NAME);
END LOOP;
END PROCEDURE1;
by executing this you only created the procedure and stored it in db, you need to call it and turn on system output to see the output. like this:
set serveroutput on;
begin
PROCEDURE1(null, null, null);
end;
What environment are using to compile your code? You should certainly be seeing some immediate feedback.
Note that in most environments, though, you need to do a little more than you did before.
The final ";" in your code is part of PL/SQL. It does not trigger execution of your DDL. Generally you should do this:
CREATE OR REPLACE PROCEDURE myproc
IS
BEGIN
...
END myproc;
/
And that "/" will submit your statement for execution.

how to corectly call table function in PL/SQL

I have table function which returns table of file names (type t_file_list is table of clob;) from .zip file (in BLOB format) the header is like:
function get_file_list(
p_zipped_blob in blob
,p_encoding in varchar2 := null
)
return t_file_list
is
.....
end;
and I need to select these file names and for each call some procedure, but i cant find way to call function get_file_list correctly, I try this:
for i in (select * from table(zip_util_pkg.get_file_list(ab_zipped_blob)))
loop
.....
end loop;
but it gives me some errors like ORA-22905 and PLS-00642. Can someone tell me what I am doing wrong and how to call table function correctly?
No need to use SQL - you can do it entirely in PL/SQL:
DECLARE
p_files ZIP_UTIL_PKG.T_FILE_LIST;
BEGIN
p_files := zip_util_pkg.get_file_list(ab_zipped_blob);
FOR i IN 1 .. p_files.COUNT LOOP
some_procedure( p_files(i) );
END LOOP;
END;
/

How do I return the rows from an Oracle Stored Procedure using SELECT?

I have a stored procedure which returns a ref cursor as follows:
CREATE OR REPLACE PROCEDURE AIRS.GET_LAB_REPORT (ReportCurTyp OUT sys_refcursor)
AS
v_report_cursor sys_refcursor;
report_record v_lab_report%ROWTYPE;
l_sql VARCHAR2 (2000);
BEGIN
l_sql := 'SELECT * FROM V_LAB_REPORT';
OPEN v_report_cursor FOR l_sql;
LOOP
FETCH v_report_cursor INTO report_record;
EXIT WHEN v_report_cursor%NOTFOUND;
END LOOP;
CLOSE v_report_cursor;
END;
I want to use the output from this stored procedure in another select statement like:
SELECT * FROM GET_LAB_REPORT()
but I can't seem to get my head around the syntax.
Any ideas?
Whenever I've had to do this; I've used the Oracle TYPE and CAST features.
Something like:
SELECT *
FROM TABLE(CAST(F$get_Cassette_Tracking('8029241') AS cass_tracking_tab_type))
You need to setup the TYPE and all the columns you need and have them use:
pipe ROW(out_obj)
to capture your data. There are many ways to do this and if I can dig out a better example I will but this might give you an idea.
See this SO for a working example: Oracle Parameters with IN statement?

How to show result when table is my type in Oracle

I created a simple Oracle type:
create or replace TYPE MY_TYPE AS OBJECT (ID NUMBER(30), NAME VARCHAR2(20));
Then, I created a second table type:
create or replace TYPE MY_TYPE_TABLE AS TABLE OF MY_TYPE;
Finally, I created a simply function:
create or replace FUNCTION my_function(line_id IN NUMBER) RETURN MY_TYPE_TABLE
AS
return_data MY_TYPE_TABLE := MY_TYPE_TABLE();
BEGIN
return_data.EXTEND;
return_data(return_data.count) := (MY_TYPE(10, 'BOB')) ;
return_data.EXTEND;
return_data(return_data.count) := (MY_TYPE(11, 'ALAN')) ;
RETURN return_data;
END SETTLEMENT_NET_TRACKING;
My question: How to run this function that result like this:
10 BOB
11 ALAN
Hot to do it?
If you're looking for logging in that method you could use DBMS_OUTPUT to log to the standard output or use UTL_FILE to log to a file. I've suggested in previous questions that if you're designing anything bigger than a trivial application you'll want to create a custom logging package.
For your problem it would look something like this:
dbms_output.enable;
for i in return_data.first..return_data.last loop
dbms_output.put_line(return_data(i).id || ' ' || return_data(i).name);
end loop;
You'll need to enable output in your client application. For SQLPlus you'd use SET SERVEROUT ON before you call your API.
You can also do
select * from table(my_function(30))

Resources