1

How to loop through the xml array. Here is the following code which I tried.

for c in select (unnest((xpath('//PayTypes/@value', ('<dummy_root><IncludeSettle/><StartTime value="2019-03-26 08:45:48.780"></StartTime><PayTypes value="1"/><PayTypes value="2"/></dummy_root>')::xml)))::text)
loop
raise notice '%', c;
end loop;

I am getting back the following error saying below:

ERROR:  loop variable of loop over rows must be a record variable or list of scalar variables
LINE 11: for c in select (unnest((xpath('//PayTypes/@value', ('<dummy...

Any help is really appreciated. Output I am expecting is printing values 1, 2.

1

1 Answer 1

1

Maybe, you didn't declare the iterator c, I had the same problem with this cause. The code would look like (foo is the table):

    CREATE OR REPLACE FUNCTION your_method() RETURNS SETOF foo AS
    $BODY$
    DECLARE
        c xml; 
    BEGIN
        FOR c IN
            SELECT xml_column FROM foo
        LOOP
            raise notice '%', c;
        END LOOP;
        RETURN;
    END
    $BODY$
    LANGUAGE plpgsql;

    SELECT * FROM your_method();
Sign up to request clarification or add additional context in comments.

Comments

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.