一尘不染

如何在存储过程中拆分逗号分隔的字符串?

sql

如何将逗号分隔的字符串拆分为存储过程中的字符串,并将其插入表字段中?

使用Firebird 2.5


阅读 131

收藏
2021-03-10

共1个答案

一尘不染

这里有一个示例如何分割字符串并将子字符串写入表中:

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
2021-03-10