#!/bin/sh
#GendaTa.sh
echo "Please input the name of the table";
read table_name;
echo "Please input the amount of the data";
read data_amount;
sqlplus -s vpn123/vpn123@vpndb<<!
#set heading off;
set feedback;
set pagesize 0;
set trimspool on;
spool data_lj;
desc $table_name;
spool off;
!
##########################分割文件##################
sed '1d' data_lj.lst > data.lj;
sed '/-/d' data.lj > data_lj ;
tr -s " "<data_lj > datalj; #删除空格
awk '{print $2}' datalj>datalj.out
########################分割完成####################
########################确定数据类型###############
read datalj.out
case $datalj.out
^NUMBER)
###################循环插入数据####################
sqlplus -s vpn123/vpn123@vpndb<<!
for
insert into $table_name values ();
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ljamie/archive/2009/07/23/4374576.aspx
#获取表的列数,也可用cat file.txt | wc -l,不过得到的结果要减2
lineCount=0
while read line
do
if [ -z "$line" ]
then
break
else
lineCount = `expr lineCount + 1`
fi
done << file.txt
#构造SQL语句保存到tmp.sql中,wantedInsert为脚本传入参数 ,代表准备插入记录的数量,
#TableName也为脚本传入参数,代表要操作的表名
i=0
while [ $i -lt $wantedInsert ]
do
j = 0;
while [ $j -lt $lineCount ]
do
echo "insert into $TableName values(" >> tmp.sql
case columnType in
NUMBER)
data = i
echo "$data" >> tmp.sql
;;
VARCHAR)
data = i
echo "/'$data/'" >> tmp.sql
;;
esac
j = `expr j + 1`
if [ $j -ne $lineCount ]
then
echo "," >> tmp.sql
fi
done
echo ");"
i = `expr i + 1`
done
#在SQLPLUS环境下运行生成的tmp.sql文件
sqlplus -s vpn123/vpn123@vpndb<<!
@tmp.sql
!
disc