You are on page 1of 7

Oracle Apps - UTL_FILE Example

DECLARE FH UTL_FILE.FILE_TYPE; LN VARCHAR2(300); V_NAME VARCHAR2(30); V_JOB VARCHAR2(30); V_SAL NUMBER(7,2); POS1 NUMBER(2); POS2 NUMBER(2); BEGIN FH :=UTL_FILE.FOPEN ( 'D:\ORACLE\VISDB\8.1.6\PLSQL\TEMP', 'A.LST', 'R'); LOOP UTL_FILE.GET_LINE(FH, LN); POS1 := INSTR(LN,',',1,1); POS2 := INSTR(LN,',',1,2); V_NAME := SUBSTR(LN,1,POS1-1); V_JOB := SUBSTR(LN,POS1+1,POS2 - POS1 -1); V_SAL := RTRIM(SUBSTR(LN,POS2+1)); INSERT INTO UTLX VALUES (V_NAME, V_JOB, V_SAL); -- DBMS_OUTPUT.PUT_LINE(V_NAME||' '||V_JOB||' '||V_SAL); END LOOP; EXCEPTION WHEN NO_DATA_FOUND THEN UTL_FILE.FCLOSE(FH); COMMIT; WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE(SQLERRM(SQLCODE)); END; /

racle Apps - Lexical Parameters Interview Questions


1. Defining Runtime Queries with Lexical Parameters
You can modify the report query at runtime using lexical parameters. A lexical parameter is a placeholder column containing the actual text to be used in a query. To illustrate this concept, open the matrix report built earlier. For this report, create a parameter for the report year and a parameter for the user to enter one of the following values, depending on the data preference: 2. Function Data Value

