Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Vasaku.

Asked: August 05, 2002 - 9:49 am UTC

Last updated: January 02, 2005 - 12:42 am UTC

Version: Forms 6i

Viewed 10K+ times! This question is

You Asked

Tom

I have a form , and the requirements is after the user queries the forms, he should have the ability to click a button which will write the data from the block to a 1.flat file , 2.Exel work sheet.

Can you provide me withe the information as to how we can achieve this.

Thanks

Vasaku

and Tom said...

1) and 2) are basically the same -- since a .CSV (comma separated values) file is both a flat file and something Excel will gladly open.

You would use TEXT_IO in forms to open and write the text file. You would use forms built-ins to navigate the block you wanted to save (first_record, next_record built-ins).

If you wanted to get "fancy", you could use the SYLK file format for the worksheet. Lets you use fonts and such in the spreadsheet. Search this site for OWA_SYLK UTL_FILE to see an example from the database (you would replace utl_file with TEXT_IO).

Rating

  (21 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Would like more info. about OWA_SYLK UTL_FILE:

Mr. 420, August 06, 2002 - 10:21 am UTC

Hi Tom! This OWA_SYLK sounds pretty cool, because it allows you to use different fonts, does it allow us do something else also? I went to Oracle 9i documentation at
</code> http://tahiti.oracle.com/pls/db92/db92.homepage?remark=tahiti <code>but to my surprise my search turned up nothing regarding this package/utility. If you can point me in the right direction, I would really appreciate it! Thanks!

Tom Kyte
August 07, 2002 - 10:06 am UTC

Search this site
for OWA_SYLK UTL_FILE to see an example from the database


SYLK is an industry standard file format (search GOOGLE for info on SYLK)

OWA_SYLK is a package Chris Beck and I wrote together -- its "freeware" available here.

Vasaku, August 18, 2002 - 10:01 am UTC

After writing to the Excel file, how can I automatically open it. That is , I dont want to go and click the csv file to open it . As soon as I finish writing to csv, the file should open itself.

I tried using..

HOST FILEPATH/FILENAME.. but doing this the dos window comes up and closes but does not open the file.

Advise please.

VERY VERY useful

Vasaku, August 18, 2002 - 4:06 pm UTC

But Tom, when the csv file we are trying to write to is already in use we get an error when we try to write to it(ORA-302000).

Now how can we caputure the state of the csv file we are writing even before we try to write to that file , so that we can avoid writing to it and prompt the user to close that file ,and then try to write to it..

Thanks


Tom Kyte
August 18, 2002 - 4:11 pm UTC

Thats windows locking it.

I guess you'll have to open it, try to write to it and catch the exception (that is how you'll discover that it is already optening).

Else, you'll want to write a windows dll with windows specific OS calls that you can call from ORA_FFI. These windows specific calls (of which I know not -- not a windows programmer) would tell you if the file is locked or not.

Path of least resistance -- write a small routine that opens and trys to write to the file and then closes it. Have that return a boolean true or false. Call that utility routine.

LPAD RPAD

Vasaku, August 18, 2002 - 4:51 pm UTC

Tom

I'm building the query in forms dyanmically to pass it to the dump_csv routine like

if some condition then
p_query := P_query ||' another where condition'

I have a lot like the above.

To start with I have default sql like

Select
col1,
col2
....
col10
from tabl
tab2
tab3
where condition 1
conditon 2
condition 3
rest of them to be developed dyanmically

In my when button pressed trigger which calls dump_csv I', repeatedly running into ora-06502. But I have declared it as p_query (32000) which shoul be more than sufficient, and I'm doing no conversions at all.

Now I thin I should get rid of the empty spaces for each column above. Is it possible that I pass the entire sql into a function and get the scrubbed output back without any empty spaces.

Can you suggest the solution



Tom Kyte
August 18, 2002 - 7:14 pm UTC

Your where clause is probably getting the 6502 -- not the query string itself.

You are building a where clause that compares a number to a date or a string to a number or something and the conversion error is happening during the execution of the QUERY itself.

You can prove this by putting an exception block around the execute statement in the procedure. You can:

begin
execute it
exception
when others then raise_application_error( -20001, 'Error processing your query ' || sqlerrm );
end;

