Assign variable for INSERT INTO statement


 
Thread Tools Search this Thread
Top Forums Programming Assign variable for INSERT INTO statement
# 8  
Old 10-20-2010
I am processing input file data, I am checking the 7th field of input file. If its "1" then taking data from that line and inserting it into database table.Here is my script,
Code:
#!/bin/sh
usage="`basename $0` <file_name>"

Insert_Record()
{
set -x
INS_REC=$(sqlplus $USERID/$PASSWORD@$DATABASE << EOF 
set head off 
set feed off 
set serveroutput on
declare
v_cnt NUMBER:=0;
begin
select count (*) into v_cnt from TBL1;
if v_cnt >= 0 then

INSERT INTO TBL1 ( EMPNAME,EMPID,EMPBDATE)
VALUES('$EMPNAME','$EMPID',TO_DATE('$EMPBDATE','YYYY-MM-DD'));
end if;
end;
/
EOF)

TBL_ERR=$(echo "$INS_REC" | grep -e "SP2-" -e "ORA-")
echo $TBL_ERR
}

process_input_record ()
{
echo "Input file path is $INPUT_FILE_FOLDER"
if [ "$(ls -A $INPUT_FILE_FOLDER)" ]; then
#echo "Take action $INPUT_FILE_FOLDER is not Empty"
for file in "$INPUT_FILE_FOLDER/"ABC_MN_XYZ*.CSV
do	
# Did lots of things for input file data

# Check each line 
line=`head -1 $file`
nrfields=$(IFS=\;; set -- $line; echo $#)
if [ $nrfields -ne 4 ]; then
echo "Incorrect first record fields"
exit
else			
DATE=`echo $line | cut -d ";" -f 2`						
DATEGEN==`echo $DATE | cut -c1-8`			
while read line
do
fields=$(IFS=\;; set -- $line ; echo $#)
Status=`echo $line | cut -d ";" -f 7`
if [ $fields -eq 12 ]; then
if [ "$Status" -eq 1 ]; then
EMPNAME=`echo $line | cut -d ";" -f 2`
EMPID=`echo $line | cut -d ";" -f 4`
Insert_Record

sed "/$line/d" $file > tmp
mv tmp $file
fi
else
echo "Incorrect DETAIL record fields"
fi
done < $file
fi
else
echo "$INPUT_FILE_FOLDER is Empty"
fi
done
}

#----------------------------------------------------------------------------
# Main
#----------------------------------------------------------------------------

if [ $# -eq 1 ]; then

if [ -f $1 ]; then
. $1  
process_input_record

else 
echo "$ERROR_CODE : file $1 is not available"
exit 1   
fi 
else
echo "usage : $usage"
fi

Please help me out to remove the error while inserting record into table.
Thanks in advance.

Last edited by Poonamol; 10-20-2010 at 12:42 AM.. Reason: indenting
# 9  
Old 10-20-2010
The script looks okay. Change the set command to "set -vx", move it to right below the shebang, execute the script and paste the command + the output + the diagnostic messages over here.

Also, this chunk of code -

Code:
select count (*) into v_cnt from TBL1;
if v_cnt >= 0 then

is unnecessary. Your table is the infrastructure; and you don't check for infrastructure exceptions. If it is missing, then you have a much bigger problem.

tyler_durden
# 10  
Old 10-20-2010
Thanks for the reply and your time.
I got the problem,
Code:
DATE=`echo $line | cut -d ";" -f 2`
echo "Date is : $DATE"			
DATEGEN==`echo $DATE | cut -c1-8`
echo "Generated Date is : $DATEGEN"

and here is the output,
Code:
Date is : 20100913115432
Generated Date is : =20100913

I am getting incorrect value as (=20100913) in DATEGEN variable, hence the error message.
How can I remove it. Please help me out.

---------- Post updated at 01:37 AM ---------- Previous update was at 12:40 AM ----------

Still I am facing Column not allowed here error message from oracle database.
Here is the output,
Code:
Date is : 20100913115432
Migration Date is : 20100913
Header record is correct
Incorrect DETAIL record fields
Incorrect DETAIL record fields
+ + sqlplus bios/bios@BIOS
+ 0<<
set head off
set feed off
set serveroutput on

INSERT INTO TBL1 ( EMPNAME,EMPID,EMPBDATE)
VALUES($EMPNAME,$EMPID,TO_DATE($EMPBDATE,'YYYY-MM-DD'));
/
INS_REC=
SQL*Plus: Release 9.2.0.5.0 - Production on Wed Oct 20 08:33:00 2010

Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.


Connected to:
Oracle9i Enterprise Edition Release 9.2.0.5.0 - 64bit Production
With the Partitioning option
JServer Release 9.2.0.5.0 - Production

SQL> SQL> SQL> SQL> SQL>   2    3    4    5    6    7    8    9                                 TO_DATE (20100913,"YYYY-MM-DD")
                                                    *
ERROR at line 8:
ORA-00984: column not allowed here


SQL>                            TO_DATE (20100913,"YYYY-MM-DD")
                                                    *
ERROR at line 8:
ORA-00984: column not allowed here


SQL> Disconnected from Oracle9i Enterprise Edition Release 9.2.0.5.0 - 64bit Production
With the Partitioning option
JServer Release 9.2.0.5.0 - Production
+ + grep -e SP2- -e ORA-
+ echo
SQL*Plus: Release 9.2.0.5.0 - Production on Wed Oct 20 08:33:00 2010

Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.

Please help me out.
# 11  
Old 10-20-2010
There is a bug in ksh process substitution. Change
Code:
$(...)

to
Code:
`...`

although the former is preferred.
# 12  
Old 10-20-2010
If you will not take my advice to get off <<, you can at least change to:
Code:
tee /tmp/mySQL <<! | sqlplus . . .

so you know what your input looks like, for debugging.
# 13  
Old 10-21-2010
Thanks for reply,

Ichanged the code as,
Code:
INS_REC=$(tee /tmp/mySQL <<! |sqlplus $USERID/$PASSWORD@$DATABASE << EOF 
set head off 
set feed off 
set serveroutput on
INSERT INTO....
/
EOF)

But the record is not getting inserted into the table.
Could you please provide correct way to do it. Please reply back with Inser_Record() whole function as your sugession.
I already provided most of the code of my script.
Thanks in advance.

Last edited by Poonamol; 10-21-2010 at 12:39 AM..
# 14  
Old 10-21-2010
Quote:
Originally Posted by durden_tyler
Finally, Neo's suggestion is noteworthy. Small Oracle projects (such as yours apparently) that deal mainly with strings or "string-like" data may be fine with Unix shells. But as the project increases in scope and volume, and as you keep on adding more and more complex datatypes - LOBs, XML, timestamps, Collections - nested tables, arrays etc. the use of a wrapper API like Perl DBI or PHP or JDBC will make your life easier. They are very robust, well tested and bind quite tightly with complex Oracle types.
Yes, I don't understand why a poster says "My project must be in KSH!" UNLESS it is homework or classwork.

As durden_tyler concurs, there are better ways to work with DB applications than command line shell languages.

I greatly enjoy reading all the creative solutions and debugging, but the fact of the matter is that using a more suitable API to the database, such as PHP or Perl libs/wrappers are best for queries, debugging, array management, row and field selection, etc.

That is why I don't understand why the solution "must be in KSH" unless the project is academic in nature.

Poonamol, why does this project "have to be in KSH"?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. 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

3. Shell Programming and Scripting

How to insert a space and assign the value to another variable?

Hello, I need to insert a space between 2 strings. I used many techniques and all of them worked but when I assign the value to another variable then the inserted space vanishes, strange! Please advise. # dat=`date |awk '{print $2,$3}'` # echo $dat Nov 3 The above is perfectly fine. Now... (4 Replies)
Discussion started by: prvnrk
4 Replies

4. 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

5. 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

6. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

7. Shell Programming and Scripting

How can i assign an select statement into a variable?

I am trying to assign an select statement into a variable. Can someone hel me with this. example : a='select * from dual' echo $a should give me select * from dual But this is not working. I trying with \ before * and quotes too. (1 Reply)
Discussion started by: rdhanek
1 Replies

8. 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

9. 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

10. 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
Login or Register to Ask a Question