使用shell脚本执行sql语句:
sqlplus classpath系统环境变量预配置、Oracle tnsnames.ora配置
1、配置sql服务器环境:tnsnames.ora: (../product/version/Client/network/admin)
在tnsname.ora文件中,配置服务器映射:
ENV_DESTINATION =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = hostname)(PORT = port))
)
(CONNECT_DATA =
(SERVICE_NAME = server)
)
)
例:
sqlplus user/pwd@ENV_DESTINATION@%1\test.sql
@参数
%1 第一个命令行参数
2.copy命令:迁移将SQL查询出的结果作为数据源插入当前数据库
COPY FROM user/pwd@ENV_SOURCE INSERT schema1.table1 USING SELECT * FROM schema2.table2;
3.spool命令:export导出数据到指定的目录下C:\test.txt ,字段分割规则'<EOFD>'
spool C:\test.txt
select TRIM(ID)||'<EOFD>'|| TRIM(ACTION_TYPE)
FROM schema.tablename;
4.sqlldr命令:读取3中导出的数据作为数据源,依据相同的字段分割规则'<EOFD>' ,执行
4.1 预定义规则test.ctl 文件:
load data
infile 'C:\test.txt '
INSERT
INTO TABLE schem2.tablename2
FIELDS TERMINATED BY '<EOFD>'
trailing nullcols
(ID,NAME char(1000))
sqlldr user/pwd@ENV_DESTINATION control=%1\test.ctl log=%1\log1.log bad=%1\bad1.log rows=500 parallel=true
另:sql注释符号:-- bat注释符号 rem