CREATE OR REPLACE FUNCTION "getArticulos"(refcursor)
RETURNS refcursor AS
$BODY$
BEGIN
CREATE TEMP TABLE "Temporal" AS
SELECT a."idArticulo", SUM("Stock") AS "Stock"
FROM "ArticuloMarca" AS am, "Articulo" AS a
WHERE a."idArticulo" = am."idArticulo"
GROUP BY a."idArticulo"
ORDER BY a."idArticulo";
OPEN $1 FOR
SELECT DISTINCT(a."idArticulo"), a."Nombre", a."Descripcion", a."idFamilia", f."Nombre" AS "Familia",a."idTipo", t."Nombre" AS "Tipo", tmp."Stock", a."MinStock", a."MaxStock"
FROM "Articulo" AS a, "ArticuloMarca" AS am, "Familia" AS f, "Tipo" AS t, "Temporal" AS tmp
WHERE a."idFamilia" = f."idFamilia" AND a."idTipo" = t."idTipo" AND a."idArticulo" = tmp."idArticulo"
ORDER BY a."idArticulo";
RETURN $1;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
这就是我的功能,但我一直在尝试不使用TEMP TABLE,我需要一些我不需要删除的内容。
我尝试过RECORD,这个叫做refcursor,但是我得到了一行,我需要整个结果。知道我可以使用什么?¿
我尝试在RETURN $ 1之前和之前添加DROP TABLE“Temporal”;但它不起作用。
答案 0 :(得分:3)
我认为您不需要为简单查询创建TEMP表(甚至也不需要创建函数)。更重要的是,如果你使用提供WITH queries的Postgresql 9.x,你应该阅读它。
答案 1 :(得分:1)
请检查documentation的这一部分。目前还不是很清楚你想要实现什么,但我猜,你需要一套返回功能。
我希望这段代码可以满足您的需求:
CREATE OR REPLACE FUNCTION "getArticulos"(
OUT "Articulo"."idArticulo",
OUT "Articulo"."Nombre",
OUT "Articulo"."Descripcion",
OUT "Articulo"."idFamilia",
OUT "Familia"."Nombre",
OUT "Articulo"."idTipo",
OUT "Tipo"."Nombre",
OUT int4, -- I cannot get the source table for “Stock” column
OUT "Articulo"."MinStock",
OUT "Articulo"."MinStock"
)
RETURNS SETOF record
AS $BODY$
BEGIN
RETURN QUERY
WITH "Temporal" AS (
SELECT a."idArticulo", SUM("Stock") AS "Stock"
FROM "ArticuloMarca" AS am, "Articulo" AS a
WHERE a."idArticulo" = am."idArticulo"
GROUP BY a."idArticulo"
ORDER BY a."idArticulo")
SELECT DISTINCT(a."idArticulo"), a."Nombre", a."Descripcion", a."idFamilia", f."Nombre" AS "Familia",a."idTipo", t."Nombre" AS "Tipo", tmp."Stock", a."MinStock", a."MaxStock"
FROM "Articulo" AS a, "ArticuloMarca" AS am, "Familia" AS f, "Tipo" AS t, "Temporal" AS tmp
WHERE a."idFamilia" = f."idFamilia" AND a."idTipo" = t."idTipo" AND a."idArticulo" = tmp."idArticulo"
ORDER BY a."idArticulo";
END;
$BODY$
LANGUAGE plpgsql VOLATILE COST 100;
由于显而易见的原因,我无法测试此功能。
另外,请考虑将LANGUAGE
更改为SQL
,因为plpgsql开销没有理由执行此操作。