2012-11-05 50 views
1

我想一個ksh的殼變量傳遞爲一個整數,以在sqlplus或轉換可變使用它。這之前數爲i寫的代碼(不工作):傳遞一個數字值以在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似乎並沒有被working.passing變量爲sqlplus,很容易和很多問題提到它,但我特別想傳遞一個整數值和你它它

編輯: 我使用oracle 10g db。

回答

0

問題通過使用此代碼的字符轉換爲整數的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; 
/

做,如果你找到更好的替代品出來。