urgent help : want to check data in oracle from flate file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting urgent help : want to check data in oracle from flate file
# 1  
Old 05-18-2009
urgent help : want to check data in oracle from flate file

Hi All,

I have a flat file like this on unix AIX server:

MXBOFO CSWP 5340 3794499 MXBOBIS CSWP 5340 3581124
MXBOFO CSWP 5340 3794531 MXBOBIS CSWP 5340 3583720
MXBOFO CSWP 5340 3794514 MXBOBIS CSWP 5340 3580763
MXBOFO CSWP 5340 3795578 MXBOBIS CSWP 5340 3794995
MXBOFO CSWP 5340 3710835 MXBOBIS CSWP 5340 3733963
MXBOFO CSWP 5340 3733963 MXBOBIS CSWP 5340 3710835
MXBOFO CSWP 5340 3749157 MXBOBIS CSWP 5340 3749208
MXBOFO CSWP 5340 3749208 MXBOBIS CSWP 5340 3749157
..........

Now i want to write a shell script which will check this rows (values) in a table in database (oracle) and if row exists it will delete the row else do nothing.

please help!!
# 2  
Old 05-18-2009
what have you tried?
# 3  
Old 05-18-2009
Quote:
Originally Posted by unknown123
MXBOFO CSWP 5340 3749157 MXBOBIS CSWP 5340 3749208
MXBOFO CSWP 5340 3749208 MXBOBIS CSWP 5340 3749157
..........

Now i want to write a shell script which will check this rows (values) in a table in database (oracle) and if row exists it will delete the row else do nothing.
You don't have to check if a row exists, just delete it. Oracle will not raise an error if you delete a not existing row.

The query-then-delete approach amounts to the following:
When the row exists, doing a select is just overhead; when the row is not there, you do only a select but then the cost is about the same as doing straigth a delete.

You didn't specify what are the fields you want to use as a key into the table, so I am assuming that the table has a primary key column F4 to be matched with field 4.

Code:
while read f1 f2 f3 f4 f5 f6 f7 f8
do
  sqlplus -s /nolog << SQLEND
  var f4 varchar2
  connect ${user}/${password}@${servicename}
  exec :f4 := '$f4'
  delete TABLE where F4 = :f4;
  commit;
  quit
SQLEND
done

The above code is simple, but it has too much overhead because for every input line it forks a new sqlplus process, which in turn connects to the Oracle database instance and therefore another new process (Oracle server) is forked (unless the Oracle instance is configured as shared server).

Altough a bit more complex, the following approach is better:

Code:
{
echo "var f4 varchar2\nconnect ${user}/${password}@${servicename}"
while read f1 f2 f3 f4 f5 f6 f7 f8
do
  echo "exec :f4 := '$f4'\ndelete TABLE where F4 = :f4;"
done
echo "commit;\nquit"
} | sqlplus -s /nolog

I am using :f4 (bind variable) instead of '$f4' (literal value) inside the delete statement to allow the Oracle optimizer to do less parsing.
Read here for more explanations:
http://asktom.oracle.com/pls/asktom/...:2444907911913
# 4  
Old 05-18-2009
Executing a delete statemet for each row to be deleted leaves no choice to Oracle besides separately locate each single row (hopefully using a suitable index). We are in fact emulating a join method named nested loop.
There is a break even point where the number of rows to delete is large enough (in respect to the number of rows in the table) that an hash join is more efficient. This point is about 1% of the number of rows in the table, more or less depending on the average size of the rows.

To give to the Oracle optimizer the option to use an hash join, we must execute a single delete statement for all the rows to be deleted. This means that the rows from the input file must be loaded into a table (say SOURCE), so that it can be joined to the target TABLE.
We better also tell the Oracle optimizer about the number of rows in both the tables so that it can choose the best join method. This can be accomplished by collecting statistics using a procedure in the system package DBMS_STATS.

Code:
{ echo "var f4 varchar2\nconnect ${user}/${password}@${servicename}"
  echo "create table SOURCE (ID varchar2(99));"
  while read f1 f2 f3 f4 f5 f6 f7 f8
  do
    echo "exec :f4 := '$f4'\ninsert into SOURCE (ID) values (:f4);"
  done
  echo "delete TABLE where F4 in (select ID from SOURCE);"
  echo "drop table SOURCE;\nquit"
} | sqlplus -s /nolog

