Visit Our UNIX and Linux User Community


Script does not execute Insert Statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script does not execute Insert Statement
# 1  
Old 08-24-2006
Script does not execute Insert Statement

Hi

I have a text file , contents are
Line1:field1,field2,field3,field4,field5,field6.......field20
Line2:field1,field2,field3,field4,field5,field6.......field20
Line3:field1,field2,field3,field4,field5,field6.......field20
....and so on...

I want to read this file and insert the data into table.

How can I do that...can anybody help ?
I tried to read the text file , each line, cut the fields, make connection to database and then execute the Insert Statement....
But when I do this the Insert statement does not execute.
Can anybody guide me how do I achieve this ?

Code:
#####Connecting sqlplus to check the connectivity
sqlplus -s /nolog <<EOF>/dev/null
connect ${DB_LOGIN}/${DB_PASSWORD}@${DB_NAME}

cat /$FILEPATH/$FILENAME | while read LINE
do
    echo ${LINE}
    acctno=`echo $LINE | cut -f1 -d','`
    echo $acctno
    salutation=`echo $LINE | cut -f2 -d','`
    name=`echo $LINE | cut -f3 -d','`
    billdate=`echo $LINE | cut -f4 -d','`
    totaldue=`echo $LINE | cut -f5 -d','`
    billdeductiondate=`echo $LINE | cut -f6 -d','`
    billduedate=`echo $LINE | cut -f7 -d','`
    templatecode=`echo $LINE | cut -f8 -d','`
    billid=`echo $LINE | cut -f9 -d','`
    emailid=`echo $LINE | cut -f10 -d','`
    brnno=`echo $LINE | cut -f11 -d','`
    billsummaryid=`echo $LINE | cut -f12 -d','`
    batchno=`echo $LINE | cut -f13 -d','`
    emailfile=`echo $LINE | cut -f16 -d','`
    echo "Insert into table"

    spool ${SYS_TEMP_DIR}/${JOB_ID}_db_sql.log
    INSERT INTO SendMailDetails (acctno) values ('${acctno}');
    echo "Table Appended"
    commit;
    spool off;
    exit;
    EOF
done

I also tried:

Code:
MYCOMMAND=`awk '$1' ${FILEPATH}${FILENAME}`
sqlplus -s /nolog <<EOF>/dev/null
connect ${DB_LOGIN}/${DB_PASSWORD}@${DB_NAME}

spool my.log
Insert into SendMailDetails(acctno) values($MYCOMMAND);
spool off
commit;
exit
EOF;

But am unsuccessfull, Please help.....
# 2  
Old 08-24-2006
I think its pretty easy to use sql loader for your purpose.
# 3  
Old 08-24-2006
Thanks Mukund

Atleast I got a direction...

Thanks
Amruta
# 4  
Old 08-24-2006
Amruta, you could use the shell to do this quite easily:
Code:
#####Connecting sqlplus to check the connectivity
sqlplus -s /nolog <<EOF>/dev/null
connect ${DB_LOGIN}/${DB_PASSWORD}@${DB_NAME}
oldIFS=$IFS
IFS=,
while read acctno salutation name billdate totaldue billdeductiondate billduedate templatecode billid emailid brnno billsummaryid batchno field14 field15 emailfile rest_of_the_fields; do
    echo "Insert into table"
# insert into whatever table, whichever values that you want
# after you are done, go ahead
    echo "Table Appended"
    commit;
    spool off;
    exit;
    EOF
done

field14, field15: hold the 14th and 15th fields, rest_of_the_fields: holds the fields from 17 to end of line. Everything else goes into the appropriately named variables.
# 5  
Old 08-25-2006
MySQL SQL Loader to Append Data

Hi there
I worked on Mukund's suggestion to use sqlloader...it worked successfully.
this is the main shellscript
LoadMain.sh

Code:
#!/usr/bin/ksh

DB_LOGIN=SOMESYSTEM
DB_PASSWORD=SOMEPASSWD
DB_NAME=SOMENAME
FILENAME="$1"

sqlldr ${DB_LOGIN}/${DB_PASSWORD}@${DB_NAME} control=tryloader log=mlist2.log data=mlist2.txt