if you start getting ora-20001's you know it is the query itself that is generating the error (you are putting invalid where clauses there)


question about dump csv again

Vasaku, August 19, 2002 - 10:55 am UTC

Tom

As youknow I'm using dump_csv from oracle forms using TEXT_IO.

now for one of the columns in the query I'm passing, I need not the column values, but the look up description for that column value.

And that column is always going to be in position 5 in the select clause.

For example lets say that the 5th column is deptno , but I want to write dname to the flat file.

I see two ways of doing it .

1.Write a database function returning the description and make it part of the select.

2.Capture the column in your dump_csv and get the description.

Can you please show how to achieve the solution using 2, as I'm discouraged to use functions in the above scenario.

Thanks

Tom Kyte
August 19, 2002 - 11:23 am UTC

I see many many more then 2 ways.

3) use a join

select emp.empno, emp.ename, dept.dname, emp.hiredate
from emp, dept
where emp.depto = dept.deptno

4) use a select of a select

select empno, ename, (select dname from dept where dept.deptno=emp.deptno), emp.hiredate
from emp;



Use #3.

#4 is more appealing

Vasaku, August 19, 2002 - 11:35 am UTC

to me than #3, as I dont want to add an additional table to my query.

Theoritically which do you think is more economical i.e. #3,#4 or using a database function, or doing the look up in the column count loop in the dump_csv routine.

Actually, even in number 4 the way I seeit , if the lookup table has a lot of rows and not properly indexes that will slow down the query in a big way.

Why did you suggest #3 and not #4.

Thanks

Tom Kyte
August 19, 2002 - 12:57 pm UTC

Doh -- read it again. How many tables are in query #3, now, how many queries are in query #4

The answer is the SAME.

You scare me (people like you do). the database was built to join, joins are not evil, improperly designed tables (eg: a lookup without an index perhaps) are. And in fact, query #3 would be infinitely preferable in all cases give this example -- and we would certainly hope the cost based optimizer would AVOID any and all indexes and do the right thing which would be a hash join for a large set!!!! That query doesn't WANT to use any indexes



o.k.

VASAKU, August 19, 2002 - 3:31 pm UTC

Tom

Sorry for scaring you, but I have never used select within a select the way you have used , that was the reason I was concerned.

So in effect when optimizer resolves #3 and #4 it takes the same path according to you.

Now if #3 is better as you have said, I was interested to know as to why #3 is more preferable to #4, and in what cases #4 would be the ideal solution.

Once agan sorry for scaring you.


Tom Kyte
August 19, 2002 - 4:05 pm UTC

No, you scare me because you are "afraid of joins", and that means you don't believe the database should be asked to do heavy lifting and that is exactly what the database is about.

I can show you 25 table joins that outperform a single table query.

I can show you the opposite.

The number of tables referenced in a query has no bearing whatever on its performance.


#4 is cool if you want to avoid an outer join and there is a change that a row in emp won't have a mate in dept. It has other uses as well.

link

Vasaku, August 19, 2002 - 4:38 pm UTC

---------
#4 is cool if you want to avoid an outer join and there is a change that a row
in emp won't have a mate in dept. It has other uses as well.
-------------
Tom can you give me a link or discuss the "other uses as well"

How can I avoid a outer join using #4

Tom Kyte
August 19, 2002 - 7:09 pm UTC

