如何在sql中创建具有参数的视图
我想创建一个带有参数的视图,然后像这样在此视图上进行选择如何在sql中创建具有参数的视图,sql,postgresql,postgresql-9.4,Sql,Postgresql,Postgresql 9.4,我想创建一个带有参数的视图,然后像这样在此视图上进行选择 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,
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 * from statistiquess('2019-03-06', '2019-03-29');
当我尝试像这样从函数中选择all时,出现了这个错误
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 * from statistiquess('2019-03-06', '2019-03-29');
错误:科隆«datedeb»不存在
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;
上面的语法具有
函数
而不是标题所述的视图。只有函数和存储过程可以接受参数。函数返回一个值,而存储过程不返回。问题是
视图是一种存储查询,您可以像查询任何其他表一样进行查询
SELECT * FROM statistiquess WHERE c.jour BETWEEN '2019-03-06' AND '2019-03-29';
如果不了解您的数据和用例,就很难说您应该使用哪一个。从您的问题来看,您似乎需要一个视图或存储过程。您不需要在函数内部创建视图来实现这一点。只需从函数中返回查询结果:
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;
您必须调整返回列的数据类型(在表(…)
part中)——我只是猜测它们可能是什么
然后您可以像“带参数的视图”一样使用它:
我的所有数据都在课程表中,该表包含课程开始时间、课程结束时间、教师到达时间、教师离开时间、教师标识符、科目标识符和培训标识符。因此,我想计算编程的总小时数,教学的总小时数,错过的总小时数,按科目分组,教师,两个日期之间的培训。您可以创建一个视图,每个事件有一行,并创建一个查询。或者您可以创建一个存储过程来输出您所需要的内容。视图将允许您对同一数据集提出不同的问题。如果只返回表,存储过程不是更好吗?@denov:不,使用(SQL)函数更好。首先,您可以针对函数的结果进行连接,并且可以在其上应用where条件(过程不可能),其次,如果函数用于更大的内容(例如连接),优化器可以内联查询。此外:如果将refcursor用作out参数,则过程只能“返回”某些内容。在SQL客户机中运行时使用非常复杂
select *
from statistiquess('2019-03-06', '2019-03-29');