1

I have the following stored procedure or function defined in my Postgresql database:

    CREATE OR REPLACE FUNCTION insert_val(int)
    $body$
    BEGIN
      FOR i IN 1..10 LOOP
        insert into test (val)
        values($23);
      END LOOP;
   END;
   $body$ Language 'plpgsql' VOLATILE;

I just want to insert this data inside of a loop, but I get always this error:

Syntaxfehler bei »begin«

Is maybe that I missed something in my function?

2 Answers 2

2

I don't understand the error message since it's not in English, but I can see a few problems in your code

CREATE OR REPLACE FUNCTION insert_val(IN val int) RETURNS VOID  AS 
$body$
    BEGIN
      FOR i IN 1..10 LOOP
        insert into test (val)
        values($23);
      END LOOP;
   END;
   $body$ Language 'plpgsql' VOLATILE;

You were missing the return type, you were missing AS and you have forgotten to name the in argument.

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

3 Comments

Hi THX for your answer, you were right, I mist the Returns Void As. I executed the function and I didn't get any errors this time, but also not row in the table test was affected, I mean didn't insert nothing. Can you PLS help me? P.S: The error that I got just means syntax error in $body$, sorry for the not translation
well it will not result in a row being created in the table until you call it! select insert_val(1) for example!
Thanks for the advice
1

You forgot

RETURNS void AS

between the first and the second line.

But that will only take care of the syntax. The $23 is clearly wrong since there are no 23 function arguments. Did you mean $1?

2 Comments

Hi, yes I added already the values to insert like: values($1, $2, $3, $4, $5, $6, $7, $8, $9, $10). I fixed already the syntax error of RETURNS void AS, but I still don't get the correct result
Explain your problem.

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.