批处理实现从Excel导入Oracle

之前做过一个从Oracle导出到Excel的批处理,简单点说就是通过Sqlplus将表数据Spool到一个文本文件中或简化的Excel格式文档中,而这里我要做的事恰好相反,工具则是使用sqlldr, sqlldr的用法没有研究太多,这里主要用写写如何使用批处理来调用实现导入的自动化过程。

主要工作涉及到以下几块:

1. 将Excel转换成csv格式的文件,这里要借助工具xls2csv.exe实现excel到csv的转换;

xls2csv的用法: xls2csv excelfile.xls gbk

以上命令执行后,程序会将excel文件中的每一张sheet都生成一个csv文件,并以excel文件的名字_sheet名字.csv命名

2. 使用Sqlldr将数据导入Oracle,

Sqlldr的用法:sqlldr user/pass@tnsname control=example.ctl data=data.csv

以上命令执行后,sqlldr会将data.txt数据文件中数据按照control指定的规则插入到数据库中,具体的插入规则和表名都在control文件中指定。

Control文件写法:

load data
infile 'data.csv'
append
into table t_tab_data
fields terminated by ';' optionally enclosed by '"'
(
filed1,
filed2,
filed3
)

以上语法文件中指定了从文件data.csv中导数据,导入到表t_tab_data中,

fields terminated by ';' optionally enclosed by '"' 表示数据文件中字段以;分隔,各字段中的"忽略掉,这些规则也可以写到各字段后,分别对各字段进行约束。

另外,Control文件中的infile ‘data.csv’也可以写成infile ‘*', 这样data.csv中的数据可以直接放到control文件的最后,sqlldr会在读完control文件的规则部分后,再去读取数据部分导入数据库。

3. 将上面要做的工作写成批处理

@Echo Off
Color 0A
Title Network Config Assistant By Eric
Pushd %CD%
Prompt $G
MODE CON COLS=100 LINES=30
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
Set OraUserPass=icd/icd@Eric

Echo 1. Start importing agent list to database...
Echo ========================================================

Set /P xlsfilename=Agent List in Excel format(agents.xls as default):
IF /I "%xlsfilename%"=="" Set xlsfilename=agents.xls

Echo Generating csv file for agent list from excel...
start /min /wait xls2csv.exe %xlsfilename% gbk

FOR /F %%i in ('DIR /B *.CSV /O:-D') DO (
    SET FILENAME=%%i
    GOTO :EXITFOR
)

:EXITFOR
Echo Generate csv file "%FILENAME%" successfully.

Echo\

Set tmpAgentTab=t_test_agent

REM ==================Check table for Agent Configed in IEX=================
Echo Start checking table !tmpAgentTab!...
Echo select * from !tmpAgentTab! where 1^<^>1;>%temp%\tmpCheckTab.sql
Echo exit;>>%temp%\tmpCheckTab.sql
Sqlplus %OraUserPass% @"%temp%\tmpCheckTab.sql" | find /I "ERROR">NUL && IF errorlevel 0  (
    Echo Table !tmpAgentTab! is not exist.
    CALL :CreateAgentTable
    Goto :AgentClear
)

REM ==================Truncate table t_test_agent================
Echo Table !tmpAgentTab! is already exist.
Echo\

Echo Truncating table !tmpAgentTab!...
Echo Truncate table !tmpAgentTab!;>%temp%\tmpTruncate.sql
Echo Exit>>%temp%\tmpTruncate.sql
Sqlplus %OraUserPass% @"%temp%\tmpTruncate.sql" 1>NUL
Echo Truncate table !tmpAgentTab! finished.

:AgentClear
Echo\
REM ==================Import data from list to db=================
REM ----Generate control file----
Set tmpAgentCtrlFile="%CD%\agent.ctl"

REM Echo load data>%tmpAgentCtrlFile%
REM Echo infile '%FILENAME%'>>!tmpAgentCtrlFile!
REM Echo append>>!tmpAgentCtrlFile!
REM Echo into table icd.t_test_agent>>!tmpAgentCtrlFile!
REM Echo fields terminated by ';' optionally enclosed by '^"'>>!tmpAgentCtrlFile!
REM Echo (>>!tmpAgentCtrlFile!
REM Echo agentid,>>!tmpAgentCtrlFile!
REM Echo agentname,>>!tmpAgentCtrlFile!
REM Echo agentskill>>!tmpAgentCtrlFile!
REM Echo ^)>>!tmpAgentCtrlFile!

Echo Importing data to table !tmpAgentTab!...
sqlldr %OraUserPass% control=agent.ctl 1>NUL
Echo Importing data to table !tmpAgentTab! finished.

Echo\

REM ============Create table t_test_agent================
:CreateAgentTable
IF /I "%0"==":CreateAgentTable" (
Echo Start creating table !tmpAgentTab!...
Echo Create table t_test_agent ^(>%temp%\tmpAgentTab.sql
Echo agentid number(5^),>>%temp%\tmpAgentTab.sql
Echo agentname varchar2(50^),>>%temp%\tmpAgentTab.sql
Echo agentskill varchar2(2000^)>>%temp%\tmpAgentTab.sql
Echo ^);>>%temp%\tmpAgentTab.sql
Echo Exit>>%temp%\tmpAgentTab.sql

Sqlplus %OraUserPass% @"%temp%\tmpAgentTab.sql" 1>NUL
Echo Create table !tmpAgentTab! successfully.
Echo\
Goto :EOF
)

Echo 2. Export data report of discrepancy...
Echo ========================================================
Echo Exporting report from database, please wait...
Start /WAIT ExportBySql.bat %OraUserPass% report.xls
Echo Export report from database finished.
Echo\
Echo\

:EXIT
Pause

 

你可能感兴趣的:(oracle,职场,数据处理,休闲)