1

我想在使用之前将 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

我该如何解决这个问题,即将字符转换为数字,甚至typeset -i似乎不起作用。将变量传递给 sqlplus 很容易,很多问题都涉及到它,但我特别想传递一个整数值并使用它

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

4

1 回答 1

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;
/

一定要告诉你是否找到更好的选择。

于 2012-11-05T04:19:03.567 回答