The "how can I avoid an outer join" should be obvious. You use that technique (#4) to pick up the dname from dept instead of outer joining EMP to DEPT -- thats that.


Its other users might be to get the first rows of a complex query back faster -- to run little correlated subqueries for each row instead of a massive sort/aggregate.

I've used it when I don't know what table I want to join to -- consider:

select object_type, object_name,
decode( status, 'INVALID', '*', '' ) status,
decode( object_type,
'TABLE', (select tablespace_name from user_tables where
table_name = object_name),
'TABLE PARTITION', (select tablespace_name from
user_tab_partitions where partition_name = subobject_name),
'INDEX', (select tablespace_name from user_indexes where
index_name = object_name),
'INDEX PARTITION', (select tablespace_name from
user_ind_partitions where partition_name = subobject_name),
'LOB', (select tablespace_name from user_segments where
segment_name = object_name),
null ) tablespace_name
from user_objects a
order by object_type, object_name
/

(oh yes, I know I could have used user_segments -- I just found the performance of that to not be quite as good as this)

Use your imagination.

Another way to get data to EXCEL from FORMS

Tom Reid, August 20, 2002 - 6:01 am UTC

An often overlooked way to get data from forms to EXCEL is by using the PLL library function write_block. You can define the separation character between the fields it writes, thus can easily produce CSV output files for reading into EXCEL. If you put the code for this behind an ICON your whole forms app can use it.

Please see my website </code> http://the-big-o.port5.com <code>for the articles on using PLL libraries in FORMS

INLINE VIEW IN FROM CLAUSE AND DUMP_CSV

Vasaku, August 20, 2002 - 12:10 pm UTC


Is the dump_csv smart enough to handle inline views in the from clause of a select which we pass into dump_csv.

For example if the from clause is like

emp,dept,(select a,b from c)

can the dbms_sql.parse capable enough to parse the above from clause

Tom Kyte
August 20, 2002 - 2:07 pm UTC

yes.

WinApi for selecting directory and path

Vasaku, August 23, 2002 - 1:08 am UTC

How can I call the utility which will open a dialog box, just like save as or open in windows , and will allow the user to select the directory and file to write his Excel output .

Does forms has any builtins to call the win api dialog box to select the directory and file?

Tom Kyte
August 23, 2002 - 7:04 am UTC

Not that I am aware of. Forms has a utility to let you pick an existing file (get_filename or something like that) but I'm not sure if it lets you pick a non-existent one.

try </code> http://otn.oracle.com/ <code>under discussion forums -> developer. I myself haven't written a form since 1995...

Know before hand

Vasaku, August 23, 2002 - 1:11 am UTC

Tom

Is there any way I can gather the amount of data which a query is going to return,i.e. I want to write to excel file only when the amount of data is more that a particular size or particular number of rows.

Is this possible , or how can we make this possible?

Tom Kyte
August 23, 2002 - 7:08 am UTC

Not even we know the answer to that.

Sure, we can "estimate" it -- if you were to explain the query and you were using the cost based optimizer -- we attempt to guesstimate the cardinality of the result set:

scott@ORA920.LOCALHOST> analyze table emp compute statistics;
Table analyzed.

scott@ORA920.LOCALHOST> analyze table dept compute statistics;
Table analyzed.

scott@ORA920.LOCALHOST> set autotrace traceonly explain
scott@ORA920.LOCALHOST> select * from emp;

Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=14 Bytes=448)
1 0 TABLE ACCESS (FULL) OF 'EMP' (Cost=2 Card=14 Bytes=448)



scott@ORA920.LOCALHOST> select count(*) from emp;

Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1)
1 0 SORT (AGGREGATE)
2 1 TABLE ACCESS (FULL) OF 'EMP' (Cost=2 Card=14)



scott@ORA920.LOCALHOST> select * from emp, dept;

Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=10 Card=56 Bytes=2800)
1 0 MERGE JOIN (CARTESIAN) (Cost=10 Card=56 Bytes=2800)
2 1 TABLE ACCESS (FULL) OF 'DEPT' (Cost=2 Card=4 Bytes=72)
3 1 BUFFER (SORT) (Cost=8 Card=14 Bytes=448)
4 3 TABLE ACCESS (FULL) OF 'EMP' (Cost=2 Card=14 Bytes=448)



scott@ORA920.LOCALHOST> set autotrace off




So, you could explain the query and use the estimated total CARD but it could be way off.

The bottom line, until you fetch the last row -- neither one of us knows the total number of rows you'll be getting!



How to send reports to excel tab pages in web.

suresh, October 31, 2003 - 7:23 am UTC

I have a form and the requirements is after the user queries the forms, he should have the ability to click a button which will write the data from the block to a Exel work sheets(Multiple Tabpages - Each sheet has one report).
(Take dept report is send to one tab sheet and emp is send another tab sheet ) and i am generating this report in Web
by using Oracle9i and Forms 6i.

I am invoking the Excel using

OWA_UTIL.MIME_HEADER('application/vnd.ms-excel');

Can you provide me with the information as to how we can achieve this.( pls tell me all possible solutions).

pls reply immd...

Regards,
suresh.