SUM Monthly total orders shipped AVG Average shipping delay by month MAX Maximum shipping delay by month Also, create the appropriate validation triggers for the individual parameters. Next, create a placeholder column at the report level called SELECTION_DATA. Set this field as a character field with a width of 100 characters and assign a default value of h.hist_ord_shipped. Next, create a before report trigger as follows: function Before_Report_Trigger return boolean is begin if :P_REPORT_TYPE = `SUM' then :SELECTION_CRITERIA := `h.hist_ord_shipped'; elsif :P_REPORT_TYPE = `AVG' then :SELECTION_CRITERIA := `h.hist_ship_days / h.hist_ord_shipped'; else :SELECTION_CRITERIA := `h.hist_max_days'; end if; end; Finally, modify the query: select w.wh_name WAREHOUSE, h.hist_month_no MONTHNO, to_char (to_date (to_char (h.hist_month_no), `MM'),'MON') RPT_MONTH, &SELECTION_CRITERIA from warehouses w, warehouse_history h where w.wh_code = h.hist_wh_code and h.hist_year = :P_year h.hist_month_no MONTHNO, to_char (to_date (to_char (h.hist_month_no), `MM'),'MON') RPT_MONTH, &SELECTION_CRITERIA from warehouses w, warehouse_history h where w.wh_code = h.hist_wh_code and h.hist_year = :P_year You reference the lexical parameter in the query using an ampersand (&) before the parameter name. Lexical parameters within a query substitute the text stored in the parameter directly into the query. For this reason, when using a lexical parameter, you must enter a default value for NULL values to assist with compilation in the designer.

3. Ten Tips for Oracle Reports


Oracle Reports is a powerful tool that you can use to generate useful reports against Oracle databases. Although there are no hard and fast rules regarding how you should use the tool, experience has borne a number of tips that make use of this tool much easier: Attempt to lay out the report on paper. This assists with the development of the data model as well as the final layout. Understand where subtotals should be provided to create the data breaks up front. When the default layout is used, define the page size to be excessively wide. You can then resize and reposition the data columns to fit within the printable page. If possible, formulate the data retrieval in a single query. Experience has shown that a single, somewhat inefficient query can perform better than several, dependent, well-tuned queries. Complete the data model before attempting to finalize the layout. The addition of a single column in a query might necessitate a redesign of the layout and thus a misuse of time.

When adding an additional break level to an existing report, 90 percent of the time it is faster to redo the default layout. Adding another intermediate level frame is one of the most difficult tasks to be done. It can be done but is often not worth the effort. Rather than try to resize or reposition objects in the Layout Editor, use the Size Objects and Align Objects tools. You can select several columns at once, quickly make them all the same custom size, and then align and space them with minimal effort. To lock the relative position of multiple objects, select them and create a group to join them together. Use the Magnify tool to zoom in to view the relative positions of the objects or to zoom out to view the total report structure. When you make a mistake in the editor, use Edit | Undo to reverse the action rather than try to correct it with the mouse. Before running any report, save it in a file to make sure that it can be recovered. Also, save different versions to facilitate recovery.

SQL Loader Mostly asked Interview questions 1. What is SQL*Loader and what is it used for?
SQL*Loader is a bulk loader utility used for moving data from external files into the Oracle database. Its syntax is similar to that of the DB2 Load utility, but comes with more options. SQL*Loader supports various load formats, selective loading, and multi-table loads.

2. How does one use the SQL*Loader utility?


One can load data into an Oracle database by using the sqlldr (sqlload on some platforms) utility. Invoke the utility without arguments to get a list of available parameters. Look at the following example: sqlldr scott/tiger control=loader.ctl This sample control file (loader.ctl) will load an external data file containing delimited data: load data infile 'c:\data\mydata.csv' into table emp ( empno, empname, sal, deptno ) fields terminated by "," optionally enclosed by '"' The mydata.csv file may look like this: 10001,"Scott Tiger", 1000, 40 10002,"Frank Naude", 500, 20 Another Sample control file with in-line data formatted as fix length records. The trick is to specify "*" as the name of the data file, and use BEGINDATA to start the data section in the control file. load data infile * replace into table departments ( dept position (02:05) char(4), deptname position (08:27) char(20) ) begindata

COSC ENGL MATH POLY

COMPUTER SCIENCE ENGLISH LITERATURE MATHEMATICS POLITICAL SCIENCE

3. Is there a SQL*Unloader to download data to a flat file?


Oracle does not supply any data unload utilities. However, you can use SQL*Plus to select and format your data and then spool it to a file: set echo off newpage 0 space 0 pagesize 0 feed off head off trimspool on spool oradata.txt select col1 || ',' || col2 || ',' || col3 from tab1 where col2 = 'XYZ'; spool off Alternatively use the UTL_FILE PL/SQL package: rem Remember to update initSID.ora, utl_file_dir='c:\oradata' parameter declare fp utl_file.file_type; begin fp := utl_file.fopen('c:\oradata','tab1.txt','w'); utl_file.putf(fp, '%s, %s\n', 'TextField', 55); utl_file.fclose(fp); end; / You might also want to investigate third party tools like TOAD or ManageIT Fast Unloader from CA to help you unload data from Oracle.

4. Can one load variable and fix length data records?


Yes, look at the following control file examples. In the first we will load delimited data (variable length): LOAD DATA INFILE * INTO TABLE load_delimited_data FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS (data1, data2) BEGINDATA 11111,AAAAAAAAAA 22222,"A,B,C,D," If you need to load positional data (fixed length), look at the following control file example: LOAD DATA INFILE * INTO TABLE load_positional_data (data1 POSITION(1:5), data2 POSITION(6:15) ) BEGINDATA 11111AAAAAAAAAA 22222BBBBBBBBBB

5. Can one skip header records load while loading?


Use the "SKIP n" keyword, where n = number of logical rows to skip. Look at this example: LOAD DATA INFILE * INTO TABLE load_positional_data SKIP 5(data1 POSITION(1:5), data2 POSITION(6:15)) BEGINDATA 11111AAAAAAAAAA 22222BBBBBBBBBB

6. Can one modify data as it loads into the database?


Data can be modified as it loads into the Oracle Database. Note that this only applies for the conventional load path and not for direct path loads. LOAD DATA INFILE * INTO TABLE modified_data( rec_no "my_db_sequence.nextval", region CONSTANT '31', time_loaded "to_char(SYSDATE, 'HH24:MI')", data1 POSITION(1:5) ":data1/100", data2 POSITION(6:15) "upper(:data2)", data3 POSITION(16:22)"to_date(:data3, 'YYMMDD')" ) BEGINDATA 11111AAAAAAAAAA991201 22222BBBBBBBBBB990112 LOAD DATA INFILE 'mail_orders.txt' BADFILE 'bad_orders.txt' APPEND INTO TABLE mailing_list FIELDS TERMINATED BY ","(addr, city, state, zipcode, mailing_addr "decode(:mailing_addr, null, :addr, :mailing_addr)", mailing_city "decode(:mailing_city, null, :city, :mailing_city)", mailing_state)

7. Can one load data into multiple tables at once?


Look at the following control file: LOAD DATA INFILE * REPLACE INTO TABLE emp WHEN empno != ' ' ( empno POSITION(1:4) INTEGER EXTERNAL, ename POSITION(6:15) CHAR, deptno POSITION(17:18) CHAR, mgr POSITION(20:23) INTEGER EXTERNAL ) INTO TABLE proj WHEN projno != ' ' ( projno POSITION(25:27) INTEGER EXTERNAL, empno POSITION(1:4) INTEGER EXTERNAL )

8. Can one selectively load only the records that one need?
Look at this example, (01) is the first character, (30:37) are characters 30 to 37: LOAD DATA

INFILE 'mydata.dat' BADFILE 'mydata.bad' DISCARDFILE 'mydata.dis' APPEND INTO TABLE my_selective_tableWHEN (01) <> 'H' and (01) <> 'T' and (30:37) = '19991217'(region CONSTANT '31', service_key POSITION(01:11) INTEGER EXTERNAL, call_b_no POSITION(12:29) CHAR )

9. Can one skip certain columns while loading data?


One cannot use POSTION(x:y) with delimited data. Luckily, from Oracle 8i one can specify FILLER columns. FILLER columns are used to skip columns/fields in the load file, ignoring fields that one does not want. Look at this example: LOAD DATA TRUNCATE INTO TABLE T1 FIELDS TERMINATED BY ',' ( field1, field2 FILLER, field3 )

10. How does one load multi-line records?


One can create one logical record from multiple physical records using one of the following two clauses:

CONCATENATE: - use when SQL*Loader should combine the same number of physical records together to form
one logical record.

CONTINUEIF - use if a condition indicates that multiple records should be treated as one. Eg. by having a '#' character in column 1. 11. How can get SQL*Loader to COMMIT only at the end of the load file?
One cannot, but by setting the ROWS= parameter to a large value, committing can be reduced. Make sure you have big rollback segments ready when you use a high value for ROWS=.

12. Can one improve the performance of SQL*Loader?


A very simple but easily overlooked hint is not to have any indexes and/or constraints (primary key) on your load tables during the load process. This will significantly slow down load times even with ROWS= set to a high value. Add the following option in the command line: DIRECT=TRUE. This will effectively bypass most of the RDBMS processing. However, there are cases when you can't use direct load. Refer to chapter 8 on Oracle server Utilities manual. Turn off database logging by specifying the UNRECOVERABLE option. This option can only be used with direct data loads. Run multiple load jobs concurrently.

13. What is the difference between the conventional and direct path loader?

The conventional path loader essentially loads the data by using standard INSERT statements. The direct path loader (DIRECT=TRUE) bypasses much of the logic involved with that, and loads directly into the Oracle data files. More information about the restrictions of direct path loading can be obtained from the Utilities Users Guide.

You might also like