如何将对象创建假脱机到日志文件中

时间:2016-03-29 20:23:12

标签: sql oracle shell

我创建了一个shell脚本,其中我正在执行包含所有create和alter table脚本的Alter_t_new.sql。现在我想要出错或创建的任何错误都应该假脱机到日志文件中。我尝试了SPOOL $ LOGFILE,但这是一个错误

   SPOOL: command not found

shell脚本:

echo "Starting installation of XXHCM_OBJECTS - XXHCM_OBJECTS ..."
echo "Starting installation of XXHCM_OBJECTS - XXHCM_OBJECTS ..."    "">>$LOGFILE
echo   "">>$LOGFILE

echo   "">>$LOGFILE
echo "Copying Files To Appropriate Directories ..."
echo "Copying Files To Appropriate Directories ..."  "">>$LOGFILE


echo   "">>$LOGFILE


echo   "">>$LOGFILE
echo "Files copied successfully !!! "
echo "Files copied successfully !!! "    "">>$LOGFILE
echo   "">>$LOGFILE

echo "Changing permissions ... "     "">>$LOGFILE
chmod 777 *.*
echo "Changed permissions ... "     "">>$LOGFILE

echo    "">>$LOGFILE
echo "Creating Custom Package, Synonyms and Grants..."   "">>$LOGFILE
echo "Creating Custom Package, Synonyms and Grants..."
echo    "">>$LOGFILE

if sqlplus $APPS_USER @Alter_t_new.sql
then
SPOOL $LOGFILE
    echo "Custom itables created successfully in APPS schema"     "">>$LOGFILE
    echo "Custom itables created successfully in APPS schema" 
     SPOOL OFF
else
    echo "Error in creating custom  itables in APPS schema"     "">>$LOGFILE
    echo "Error in creating Custom itables  in APPS schema" 
fi

echo    "">>$LOGFILE
echo "Package, Synonyms,grants and Tables created successfully"   "">>$LOGFILE
echo "Package, Synonyms,grants and Tables created successfully"
echo  "">>$LOGFILE


echo "Installation completed for XXHCM_OBJECTS - XXHCM_OBJECTS"
echo "Installation completed for XXHCM_OBJECTS - XXHCM_OBJECTS"     "">>$LOGFILE
# *****************************************************************************
# End of Script
# *****************************************************************************

2 个答案:

答案 0 :(得分:1)

我相信SPOOL需要在Alter_t_new.sql文件中。 我会做这样的事情:

    (from .sh script)
    if sqlplus $APPS_USER @Alter_t_new.sql $LOGFILE
    then
        echo "Custom itables created successfully in APPS schema"     "">>$LOGFILE
        echo "Custom itables created successfully in APPS schema" 
         SPOOL OFF
    else
        echo "Error in creating custom  itables in APPS schema"     "">>$LOGFILE
        echo "Error in creating Custom itables  in APPS schema" 
    fi


    (from Alter_t_new.sql)
    WHENEVER SQLERROR EXIT SQL.SQLCODE

    spool &1 APPEND

将$ LOGFILE作为参数传递给sqlplus命令

sqlplus $APPS_USER @Alter_t_new.sql $LOGFILE

并使用该参数附加到您要添加到

的日志文件中
spool &1 APPEND

此外,WHENEVER SQLERROR EXIT SQL.SQLCODE位应该将错误返回给调用它的.sh。以下是WHENEVER SQLERROR的一些信息:https://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12052.htm

我希望这会有所帮助。

答案 1 :(得分:0)

您必须在脚本中使用SPOOL my_log.log CREATE TABLE t1 (col1 NUMBER); SPOOL OFF 命令,它是SQLPlus命令。例如,文件内容是:

SET TERMOUT OFF

您也可以使用ApplyPropertyChange命令来抑制输出。

  

TERMOUT设置控制SQL Plus是否显示由SQL语句,PL / SQL块和SQL Plus命令生成的输出。仅当SQL Plus正在执行脚本文件时,此设置才适用。 SQL Plus显示交互式输入命令的输出。