Tom Kyte
October 31, 2003 - 8:11 am UTC


sorry -- don't do windows myself. the only thing i know is an open systems standard called SYLK that is not MS proprietary. It is a generic file format. Do not know if it does "work sheets" or not.



very very urgent - excel reports.

Suresh, October 31, 2003 - 10:18 am UTC

Tom ,

I have a form , and the requirements is after the user queries the forms, he should have the ability to click a button which will write the data from the block to a
Exel work sheets(Multiple tabs).

(This is not related to Multiple-tab sheets, Consider Each report as Individual Report).

But client asking to consider each tab sheet as one ExcelReport. (Consider Dept as one excel report and emp as another excel report).(no tab sheets)

i am Written one procedure to invoke excel by using
OWA_UTIL.MIME_HEADER('application/vnd.ms-excel');
and sended data to this invoking excel(dept report).

but problem is next to invoke excel again and generate another excel emp report.

i have one button in form to generate two excel reports ( dept , emp as separate excel reports ) ?

i am done this application in Web, using Oracle 9i & Forms 6i(when ever i click this button in web , it generate two separate excel reports(dept,emp) ).

Can you provide me with the information as to how we can achieve this.
Pls reply immd by all possible Options. very Urgent.

Thanks
Suresh.


Tom Kyte
November 01, 2003 - 11:52 am UTC



as i've said before -- i don't do windows.

i don't use excel

i quite simply "don't know"

Forms6i on SUN Solaris Sparc

Andrea, December 29, 2004 - 11:47 am UTC

Hi Tom, may I ask you the feasilibity and/or how may I write a file on the client (e.g.: csv file under C:\ ) from a screen (developed with Forms6i) that is on the remote Sun Solaris SPARC cluster server (having unix as OS) ?
I have developed screen that writes csv files on the SUN Solaris cluster server..... and I'm wondering if it's possible to do it remotly.....
Thanks for your help.
Andrea

Tom Kyte
December 29, 2004 - 7:17 pm UTC

it would be a virus at that stage.

or the clients filesystem needs be mounted to the unix machine so it appears as a local filesystem.

there are lots of utilities with the latest versions of forms that permit many more things -- you can ask about them on otn.oracle.com in the discussion forums (it has been a DECADE since I've touched forms -- this html thing came along and I've not looked back)

Not useful question

Pieraldo, December 30, 2004 - 5:39 am UTC

Not at all

Excel and HTML

Mike Friedman, January 02, 2005 - 12:42 am UTC

One thing that hasn't been mentioned is Excel's HTML capabilities.

I've recently done a prototype that reads in an Excel HTML file as a template and then generates an output report in Excel based on the template.

It's very cool.

If there is demand here I can see about developing it further and putting it up on the web.

Ora-302000

Marcos, January 22, 2007 - 8:37 am UTC

I have a problem seemed. When i try to write a log file into the server, show me this error Ora-302000.

Forms version 9.0.4.0.19.

code:

DECLARE
V_ARQUIVO TEXT_IO.FILE_TYPE;
V_FILENAME VARCHAR2(16) := '/logs/teste.log';
BEGIN
V_ARQUIVO := TEXT_IO.FOPEN(V_FILENAME, 'W');
TEXT_IO.PUT_LINE(V_ARQUIVO,'TEST');
TEXT_IO.FCLOSE(V_ARQUIVO);
END;


obs: i'm from Brazil, and i don't know speak english.

Thanks

creating excel file with password protected

wahid, January 28, 2007 - 10:46 am UTC

how can i export data from pl/sql to an excel file with password protected. so that only who knows the password to open the file can read that. is there any function in pl/sql to make it?

regarding same issue

deepthi, September 18, 2008 - 12:10 pm UTC

i also same problem xls sheet is opening but data is not exporting.can anyone suggest the code after retrieving data from table

what permissions do you have to grant to execute text_io

Dean, August 28, 2015 - 6:47 pm UTC

DBA level user can write file with text_io, non dba users can't.

Form when button pressed trigger

TFile := TEXT_IO.FOpen( 'C:\..\filename.txt', 'w' ) ;
first_record;
loop
Line :='Test line';
TEXT_IO.PUT_LINE( TFile, Line ) ;
...

TEXT_IO.FClose( TFile);