The commit statement is implied by the drop statement.
There is no performance advantage in doing a commit after every insert (this is just a myth); the converse is true, as a matter of fact every commit forces Oracle to flush the redo log buffer.

If ID is the primary key for the table SOURCE then the delete statement can be formulated differently, and this form most of the times is executed by Oracle using a more efficient plan:
Code:
delete (
  select null -- any value would do
  from TABLE a, SOURCE b
  where a.F4 = b.ID);

If ID is not the primary key for the table SOURCE, Oracle will object to this statement with a rather obscure error message about "non key preserved table". Worse, since F4 is the primary key for TABLE, Oracle will assume that you want to delete from SOURCE instead! You better make sure to have a primary key on ID and to put table TABLE first in the from list. Caveat emptor.
Of course to provide the primary key on ID the create statement above has to be enhanced.
More on this subject: http://asktom.oracle.com/pls/asktom/...:5050838809412

Instead of loading the file into a normal table (heap table), we can create a sort of virtual table whose contents is taken on demand from the external file.
This kind of table is the external table. The SQL code is more involved and some administrative burden is required to make the thing possible, therefore I am omitting a code sample.

Last edited by colemar; 05-18-2009 at 07:36 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Urgent: File Check in remote server

Hi Experts, I need a script in which I need to scp a file /home/chandan/abc.txt to a remote server using scp. Then I need to check whether scp is successful or not. How am I going to write this code? My Server Name: myserver File Name On My Server: /home/chandan/abc.txt Destination Server... (1 Reply)
Discussion started by: ChandanN
1 Replies

2. Shell Programming and Scripting

Data is available or not in a flat file generated by Oracle

Hello, please help me an the below issue. i need to check whether data is available or not in a flat file generated by oracle (sometimes sql didn't any records) to overcome this. without opening flat file. Thanks....... (1 Reply)
Discussion started by: mahesh1987
1 Replies

3. Shell Programming and Scripting

Connection to Oracle data and dump text file

Hi for all! I need for my job one shell script very concrete about connection to oracle databases and writing to text file. Well I tell you: 1.- I must read a file as parameter from linux interface: > example.sh fileToRead 2.-I must read line to line in "fileToRead" and take a... (9 Replies)
Discussion started by: wien996
9 Replies

4. Shell Programming and Scripting

Need Urgent Help from UNIX gurus to get specific data from a file

Hi, I have a file monitor.txt as below... # Times are converted to local time from GMT. # Local Timezone: EST (GMT -05:00) PARAM1 { TIME 30; CC1 "xxxxx"; CC2 "xxxxx"; CC3 "xxxxx"; CC4 "xxxxx"; } PARAM2 { 4061 :... (3 Replies)
Discussion started by: zaq1xsw2
3 Replies

5. Shell Programming and Scripting

urgent<parsing data from a excel file>

Hi all, I wud like to get ur assistance in retrieving lines containing l1.My excel dataset contains around 8000 lines.I converted it into a text tab delimiter file and got the lines containing l1,My output is a list of lines containing l1 saved in a outfile.Some of d lines from my outfile s... (5 Replies)
Discussion started by: sayee
5 Replies

6. Shell Programming and Scripting

Load data from a flat file to oracle.

I have a flat file with records like Header 123 James Williams Finance2000 124 Pete Pete HR 1500 125 PatrickHeather Engg 3000 Footer The structure is: Eno:4 characters Name:8 characters Surname : 9 characters Dept:7 characters Sal:4characters These are sample... (1 Reply)
Discussion started by: Shivdatta
1 Replies

7. Shell Programming and Scripting

Loading Oracle data to unix in flat file

Hi I would like to know how to load oracle data to unix flat file using a shell script. Can we use sqlldr to import data from oracle, if so can anyone help me with it. (2 Replies)
Discussion started by: raghav288
2 Replies

8. Shell Programming and Scripting

load a data from text file into a oracle table

Hi all, I have a data like, 0,R001,2,D this wants to be loaded into a oracle database table. Pl let me know how this has to be done. Thanks in advance (2 Replies)
Discussion started by: raji35
2 Replies

9. Shell Programming and Scripting

urgent-extracting block data from flat file using shell script

Hi, I want to extract block of data from flat file. the data will be like this start of log One two three end of log i want all data between start of log to end of log i.e One two three to be copied to another file. This particular block may appear multiple times in same file. I... (4 Replies)
Discussion started by: shirish_cd
4 Replies
Login or Register to Ask a Question