CREATE OR REPLACE PROCEDURE bulletin_abbrege(in num INTEGER)
language plpgsql
AS $$
BEGIN
CREATE OR REPLACE VIEW bulletin_abrege AS
SELECT concat(substring(candidat_nom,1,1),substring(candidat_prenom,1,1)) as initiales,parti_abv as abbrevation_parti,compte_nom as compte_nom
FROM candidat INNER JOIN parti ON candidat.parti_id=parti.parti_id INNER JOIN compte ON candidat.compte_id=compte.compte_id
WHERE candidat.compte_id = num;
END;
$$;
[42703] ERROR: column "num" does not exist Where: PL/pgSQL function bulletin_abbrege(integer) line 3 at SQL statement
I don't understand why it says the column does not exist when it is a value passed in arguments in the function?
Thank you for your help
CodePudding user response:
It is a PROCEDURE not a FUNCTION
From Execute query:
Non-optimizable SQL commands (also called utility commands) are not capable of accepting query parameters. So automatic substitution of PL/pgSQL variables does not work in such commands. To include non-constant text in a utility command executed from PL/pgSQL, you must build the utility command as a string and then EXECUTE it, as discussed in Section 43.5.4.
Try something like:
CREATE OR REPLACE PROCEDURE bulletin_abbrege(in num INTEGER)
language plpgsql
AS $$
BEGIN
EXECUTE format('CREATE OR REPLACE VIEW bulletin_abrege AS
SELECT concat(substring(candidat_nom,1,1),substring(candidat_prenom,1,1)) as initiales,parti_abv as abbrevation_parti,compte_nom as compte_nom
FROM candidat INNER JOIN parti ON candidat.parti_id=parti.parti_id INNER JOIN compte ON candidat.compte_id=compte.compte_id
WHERE candidat.compte_id = %L', num);
END;
$$;
Per Dynamic Commands and Format.