Sample Header Ad - 728x90

How to use variables in procedure (not function)?

8 votes
1 answer
36715 views
As I understand, in Postgres, procedures are for manipulating data and functions are for reading data. I would like to: 1. declare a variable 2. select a value into a variable 3. insert the variable into another table 4. do this in a transaction 5. do this in a stored procedure 6. 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.
Asked by Alexander Kleinhans (421 rep)
Nov 2, 2018, 12:37 PM
Last activity: Apr 24, 2022, 12:52 PM