【发布时间】:2014-01-28 10:09:51
【问题描述】:
我有一个 bash 脚本 (load_data.sh),它调用 sqlldr 命令将数据从 .csv 加载到表 (data_import) 中。在最近的一次调用中,我注意到即使命令执行完成,该表也不包含 .csv 文件中的数据。我这样说是因为 bash 脚本中的后续语句 (process_data.sh) 试图运行引发错误的存储过程ORA-01403: no data found。
我learned 提交在文件加载后立即发生。所以,我想知道是什么导致了这个错误,以及我将来如何避免它。
这是我的脚本:
load_data.sh
#!/usr/bin/bash -p
# code here #
if [[ -f .st_running ]]
then
echo "Exiting as looks like another instance of script is running"
exit
fi
touch .st_running
# ... #
# deletes existing data in the table
./clean.sh
sqlldr user/pwd@host skip=1 control=$CUR_CTL.final data=$fpath log=${DATA}_data.log rows=10000 direct=true errors=999
# accesses the newly loaded data in the table and processes it
./process_data.sh
rm -f .st_running
clean.sh/process_data.sh
# code here #
# ... #
sqlplus user/pwd@host <<EOF
set serveroutput on
begin
schema.STORED_PROC;
commit;
end;
/
exit;
EOF
# code here #
# ... #
STORED_PROC 由 process_data.sh 运行:
SELECT count(*) INTO l_num_to_import FROM data_import;
IF (l_num_to_import = 0) THEN RETURN;
END IF;
/* the error (`ORA-01403: no data found`) happens at this statement: */
SELECT upper(name) INTO name FROM data_import WHERE ROWNUM = 1;
控制文件
LOAD DATA
APPEND
INTO TABLE DATA_IMPORT
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
...
...
)
编辑
- 输入文件有 8 行,两次运行的日志都表明成功插入了 8 行。
- 有趣的行为:我第二次在同一个文件上运行该脚本时运行良好(没有抱怨错误)。因此,在第一次运行期间,sqlldr 命令似乎在执行下一个 sqlplus 命令之前没有完成。
【问题讨论】:
-
输入文件是空的吗?
-
sqlldr看起来是否已完成 - 您是否检查了日志以查看它是否运行,以及它插入/拒绝了多少行?您目前没有检查退出代码,这很有用。 -
@BobJarvis 不,输入文件有 8 行。
-
好的,然后回到 Alex 的问题 - 日志文件必须说什么?编辑问题并包含数据文件和 sqlldr 控制文件可能很有用。
-
@user640378 - this might be of interest.
标签: sql oracle bash sqlplus sql-loader