在 Unix 脚本中使用存储过程 OUT 参数

Use Stored Procedure OUT Parameter in Unix Script

我有一个直接从数据库中选择计数的 ksh 脚本。我正在用存储过程调用替换它。我的存储过程选择了一个 OUT 变量。

如何将 OUT 值捕获到脚本中声明的数据文件中?

    bteq <<EOF
.RUN FILE $DBENV/1logon.txt
.export data file=$PMRootDir/$FOLDER_NME/SessLogs/pdbasis_count.txt
.set recordmode OFF;
.set sidetitles OFF;
.set titledashes off;

#This Select Statement is being replaced by the Stored Procedure Call
SELECT trim(COUNT(*))
FROM [Removed]
WHERE [Removed]

.IF ERRORCODE <> 0 THEN .QUIT 11;

.export reset;
.logoff;
.quit   

EOF

你可以试试这个。 lv_status 应该给你输出参数。

            ##Procedure Execution starts

         procname=$your_user_name."your_proc_name"
        echo $procname
        echo $procname |grep "("
        if [ $? -eq 0 ]
        then
            procname=`echo $procname|cut -f1 -d")"`",lv_status)";
        else
            procname=`echo $procname`"(lv_status)";
        fi
        echo "Executing $procname"

        bteq <<EOF
        .logon $tdpid/db_user_name,db_password
        .EXPORT FILE=$tempfile
        .SET WIDTH 3000
        .SET TITLEDASHES OFF;
        CALL $procname;
        .EXPORT RESET;
        .LOGOFF
        .EXIT
        EOF

        exit_status=$?
        lv_status=`tail +2 $tempfile`

        rm -f $tempfile

        if [ $lv_status ] 
        then
            if [ $lv_status = "FAILED" -o $exit_status -ne 0 ]
            then
                echo "Error in executing procedure $procname";
                exit 1;
            fi
        else
            echo "The procedure does not have the mandatory OUT status parameter. Consider remodeling the code for full error trapping"
            exit 1
        fi

        echo "Procedure $procname completed successfully"