POSTGRESQL Procedure Copy split string to table

Source: Internet
Author: User

My scene is this, there is a table with a field that uses the split string to save the data.



Although hibernate itself provides org.hibernate.annotations.Type annotations, it is possible to convert this kind of data by implementing Org.hibernate.usertype.UserType, but it is not convenient to use.

To get rid of this data, I used the mapping table instead. For the new data, of course, there is no problem, for historical data, need procedure to synchronize. Share a procedure here.


The main reference is the regexp_split_to_table function of POSTGRESQL.

Regexp_split_to_table (stringtext, pattern text [, flagstext] ) Setof text Split string using a POSIX regular expression as the delimiter. See sections 9.7.3 for more information. Regexp_split_to_table (' Hello World ', E ' \\s+ ') Hello

World

(2 rows)
From the API description, it can be found that the function can be a comma-delimited string of such rules as a delimiter split.

Let's take a look at the effect of the function.


Just to meet the results we want, to provide complete procedure.

DROP FUNCTION IF EXISTS syncref (); CREATE OR REPLACE FUNCTION syncref () RETURNS void As$body$declare r record;sqlstr Text: = ' SELECT id,regexp_split_to_table (Refids, ', ') as RefId from TableA where "Length" (refids) > 0; '; Begindelete from TableB; For R in EXECUTE sqlstr loopif ("Length" (R.refid) > 0 and R.refid! = ', ') Theninsert into TableB (id,ref_id) VALUES (r.id,c AST (R.refid as BIGINT)); END IF; END LOOP; RETURN; End$body$language Plpgsql; ALTER FUNCTION syncref ()  OWNER to DB; SELECT syncref ();D rop FUNCTION IF EXISTS syncref ();


POSTGRESQL Procedure Copy split string to table

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.