I have a case where i want to create a view inside a procedure.
Works fine (Usage of variables)
CREATE OR REPLACE PROCEDURE createViewForAnalysis(analysis INT)
LANGUAGE plpgsql
AS
$$
BEGIN
Perform
code,
isReport,
description,
SUM(dialoge) as dialoge,
SUM(response_time) as response_time,
SUM(response_time)/NULLIF(SUM(dialoge),0) as avg_response_time
FROM codes_joined WHERE analyse_id=analysis
GROUP BY code, isReport ,description;
END;
$$;
Also works fine (create view with fixed value in where)
CREATE OR REPLACE PROCEDURE createViewForAnalysis(analysis INT)
LANGUAGE plpgsql
AS
$$
BEGIN
CREATE OR REPLACE VIEW tcodes_aggregated AS
SELECT
code,
isReport,
description,
SUM(dialoge) as dialoge,
SUM(response_time) as response_time,
SUM(response_time)/NULLIF(SUM(dialoge),0) as avg_response_time
FROM codes_joined AS pg WHERE analyse_id=1
GROUP BY code, isReport ,description;
END;
$$;
When I want to use a variable inside a create view it doesn't take the value inside analysis. It throws "[42703] ERROR: column "analysis" does not exist"
CREATE OR REPLACE PROCEDURE createViewForAnalysis(analysis INT)
LANGUAGE plpgsql
AS
$$
BEGIN
CREATE OR REPLACE VIEW tcodes_aggregated AS
SELECT
code,
isReport,
beschreibung,
SUM(dialoge) as dialoge,
SUM(response_time) as response_time,
SUM(response_time)/NULLIF(SUM(dialoge),0) as avg_response_time
FROM codes_joined AS pg WHERE analyse_id=analysis
GROUP BY code, isReport ,description;
END;
$$;
Any hints?