sql - Error while concatenating plpgsql var with query on cursor statement -
i getting error trying concatenate var sch in second for:
error: syntax error in or next "||" sql state: 42601 character: 1151
does know how solve problem concatenation?
create or replace function generate_mallet_input2() returns void $$ declare sch name; r record; begin sch in select schema_name information_schema.schemata schema_name not in ('test','summary','public','pg_toast','pg_temp_1','pg_toast_temp_1','pg_catalog','information_schema') loop r in select rp.id id,g.classified classif, concat(rp.summary,rp.description,string_agg(c.message, '. ')) mess sch.report rp inner join || sch || .report_comment rc on rp.id=rc.report_id inner join || sch || .comment c on rc.comments_generatedid=c.generatedid inner join || sch || .gold_set g on rp.id=g.key g.classified = (values('bug'),('improvement'),('refactoring')) group g.classified,rp.summary,rp.description,rp.id loop if r.classif = 'bug' execute 'copy( values('|| r.mess || ') ) ''/tmp/csv-temp/bug/' || quote_ident(sch) || '-' || r.id || '.txt '' delimiter '' '' '; elsif r.classif = 'improvement' execute 'copy( values('|| r.mess || ') ) ''/tmp/csv-temp/improvement/' || quote_ident(sch) || '-' || r.id || '.txt '' delimiter '' '' '; elsif r.classif = 'refactoring' execute 'copy( values('|| r.mess || ') ) ''/tmp/csv-temp/refactoring/' || quote_ident(sch) || '-' || r.id || '.txt '' delimiter '' '' '; end if; end loop; end loop; return; end; $$ language plpgsql strict;
you cannot dynamically change schema embedded sql. database related identifier have constant. if have modern postgresql (probably higher 9.2, works in 9.4), can change search path:
create schema s1; create schema s2; create table s1.t1(a int); create table s2.t1(a int); insert s1.t1 values(10); insert s2.t1 values(20); $$ declare r record; begin in 1..2 loop perform set_config('search_path', 's'||i, true); r in select * t1 loop raise notice '%', r; end loop; end loop; end; $$;
if code doesn't work, have use dynamic sql - for in execute
statement.
Comments
Post a Comment