As I understand, in Postgres, procedures are for manipulating data and functions are for reading data. I would like to:
- declare a variable
- select a value into a variable
- insert the variable into another table
- do this in a transaction
- do this in a stored procedure
- roll back if necessary
CREATE PROCEDURE test_variable()
LANGUAGE SQL
AS $$
BEGIN;
DECLARE some_id INTEGER;
SELECT nextval('some_sequence') INTO some_id;
INSERT INTO some_table (some_column) VALUES (some_id);
END;
$$;
The above is not working out for me. When I search for solutions, there's so many different variables involving functions, $$
, declaration, thing not returning anything; can't seem to find a simple example; I just need a clear example of the syntax.
some_id
.ERROR: syntax error at or near "INTEGER"
.declare
section must come before the firstbegin
blockBEGIN
andBEGIN;
are two different things. You need at least `DECLARE ...; BEGIN -- without semicolon! ... END; block, just as @a_horse_with_no_name said. Also, a function call is always wrapped in a transaction, so you don't necessarily have to deal with it yourself in a procedure.procedures are for manipulating data and functions are for reading data
. Whoever told you this, may be an expert on flying saucers, but certainly not on Postgres.