Dennys Gonzalez
Dennys Gonzalez

Reputation: 39

Stored procedure to insert data into Postgresql

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?

Upvotes: 1

Views: 4039

Answers (2)

Laurenz Albe
Laurenz Albe

Reputation: 246308

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?

Upvotes: 1

e4c5
e4c5

Reputation: 53734

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.

Upvotes: 2

Related Questions