0
CREATE OR REPLACE FUNCTION f_sync_from_xml()
  RETURNS boolean AS
$BODY$
DECLARE
    myxml    xml;
    datafile text := 'Questo PC/Documenti/ABBATE_EMANUELE_Lvl1F2Va_20160418-1759.xml';
BEGIN

myxml := pg_read_file(datafile, 0, 100000000); 

END;
$BODY$ language plpgsql;


CREATE TEMP TABLE tmp AS
SELECT (xpath('//some_id/text()', x))[1]::text AS id
FROM   unnest(xpath('/xml/path/to/datum', myxml)) x;

At the last line myxml gives an error the column not exists.

4
  • unnest(xpath('/xml/path/to/datum', myxml)) here myxml is variable from f_sync_from_xml()?.. Commented Nov 21, 2017 at 11:24
  • I want a procedure to create a table from xml file but myxml gives an error the column not exists Commented Nov 21, 2017 at 11:24
  • so put it inside function?.. Commented Nov 21, 2017 at 11:25
  • I declared a variable myxml... Commented Nov 21, 2017 at 11:25

1 Answer 1

2

put statement inside the function body where you declare the variable myxml

CREATE OR REPLACE FUNCTION f_sync_from_xml()
  RETURNS boolean AS
$BODY$
DECLARE
    myxml    xml;
    datafile text := 'Questo PC/Documenti/ABBATE_EMANUELE_Lvl1F2Va_20160418-1759.xml';
BEGIN

myxml := pg_read_file(datafile, 0, 100000000); 

CREATE TEMP TABLE tmp AS
SELECT (xpath('//some_id/text()', x))[1]::text AS id
FROM   unnest(xpath('/xml/path/to/datum', myxml)) x;
END;
$BODY$ language plpgsql;

also - if you jsut create a table on each execution - second execution will give you error, that tble exists

you can use IF NOT EXISTS:

t=# create temp table if not exists tmp as select now();
SELECT 1
Time: 57.280 ms
t=# create temp table if not exists tmp as select now();
NOTICE:  relation "tmp" already exists, skipping
CREATE TABLE AS
Time: 0.223 ms
Sign up to request clarification or add additional context in comments.

4 Comments

How I create a table? I have to drop temp from the create table?
With if not exists give an error on as my code: CREATE TEMP TABLE if not exists tmp AS SELECT (xpath('//some_id/text()', x))[1]::text AS id FROM unnest(xpath('/xml/path/to/datum', myxml)) x; END; $BODY$ language plpgsql;
maybe you have older version with create tem table AS not supported yet?..
pgadmin III is my version

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.