sql 从逗号分隔列postgresql中插入splitin值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 从逗号分隔列postgresql中插入splitin值相关的知识,希望对你有一定的参考价值。

-- inserting
select for_loop_through_query();
-- selecting
select * from temporary_table;
create function for_loop_through_query() returns void
    language plpgsql
as
$$
DECLARE
    rec  RECORD;
    item RECORD;
BEGIN

    FOR rec IN select users.id             as userid,
                      users.name           as username,
                      accounts.name        as account,
                      users.multifonnumber as multifonnumber
               from iris_contact users
                        left join iris_account accounts on users.accountid = accounts.id
               where users.multifonnumber is not null
                 and users.name not like '%Мариуполь%'
        LOOP
            for item in select *
            -- multifonnumber - column that contains comma separated value that needs to split
                        from regexp_split_to_table(rec.multifonnumber, ',')
                loop
                    insert into temporary_table(fio,
                                                account,
                                                multifonnumber)
                    values (rec.username,
                            rec.account,
                            item);
                end loop;

        END LOOP;
END;
$$;
alter function for_loop_through_query() owner to postgres;

以上是关于sql 从逗号分隔列postgresql中插入splitin值的主要内容,如果未能解决你的问题,请参考以下文章

读取逗号分隔值并批量插入SQL表

如何将不同的逗号分隔值插入mysql中的不同列

SQL Server 2005 中的逗号分隔值插入

通过连接来自不同列的逗号分隔值来创建字符串

如何转换逗号分隔列表中的列数据

SQL 2005 在分隔符上拆分逗号分隔列