sorin
sorin

Reputation: 170330

How to convert a big set of SQL queries into a single stored procedure that uses a variable?

I am trying to convert a big list of SQL statements into a PostgreSQL stored procedure that uses a variable, one that should be populated from the result of one SELECT.

If you want to see what has to be run, you can check it here

As far as I know PostgreSQL does not allow use to use variables inside stored procedures that are using SQL language, so I'm looking for solutions that would require a minimal number of changes.

Upvotes: 0

Views: 190

Answers (2)

Miki
Miki

Reputation: 7188

The code seems to be pretty repetitive. Will EXECUTE be of any help? (manual about execute) (example and more information) It allows you to run predefined queries and create new ones on the fly.

Upvotes: 1

sorin
sorin

Reputation: 170330

It's much easier after you find the right syntax:

Here is the procedure definition for plpgsql language:

DECLARE myvar integer;
BEGIN
SELECT INTO myvar FROM ...;
-- use myvar
END;

Upvotes: 1

Related Questions