将分号分隔数据传递给函数

时间:2012-08-15 12:15:10

标签: oracle stored-procedures plsql mybatis stringtokenizer

我想修改以下程序,以1; 2; 3的形式发送数据。在下面的程序中,我发送的数据如1,然后我检查数据是否是有效数字,如果是,那么我得到一些查询表并获取数据计数。我无法修改发送分号分隔数据的过程并检查数据是否为数字,这里p_in_field_value应该像1; 2; 3应该传递给下面的函数。

FUNCTION p_get_option_value(p_in_field_value IN VARCHAR2) RETURN VARCHAR2 IS
    v_is_number boolean;
    v_count number;
    v_option_val VARCHAR2(300);
   begin
     v_is_number := pkg_trial.f_check_value_numeric(p_in_field_value);
       IF (v_is_number = TRUE) THEN
                                  select count(id) into v_count from t_field_option where field_option_id=p_in_field_value;
          if(v_count >=1) THEN
          v_option_val:=  pkg_form_common.f_option_values(p_in_field_value);
          return v_option_val;  
          else
            return p_in_field_value;
                    END IF;

      ELSE
       return p_in_field_value;
       END IF; 

FUNCTION f_check_value_numeric(p_in_field_value IN VARCHAR2) RETURN BOOLEAN IS
    v_is_number number;
   begin
     v_is_number := to_number(p_in_field_value);
      return TRUE;
    exception
       when others then
       return FALSE;

   END f_check_value_numeric;

请告诉我如何执行此操作?

1 个答案:

答案 0 :(得分:0)

不确定应如何计算v_option_val的值,但您可以按如下方式拆分分号分隔数据:

FUNCTION p_get_option_value(p_in_field_value IN VARCHAR2) RETURN VARCHAR2 IS
    v_is_number boolean;
    v_count number;
    v_option_val VARCHAR2(300);
    v_pos     NUMBER;
    v_in_field_valid  VARCHAR2(4000);
    v_val     VARCHAR2(4000);
BEGIN
    v_in_field_valid  := p_in_field_value;

    WHILE v_in_field_valid IS NOT NULL 
    LOOP 

      v_pos :=  instr(v_in_field_valid, ';');
      IF v_pos  = 0 THEN
        v_val := v_in_field_valid;
        v_insurance_value := NULL;
      ELSE 
        v_val := SUBSTR(v_in_field_valid, 1, v_pos -1);
        v_in_field_valid := substr(v_str, v_pos + 1);
      END IF;

      v_is_number := pkg_trial.f_check_value_numeric(v_val);
       IF (v_is_number = TRUE) THEN
                                  select count(id) into v_count from t_field_option where field_option_id=v_val;
          if(v_count >=1) THEN
          v_option_val:=  v_option_val || pkg_form_common.f_option_values(v_val);
     END LOOP
    return p_in_field_value;
   END;