How to split comma separated string inside stored procedure?

后端 未结 6 1534
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-16 23:41

How to split comma separated string into strings inside store procedure and insert them into a table field?

Using Firebird 2.5

6条回答
  •  自闭症患者
    2020-12-16 23:57

    Here a sample how to split the string and write the sub-strings into a table:

    create procedure SPLIT_STRING (
      AINPUT varchar(8192))
    as
    declare variable LASTPOS integer;
    declare variable NEXTPOS integer;
    declare variable TEMPSTR varchar(8192);
    begin
      AINPUT = :AINPUT || ',';
      LASTPOS = 1;
      NEXTPOS = position(',', :AINPUT, LASTPOS);
      while (:NEXTPOS > 1) do
      begin
        TEMPSTR = substring(:AINPUT from :LASTPOS for :NEXTPOS - :LASTPOS);
        insert into new_table("VALUE") values(:TEMPSTR);
        LASTPOS = :NEXTPOS + 1;
        NEXTPOS = position(',', :AINPUT, LASTPOS);
      end
      suspend;
    end
    

提交回复
热议问题