3

I am trying to construct a string from a table. I need to construct a string from each row and then concatenate these strings (with an appropriate separator) together. In a simple example the select produces a table with 3 rows; however, I get the message above.

Can anyone help? Thanks

for mFiles in 
    select url || pth || basename || '/' || basename || '.2.' || clientname into fName from files 
       where ( userId is null or uid != userId ) and (url is not null) and (pth is not null) 
          order by RANDOM() limit nImages LOOP
       res := res || fName || '|';
     RAISE NOTICE ' fName: % ' , fName;
   END LOOP;
1
  • 1
    What's your PostgreSQL version? Odd issue. I haven't looked into it or tested, but if I had to make a wild guess I'd be wondering if the limit clause was the issue... Commented Dec 30, 2014 at 1:10

2 Answers 2

2

The problem was 'into fName'. Replacing this by 'as fName' and writing res:= res || mFiles.fName || '|';

fixed the problem. Don't understand why exactly but ...

Sign up to request clarification or add additional context in comments.

Comments

0

Following from Craig Ringer's suggestion, what if you remove the limit clause and instead break out of the loop with code like this (which would both test whether limit is the culprit and provide a good workaround):

idx := 1
for mFiles in 
    select url || pth || basename || '/' || basename || '.2.' || clientname into fName from files 
    where ( userId is null or uid != userId ) and (url is not null) and (pth is not null) 
    order by RANDOM()
    LOOP
        res := res || fName || '|';
        RAISE NOTICE ' fName: % ' , fName;

        IF idx = nImages THEN
            EXIT
        END IF;
    END LOOP;

With the addition of declaring idx in the appropriate section higher up in the code.

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.