加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

oracle – 在PL/SQL存储过程中分割逗号分隔的字符串

发布时间:2020-12-12 14:01:39 所属栏目:百科 来源:网络整理
导读:我有CSV字符串100.01,200.02,300.03,我需要传递到Oracle中的PL / SQL存储过程。 在proc中,我需要在表的Number列中插入这些值。 为此,我从这里得到了一个工作方法: http://stackoverflow.com/questions/1089508/how-to-best-split-csv-strings-in-oracle-
我有CSV字符串100.01,200.02,300.03,我需要传递到Oracle中的PL / SQL存储过程。
在proc中,我需要在表的Number列中插入这些值。

为此,我从这里得到了一个工作方法:

http://stackoverflow.com/questions/1089508/how-to-best-split-csv-strings-in-oracle-9i

[2]按层次使用SQL的连接。

现在,我有另一个要求。
我需要传递2个CSV字符串(长度相等)作为PL / SQL存储proc.的输入。我需要打破这个字符串,并将两个CSV字符串中的每个值插入表中的两个不同的列。请允许我知道怎么去呢?

CSV输入示例:
mystring varchar2(2000):=’0.75,0.64,0.56,0.45′;

myAmount varchar2(2000):=’0.25,0.5,0.65,0.8′;

myString值将进入列A和myAmount值到表B中的列。

你能让我知道如何实现吗?

谢谢。

这应该做你正在寻找的..它假设你的列表将永远只是数字。如果不是这样,只需将对DBMS_SQL.NUMBER_TABLE的引用更改为适用于所有数据的表类型:
CREATE OR REPLACE PROCEDURE insert_from_lists(
    list1_in IN VARCHAR2,list2_in IN VARCHAR2,delimiter_in IN VARCHAR2 := ','
)
IS 
    v_tbl1 DBMS_SQL.NUMBER_TABLE;
    v_tbl2 DBMS_SQL.NUMBER_TABLE;

    FUNCTION list_to_tbl
    (
        list_in IN VARCHAR2
    )
    RETURN DBMS_SQL.NUMBER_TABLE
    IS
        v_retval DBMS_SQL.NUMBER_TABLE;
    BEGIN

        IF list_in is not null
        THEN
            /*
            || Use lengths loop through the list the correct amount of times,|| and substr to get only the correct item for that row
            */
            FOR i in 1 .. length(list_in)-length(replace(list_in,delimiter_in,''))+1
            LOOP
                /*
                || Set the row = next item in the list
                */
                v_retval(i) := 
                        substr (
                            delimiter_in||list_in||delimiter_in,instr(delimiter_in||list_in||delimiter_in,1,i  ) + 1,instr (delimiter_in||list_in||delimiter_in,i+1) - instr (delimiter_in||list_in||delimiter_in,i) -1
                        );
            END LOOP;
        END IF;

        RETURN v_retval;

    END list_to_tbl;
BEGIN 
   -- Put lists into collections
   v_tbl1 := list_to_tbl(list1_in);
   v_tbl2 := list_to_tbl(list2_in);

   IF v_tbl1.COUNT <> v_tbl2.COUNT
   THEN
      raise_application_error(num => -20001,msg => 'Length of lists do not match');
   END IF;

   -- Bulk insert from collections
   FORALL i IN INDICES OF v_tbl1
      insert into tmp (a,b)
      values (v_tbl1(i),v_tbl2(i));

END insert_from_lists;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读