Oracle PLSQL从动态字符串中提取值
我有一个字符串,它可以有不同的参数和不同数量的参数。我正在尝试获取参数名和参数值。例如:Oracle PLSQL从动态字符串中提取值,oracle,plsql,Oracle,Plsql,我有一个字符串,它可以有不同的参数和不同数量的参数。我正在尝试获取参数名和参数值。例如: FIRST_PARAM=1^SECOND_PARAM=2^THIRD_PARAM=3^FOURTH_PARAM=4^ 我可以通过现有的包获取参数值,但这是我遇到困难的名称。到目前为止,我所做的是: param_count_ := length(params_) - length(replace(params_,'^',null)); DBMS_OUTPUT.PUT_LINE('Param Count: '
FIRST_PARAM=1^SECOND_PARAM=2^THIRD_PARAM=3^FOURTH_PARAM=4^
我可以通过现有的包获取参数值,但这是我遇到困难的名称。到目前为止,我所做的是:
param_count_ := length(params_) - length(replace(params_,'^',null));
DBMS_OUTPUT.PUT_LINE('Param Count: '||param_count_);
WHILE(index_ <= param_count_)
LOOP
param_value_ := client_sys.Get_Key_Reference_Value(params_,index_);
IF(index_ = 1) THEN
param_name_ := NVL(SUBSTR(params_, 0, INSTR(params_, '=')-1), params_);
ELSE
param_name_ := SUBSTR(params_,
INSTR(params_, '^',1,index_) + 1,
INSTR(params_, '=',1,index_+1) - INSTR(params_, '^',1,index_) - 1);
END IF;
DBMS_OUTPUT.PUT_LINE('Reference Name: '||param_name_);
DBMS_OUTPUT.PUT_LINE('Reference Value: '||param_value_);
index_ := index_+1;
END LOOP;
它似乎一点也不合适。我怎样才能解决这个问题呢?以下是满足您要求的一种方法:
BEGIN
FOR cur IN (SELECT REGEXP_SUBSTR(PARAM_, '[^= ]+', 1, 1) AS "KEY",
REGEXP_SUBSTR(PARAM_, '[^= ^]+', 1, 2) AS "VALUE"
FROM
(SELECT REGEXP_SUBSTR(str, '[^\^ ]+', 1, LEVEL) AS PARAM_
FROM (SELECT 'FIRST_PARAM=1^SECOND_PARAM=2^THIRD_PARAM=3^FOURTH_PARAM=4^'
AS STR FROM DUAL) T
CONNECT BY REGEXP_SUBSTR(str, '[^\^ ]+', 1, LEVEL) IS NOT NULL
))
LOOP
DBMS_OUTPUT.PUT_LINE('Reference Name: '|| cur.KEY ||
' <=> '|| 'Reference Value: '|| cur.VALUE);
END LOOP;
END;
输出:
Reference Name: FIRST_PARAM <=> Reference Value: 1
Reference Name: SECOND_PARAM <=> Reference Value: 2
Reference Name: THIRD_PARAM <=> Reference Value: 3
Reference Name: FOURTH_PARAM <=> Reference Value: 4
Reference Name: FIRST_PARAM <=> Reference Value: 1
Reference Name: SECOND_PARAM <=> Reference Value: 2
Reference Name: THIRD_PARAM <=> Reference Value: 3
Reference Name: FOURTH_PARAM <=> Reference Value: 4