如何在 sql 中创建带参数的视图
How to create a view in sql that takes parameters
我想创建一个接受参数的视图,然后像这样在这个视图上创建一个select离子
CREATE OR REPLACE FUNCTION statistiquess(dateDeb date, dateFin date)
RETURNS void AS
$$
CREATE OR REPLACE VIEW statistics
AS
SELECT
e.matricule_ens,
e.nom_ens,
e.prenom_ens,
m.code_matiere,
m.nom_matiere,
f.id_formation,
f.nom_formation,
SUM ((CAST (c.heure_fin AS TIME))-(CAST (c.heure_deb AS TIME))) AS heure_total_programme,
SUM ((CAST (c.heure_dep_ens AS TIME))-(CAST (c.heure_arr_ens AS TIME))) AS heure_total_enseigne
FROM
enseignant e inner join cours c on e.matricule_ens = c.matricule_ens
inner join matiere m on c.code_matiere = m.code_matiere
inner join formation f on f.id_formation = c.id_formation
WHERE
c.jour between dateDeb and dateFin
GROUP BY
e.matricule_ens, m.code_matiere, f.id_formation
ORDER BY
e.nom_ens;
$$
LANGUAGE SQL;
当我尝试 select 全部来自这样的函数时出现此错误
select * from statistiquess('2019-03-06', '2019-03-29');
错误:la colonne « datedeb » n'existe pas
LINE 6: ..._formation = c.id_formation where (c.jour between datedeb an...
QUERY:
CREATE OR REPLACE VIEW statistics AS select e.matricule_ens, e.nom_ens, e.prenom_ens, m.code_matiere, m.nom_matiere, f.id_formation, f.nom_formation,
SUM ((CAST (c.heure_fin AS TIME))-(CAST (c.heure_deb AS TIME))) AS heure_total_programme,
SUM ((CAST (c.heure_dep_ens AS TIME))-(CAST (c.heure_arr_ens AS TIME))) AS heure_total_enseigne
from enseignant e inner join cours c on e.matricule_ens = c.matricule_ens inner join matiere m on c.code_matiere = m.code_matiere
inner join formation f on f.id_formation = c.id_formation where (c.jour between datedeb and datefin)
GROUP BY e.matricule_ens, m.code_matiere, f.id_formation ORDER BY e.nom_ens;
您上面的语法 function
不是标题所说的视图。只有函数和存储过程可以带参数。 function return 一个值,而存储过程则没有。 syntax for a function 是
CREATE [OR REPLACE] FUNCTION function_name (arguments)
RETURNS return_datatype AS $variable_name$
DECLARE
declaration;
[...]
BEGIN
< function_body >
[...]
RETURN { variable_name | value }
END; LANGUAGE plpgsql;
的语法
CREATE [OR REPLACE] PROCEDURE procedure_name(parameter_list)
LANGUAGE language_name
AS $$
stored_procedure_body;
$$;
视图是一个存储的查询,您可以像查询任何其他查询一样查询 table。
SELECT * FROM statistiquess WHERE c.jour BETWEEN '2019-03-06' AND '2019-03-29';
在不了解您的数据和用例的情况下,很难说您应该使用哪一个。从你的问题来看,你似乎需要一个视图或存储过程。
您不需要在函数内部创建视图来执行此操作。只是 return 函数内的查询结果:
CREATE OR REPLACE FUNCTION statistiquess(dateDeb date, dateFin date)
RETURNS tables (matricule_ens text, nom_ens text, prenom_ens text,
code_matiere text, nom_matiere text, id_formation int,
nom_formation text, heure_total_programme bigint,
heure_total_enseigne bigtin) AS
$$
SELECT
e.matricule_ens,
e.nom_ens,
e.prenom_ens,
m.code_matiere,
m.nom_matiere,
f.id_formation,
f.nom_formation,
SUM ((CAST (c.heure_fin AS TIME))-(CAST (c.heure_deb AS TIME))) AS heure_total_programme,
SUM ((CAST (c.heure_dep_ens AS TIME))-(CAST (c.heure_arr_ens AS TIME))) AS heure_total_enseigne
FROM
enseignant e inner join cours c on e.matricule_ens = c.matricule_ens
inner join matiere m on c.code_matiere = m.code_matiere
inner join formation f on f.id_formation = c.id_formation
WHERE
c.jour between dateDeb and dateFin
GROUP BY
e.matricule_ens, m.code_matiere, f.id_formation
ORDER BY
e.nom_ens;
$$
LANGUAGE SQL;
您将不得不调整 returned 列的数据类型(在 table (...)
部分内)- 我只是猜测它们可能是什么。
然后你可以像"view with parameters":
一样使用它
select *
from statistiquess('2019-03-06', '2019-03-29');
我想创建一个接受参数的视图,然后像这样在这个视图上创建一个select离子
CREATE OR REPLACE FUNCTION statistiquess(dateDeb date, dateFin date)
RETURNS void AS
$$
CREATE OR REPLACE VIEW statistics
AS
SELECT
e.matricule_ens,
e.nom_ens,
e.prenom_ens,
m.code_matiere,
m.nom_matiere,
f.id_formation,
f.nom_formation,
SUM ((CAST (c.heure_fin AS TIME))-(CAST (c.heure_deb AS TIME))) AS heure_total_programme,
SUM ((CAST (c.heure_dep_ens AS TIME))-(CAST (c.heure_arr_ens AS TIME))) AS heure_total_enseigne
FROM
enseignant e inner join cours c on e.matricule_ens = c.matricule_ens
inner join matiere m on c.code_matiere = m.code_matiere
inner join formation f on f.id_formation = c.id_formation
WHERE
c.jour between dateDeb and dateFin
GROUP BY
e.matricule_ens, m.code_matiere, f.id_formation
ORDER BY
e.nom_ens;
$$
LANGUAGE SQL;
当我尝试 select 全部来自这样的函数时出现此错误
select * from statistiquess('2019-03-06', '2019-03-29');
错误:la colonne « datedeb » n'existe pas
LINE 6: ..._formation = c.id_formation where (c.jour between datedeb an...
QUERY:
CREATE OR REPLACE VIEW statistics AS select e.matricule_ens, e.nom_ens, e.prenom_ens, m.code_matiere, m.nom_matiere, f.id_formation, f.nom_formation,
SUM ((CAST (c.heure_fin AS TIME))-(CAST (c.heure_deb AS TIME))) AS heure_total_programme,
SUM ((CAST (c.heure_dep_ens AS TIME))-(CAST (c.heure_arr_ens AS TIME))) AS heure_total_enseigne
from enseignant e inner join cours c on e.matricule_ens = c.matricule_ens inner join matiere m on c.code_matiere = m.code_matiere
inner join formation f on f.id_formation = c.id_formation where (c.jour between datedeb and datefin)
GROUP BY e.matricule_ens, m.code_matiere, f.id_formation ORDER BY e.nom_ens;
您上面的语法 function
不是标题所说的视图。只有函数和存储过程可以带参数。 function return 一个值,而存储过程则没有。 syntax for a function 是
CREATE [OR REPLACE] FUNCTION function_name (arguments)
RETURNS return_datatype AS $variable_name$
DECLARE
declaration;
[...]
BEGIN
< function_body >
[...]
RETURN { variable_name | value }
END; LANGUAGE plpgsql;
的语法
CREATE [OR REPLACE] PROCEDURE procedure_name(parameter_list)
LANGUAGE language_name
AS $$
stored_procedure_body;
$$;
视图是一个存储的查询,您可以像查询任何其他查询一样查询 table。
SELECT * FROM statistiquess WHERE c.jour BETWEEN '2019-03-06' AND '2019-03-29';
在不了解您的数据和用例的情况下,很难说您应该使用哪一个。从你的问题来看,你似乎需要一个视图或存储过程。
您不需要在函数内部创建视图来执行此操作。只是 return 函数内的查询结果:
CREATE OR REPLACE FUNCTION statistiquess(dateDeb date, dateFin date)
RETURNS tables (matricule_ens text, nom_ens text, prenom_ens text,
code_matiere text, nom_matiere text, id_formation int,
nom_formation text, heure_total_programme bigint,
heure_total_enseigne bigtin) AS
$$
SELECT
e.matricule_ens,
e.nom_ens,
e.prenom_ens,
m.code_matiere,
m.nom_matiere,
f.id_formation,
f.nom_formation,
SUM ((CAST (c.heure_fin AS TIME))-(CAST (c.heure_deb AS TIME))) AS heure_total_programme,
SUM ((CAST (c.heure_dep_ens AS TIME))-(CAST (c.heure_arr_ens AS TIME))) AS heure_total_enseigne
FROM
enseignant e inner join cours c on e.matricule_ens = c.matricule_ens
inner join matiere m on c.code_matiere = m.code_matiere
inner join formation f on f.id_formation = c.id_formation
WHERE
c.jour between dateDeb and dateFin
GROUP BY
e.matricule_ens, m.code_matiere, f.id_formation
ORDER BY
e.nom_ens;
$$
LANGUAGE SQL;
您将不得不调整 returned 列的数据类型(在 table (...)
部分内)- 我只是猜测它们可能是什么。
然后你可以像"view with parameters":
一样使用它select *
from statistiquess('2019-03-06', '2019-03-29');