将数值传递给sqlplus

时间:2012-11-05 01:46:58

标签: sql shell sqlplus ksh

我想将一个ksh shell变量作为整数传递给sqlplus,或者在使用之前将变量转换为数字。这是我编写的代码(不工作):

echo
echo "Enter the day of run (1,2,3,..,N ) :" | tee -a $sqllog
read day

typeset -i first_range

if [ $day -eq 1 ]; then
first_range=5000
elif [[ $day -eq 2 ]]; then
first_range=105000
elif [[ $day -eq 3 ]]; then
first_range=205000
elif [[ $day -eq 4 ]]; then
first_range=305000
elif [[ $day -eq 5 ]]; then
first_range=405000
elif [[ $day -eq 6 ]]; then
first_range=505000
elif [[ $day -eq 7 ]]; then
first_range=605000
fi

echo "first_range value : ${first_range}" | tee -a $sqllog
{
sqlplus -S ${SF_PSTN_CONN_STRING} << EOF

alter session enable parallel DML;
--alter session set  db_file_multiblock_read_count=128;
alter session enable parallel query;

set heading off
set verify off
set echo off
set pages 0
set timi on
set lines 300
set trimspool on

# Drop table TMP_DAILY_PSTN_CMP_11035

drop table TMP_DAILY_PSTN_CMP_11035;


# Create TMP Table TMP_DAILY_PSTN_CMP_11035 from TMP_OA_PSTN_CMP_11035

Create table TMP_DAILY_PSTN_CMP_11035 (
        ACTION_OBJID NUMBER,
        CONTRACT_OBJID NUMBER,
        STATUS varchar2(40),
        UPDATE_DATE DATE
)
partition by range(ACTION_OBJID)(
partition p1 values less than (${first_range})) NOLOGGING parallel;

prompt the day of run is $day;

begin
        for k in 2..200
        loop
                execute immediate
                        'alter table TMP_DAILY_PSTN_CMP_11035 add partition p'||k||' values less than ('||${first_range}+(k*5000)||')';
        end loop;
end;
/

我得到的错误是:

Enter the day of run (1,2,3,..,N ) :
2
first_range value : 105000

Session altered.


Session altered.


Table dropped.

Elapsed: 00:00:00.03

Table created.

Elapsed: 00:00:00.02
the day of run is 2
begin
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 4

我如何解决这个问题,即将char转换为数字,甚至typeset -i似乎都没有工作。将变量传递给sqlplus很容易,很多问题都引用它,但我特别想通过一个整数值并使用它

编辑: 我正在使用oracle 10g db。

1 个答案:

答案 0 :(得分:0)

通过使用此代码将char转换为sql中的整数来解决问题:

VARIABLE ret_val NUMBER
:ret_val := 1;
PRINT ret_val
begin
                                for k in 2..200
                                loop
                                   SELECT (select to_number(${first_range}) from dual)+5000*(k-1) INTO :ret_val FROM dual;
                execute immediate
                        'alter table TMP_DAILY_PSTN_CMP_11035 add partition p'||k||' values less than ('||:ret_val||')';
        end loop;
end;
/

告诉你是否找到了更好的选择。