Nitika19
asked on
print using dbms_output.put_line while triggers get executed
I have a web application in JSP with Oracle 8i as database. I have a few triggers in Oracle which contain dbms_output.put_line to print values on the SQL*Plus Interface(used for debugging). When the user performs an action in the application and the trigger or procedure gets executed I want that the dbms_output.put_line statement in that trigger or procedure prints on the SQL*Plus Interface.
Normally when i exceute procedure or trigger in SQL*Plus interface values are printed but not when they execute in the background as a result of operations performed by the user.
Normally when i exceute procedure or trigger in SQL*Plus interface values are printed but not when they execute in the background as a result of operations performed by the user.
dbms_output.put_line is for use in SQL*Plus environment where you have to use also the command
set serveroutput on
In server environment they can not find the terminal to display the data.
This is the reason to store the data you need either in a flat file, using UTL_FILE package or to store them in an Oracle table (the best solution).
set serveroutput on
In server environment they can not find the terminal to display the data.
This is the reason to store the data you need either in a flat file, using UTL_FILE package or to store them in an Oracle table (the best solution).
Nitika19:
Dbms_output has to be hosted in an environment like sqlplus that has the IO capability.
if you want to write OUTPUT to somewhere you can read, you can use UTL_FILE package.
this will write to a file in your desired directory, and you can read it later.
let me know if you need a sample code for that
Dbms_output has to be hosted in an environment like sqlplus that has the IO capability.
if you want to write OUTPUT to somewhere you can read, you can use UTL_FILE package.
this will write to a file in your desired directory, and you can read it later.
let me know if you need a sample code for that
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here are two examples show you use DBMS_OUTPUT.GETLINE and DBMS_OUTPUT.GETLINES to retrieve information from DBMS_OUTPUT buffer.
FUNCTION get_next_line RETURN VARCHAR2
IS
return_value VARCHAR2(255);
get_status INTEGER;
BEGIN
DBMS_OUTPUT.GET_LINE (return_value, get_status);
IF get_status = 0
THEN
RETURN return_value;
ELSE
RETURN NULL;
END IF;
END;
---------------
DECLARE
output_table DBMS_OUTPUT.CHARARR; /* output_buf_tab */
a_line VARCHAR2(10) := RPAD('*',10,'*');
status INTEGER;
max_lines CONSTANT NUMBER := 15;
BEGIN
output_table (0) := 'ABC';
output_table (12) := 'DEF';
/* Output 10 lines */
FOR linenum IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE (a_line || TO_CHAR (linenum));
END LOOP;
/* retrieve 15 lines, status will receive the line count */
status := max_lines;
DBMS_OUTPUT.GET_LINES ( output_table, status);
DBMS_OUTPUT.PUT_LINE ('lines retrieved= ' || status);
FOR linenum in 0..max_lines
LOOP
BEGIN
DBMS_OUTPUT.PUT_LINE
(linenum || ':' || NVL (output_table(linenum),'<n ull>') );
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE (linenum || ':' || sqlerrm );
END;
END LOOP;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE ('Exception, status=' || status);
DBMS_OUTPUT.PUT_LINE (SQLERRM );
END;
FUNCTION get_next_line RETURN VARCHAR2
IS
return_value VARCHAR2(255);
get_status INTEGER;
BEGIN
DBMS_OUTPUT.GET_LINE (return_value, get_status);
IF get_status = 0
THEN
RETURN return_value;
ELSE
RETURN NULL;
END IF;
END;
---------------
DECLARE
output_table DBMS_OUTPUT.CHARARR; /* output_buf_tab */
a_line VARCHAR2(10) := RPAD('*',10,'*');
status INTEGER;
max_lines CONSTANT NUMBER := 15;
BEGIN
output_table (0) := 'ABC';
output_table (12) := 'DEF';
/* Output 10 lines */
FOR linenum IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE (a_line || TO_CHAR (linenum));
END LOOP;
/* retrieve 15 lines, status will receive the line count */
status := max_lines;
DBMS_OUTPUT.GET_LINES ( output_table, status);
DBMS_OUTPUT.PUT_LINE ('lines retrieved= ' || status);
FOR linenum in 0..max_lines
LOOP
BEGIN
DBMS_OUTPUT.PUT_LINE
(linenum || ':' || NVL (output_table(linenum),'<n
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE (linenum || ':' || sqlerrm );
END;
END LOOP;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE ('Exception, status=' || status);
DBMS_OUTPUT.PUT_LINE (SQLERRM );
END;
ASKER
Thanks everyone for replying.
Can someone plz give a simple sample code for this. I used the code provided by schwertner but somehow i can't make it work.
Can someone plz give a simple sample code for this. I used the code provided by schwertner but somehow i can't make it work.
ASKER
I am doing the following but getting errors
create or replace procedure proc_sales_update(client in NUMBER,dept in varchar2,mnth in varchar2,year in varchar2,currency_sales in varchar2,v_amnt in number)
is
output_file utl_file.file_type;
cursor sale is select ....;
cursor exch is select ...;
cursor ..;
begin
output_file := utl_file.fopen ('C:\test','abc.txt', 'W');
utl_file.put_line (output_file, currency_sales);
utl_file.put_line (output_file, v_amnt);
utl_file.fclose(output_fil e);
X:='0';
open exch;
loop
fetch exch into v_exchange_rate;
exit when exch%notfound;
end loop;
close exch;
open exch1;
loop
fetch exch1 into v_exchange_rate;
exit when exch1%notfound;
end loop;
close exch1;
open sale;
loop
fetch sale INTO v_client,v_dept,v_month,v_ year,v_cur rency,v_am ount;
exit when sale%notfound;
...
x:='1';
end if;
end loop;
close sale;
if x='0' then
insert into
sales(client_id,dept_name,
month,year,currency,amount ,amount_rs )
values(client,dept,mnth,ye ar,currenc y_sales,v_ amnt,v_exc hange_rate *v_amnt);
end if;
end;
create or replace procedure proc_sales_update(client in NUMBER,dept in varchar2,mnth in varchar2,year in varchar2,currency_sales in varchar2,v_amnt in number)
is
output_file utl_file.file_type;
cursor sale is select ....;
cursor exch is select ...;
cursor ..;
begin
output_file := utl_file.fopen ('C:\test','abc.txt', 'W');
utl_file.put_line (output_file, currency_sales);
utl_file.put_line (output_file, v_amnt);
utl_file.fclose(output_fil
X:='0';
open exch;
loop
fetch exch into v_exchange_rate;
exit when exch%notfound;
end loop;
close exch;
open exch1;
loop
fetch exch1 into v_exchange_rate;
exit when exch1%notfound;
end loop;
close exch1;
open sale;
loop
fetch sale INTO v_client,v_dept,v_month,v_
exit when sale%notfound;
...
x:='1';
end if;
end loop;
close sale;
if x='0' then
insert into
sales(client_id,dept_name,
month,year,currency,amount
values(client,dept,mnth,ye
end if;
end;
When you use UTL_FILE package you should open access to the directory where the file reside. Look at my remarks. Check which version of Oracle are you using.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, ... misspelled...
change IPADDRESS parameter to IP_ADDRESS in sys_context function used in INSERT above..
Regards
change IPADDRESS parameter to IP_ADDRESS in sys_context function used in INSERT above..
Regards
Do you mean, send debugging information to SQL*PLUS even if you run your JSP application?
You can use DBMS_PIPE to establish inter-session messaging to accomplish this specific requirement.
But if you just want to track/debug out of your application, writing to file is a good approach. You can create a simple debug function of yours, in which you can use UTL_FILE instead of DBMS_OUTPUT, so that every debugging line is written to a file and can be referred later.
Tom Kyte's comment on this is undoubtedly invaluable:
<<quote>>
See
http://osi.oracle.com/~tkyte/article2/index.html#Errors
and look for the text:
"One utility I use frequently when writing"...
for my solution to this. I use UTL_FILE and a small routine we've written
called "debug.f" (works like C's printf). That way, I can see the output from a
session immediately by "tailing" or editing the file they produce.
<</quote>>
Hope, it will help.
Best of luck.