echo "I am done"

-------------
data=mlist.txt is the Input text file which contains data with delimiters.
control=tryloader.ctl is the Control file which contains information as to how the data will be appended to the table...
My tryloader.ctl contains :
Code:
LOAD DATA
APPEND INTO TABLE SendMailDetails
(
  AcctNo CHAR TERMINATED BY ",",
  Salutation CHAR TERMINATED BY ",",
  Name CHAR TERMINATED BY ",",
  BillDate CHAR TERMINATED BY ",",
  TotalDue DECIMAL EXTERNAL TERMINATED BY ",",

 ...and other fields
)

And again..thanks Guys....

Amruta Pitkar

Previous Thread | Next Thread
Test Your Knowledge in Computers #249
Difficulty: Easy
Senator Albert Gore, Jr. authored the High Performance Computing and Communication Act of 1991, creating what Gore referred to as the information superhighway.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. Shell Programming and Scripting

How to pass tablenames from a file to shell script to execute create statement in DB2

Hi, I am new to Shell Scripting, and I need to create nicknames for 600 tables in db2. I have the file names in a text file and i have to pass these table names to a shell script create nicknames in db2. Can some one please help me in this regard. (1 Reply)
Discussion started by: kamalanaatha
1 Replies

3. Shell Programming and Scripting

How to insert date in a statement?

Hi Guys, Can somebody help me in inserting today's DATE format (20110709) in my awk statement. I have a script but its not working. inputfile.txt: 269,1,0,AAA,430 231,2,0,BBB,430 252,3,0,CCC,430 214,4,0,DDD,430 script.sh #!/bin/bash DATE="`date +%Y%m%d`" cd /var/opt/ (8 Replies)
Discussion started by: pinpe
8 Replies

4. Shell Programming and Scripting

How to insert numbers to a in between statement

Hi Guys, I want to create a shell script that will give me the output below. I want to insert the numbers from the input file to my url addresses below. And from the numbers below, I want to separate the last digit with a period (i.e. from 222222222222 to 22222222222.2). Appreciate any help.... (14 Replies)
Discussion started by: pinpe
14 Replies

5. Programming

Dynamic Insert statement

I have a form , where i will put the values to a table. I wrote a insert statement for the same. Table structure is ename | character varying(30) | eadd | character varying(30) | eid | integer | sal | integer In the statements, i don't... (1 Reply)
Discussion started by: pritish.sas
1 Replies

6. Shell Programming and Scripting

error in insert statement

hi, When i try to run the code below, i get the following error "ksh: syntax error: `(' unexpected" i am not able to figure it out. Can anyone help me? Code: (2 Replies)
Discussion started by: ragavhere
2 Replies

7. AIX

Controling a statement to execute

Hi All I have a script which runs a piece of JOB. The jobs are in sequence and if it fails at a particular job I wanted it to be started from the point where it failed. What I did I prepared two properties file one which contains the entire List of the JOBS which are to be executed and the... (5 Replies)
Discussion started by: Prashantckc
5 Replies

8. Shell Programming and Scripting

How is use sselect statement o/p in insert statement.

Hi All, I am using Unix ksh script. I need to insert values to a table using the o/p from a slelect statement. Can anybody Help! My script looks like tihs. ---`sqlplus -s username/password@SID << EOF set heading off set feedback off set pages 0 insert into ${TB_NAME}_D... (2 Replies)
Discussion started by: nkosaraju
2 Replies

9. Shell Programming and Scripting

Need to execute the same statement

I have written a script for converting an IP address into its corresponding AS number in PHP. But based on the timing analysis, I've observed that it takes a long time to process large number of entries. So I need to do something directly in unix. What method would one suggest for this purpose? ... (8 Replies)
Discussion started by: Legend986
8 Replies

10. Shell Programming and Scripting

How do i execute in IF ELSE Statement

ls -ld /path/to/dir1 path/to/dir2 | awk '{print $8}' how to execute above script in IF ELSE Statement. Pls iam new to Unix (1 Reply)
Discussion started by: laknar
1 Replies

Featured Tech Videos