在PL / pgSQL函数中选择或执行

时间:2012-04-12 00:21:11

标签: postgresql function plpgsql select-into

我在我的数据库中有这个功能:

CREATE OR REPLACE FUNCTION "insertarNuevoArticulo"(nombrearticulo character varying, descripcion text, idtipo integer, idfamilia bigint, artstock integer, minstock integer, maxstock integer, idmarca bigint, precio real, marcastock integer)
RETURNS boolean AS
$BODY$
DECLARE
    articulo "Articulo"%ROWTYPE;
BEGIN
    SELECT * INTO articulo FROM "Articulo" WHERE "Nombre" = $1 AND "idTipo"=$3 AND "idFamilia"=$4;
    IF NOT FOUND THEN
        INSERT INTO "Articulo" ("Nombre", "Descripcion", "idTipo", "idFamilia", "Stock", "MinStock", "MaxStock") Values ($1, $2, $3, $4, $5, $6, $7);
        SELECT last_value
        INTO articulo."idArticulo"
        FROM "public"."Articulo_idArticulo_seq";
    END IF;
    SELECT * FROM "ArticuloMarca" AS am WHERE am."idArticulo" = articulo."idArticulo" and am."idMarca" = $8;
    IF NOT FOUND THEN
        Insert into "ArticuloMarca"("idArticulo", "idMarca", "PrecioReferencial", "Stock") Values (articulo."idArticulo", $8, $9, $10);
        RETURN TRUE;
    END IF;
    RETURN FALSE;
END;
$BODY$
 LANGUAGE plpgsql VOLATILE
 COST 100;
 ALTER FUNCTION "insertarNuevoArticulo"(character varying, text, integer, bigint, integer, integer, integer, bigint, real, integer)
 OWNER TO postgres;

但是一旦我尝试使用它,它说如果我想丢弃结果我需要使用PERFORM!这里的问题是我不想!我希望它们在我声明的articulo行中。

我正在使用这句话:

SELECT "insertarNuevoArticulo"('Acetaminofen', 'caro', '1' , '1', '8', '1', '10', '1', '150.7', '10');

我得到的错误是42601,语法错误!如果我使用IDE创建它怎么可能?对这个问题有什么看法吗?

2 个答案:

答案 0 :(得分:13)

在plpgsql代码中,没有目标的SELECT会触发错误。但是你很明显想要SELECT INTO,你只想设置FOUND的状态。您可以使用PERFORM

更好,但,请使用IF EXISTS ...。考虑一下这个函数的重写:

CREATE OR REPLACE FUNCTION "insertarNuevoArticulo"
      (nombrearticulo text, descripcion text, idtipo int, idfamilia bigint, artstock int
     , minstock int, maxstock int, idmarca bigint, precio real, marcastock int)
  RETURNS boolean AS
$func$
DECLARE
    _id_articulo "Articulo"."idArticulo"%TYPE;
BEGIN
    SELECT a."idArticulo" INTO _id_articulo
    FROM   "Articulo" a
    WHERE  a."Nombre" = $1 AND a."idTipo" = $3 AND a."idFamilia" = $4;

    IF NOT FOUND THEN
        INSERT INTO "Articulo"("Nombre", "Descripcion", "idTipo"
                             , "idFamilia", "Stock", "MinStock", "MaxStock")
        VALUES ($1, $2, $3, $4, $5, $6, $7)
        RETURNING "Articulo"."idArticulo" INTO _id_articulo;
    END IF;

   IF EXISTS (SELECT FROM "ArticuloMarca" a
              WHERE a."idArticulo" = _id_articulo AND a."idMarca" = $8) THEN
      RETURN FALSE;
   ELSE
      INSERT INTO "ArticuloMarca"("idArticulo", "idMarca", "PrecioReferencial", "Stock")
      VALUES (_id_articulo, $8, $9, $10);
      RETURN TRUE;
    END IF;
END
$func$  LANGUAGE plpgsql;

关于EXISTS

其他主要观点

Postgres 9.5 +

在Postgres 9.5或更高版本中,请改为使用UPSERT :( INSERT ... ON CONFLICT DO NOTHING) 您会在UNIQUE"Articulo"("Nombre", "idTipo", "idFamilia")上获得"ArticuloMarca"("idArticulo", "idMarca")个约束,然后:

CREATE OR REPLACE FUNCTION insert_new_articulo
      (nombrearticulo text, descripcion text, idtipo int, idfamilia bigint, artstock int
     , minstock int, maxstock int, idmarca bigint, precio real, marcastock int)
  RETURNS boolean AS
$func$
DECLARE
    _id_articulo "Articulo"."idArticulo"%TYPE;
BEGIN
   LOOP
      SELECT "idArticulo" INTO _id_articulo
      FROM   "Articulo"
      WHERE  "Nombre" = $1 AND "idTipo" = $3 AND "idFamilia" = $4;

      EXIT WHEN FOUND;

      INSERT INTO "Articulo"("Nombre", "Descripcion", "idTipo"
                           , "idFamilia", "Stock", "MinStock", "MaxStock")
      VALUES ($1, $2, $3, $4, $5, $6, $7)
      ON     CONFLICT (tag) DO NOTHING
      RETURNING "idArticulo" INTO _id_articulo;

      EXIT WHEN FOUND;
   END LOOP;

   LOOP
      INSERT INTO "ArticuloMarca"("idArticulo", "idMarca", "PrecioReferencial", "Stock")
      VALUES (_id_articulo, $8, $9, $10)
      ON     CONFLICT ("idArticulo", "idMarca") DO NOTHING;

      IF FOUND THEN
         RETURN TRUE;
      END IF;

      IF EXISTS (SELECT FROM "ArticuloMarca"
                 WHERE "idArticulo" = _id_articulo AND "idMarca" = $8) THEN
         RETURN FALSE;
      END IF;
   END LOOP;
END
$func$ LANGUAGE plpgsql;

这更快,更简单,更可靠。添加的循环排除了并发写入的任何剩余竞争条件(几乎不添加任何成本)。没有并发写入,您可以简化。详细解释:

除此之外:使用合法的小写标识符来避免所有丑陋的double-quoting

答案 1 :(得分:4)

这条线看起来很可疑,可能是导致你悲痛的原因:

SELECT * FROM "ArticuloMarca" AS am WHERE am."idArticulo" = articulo."idArticulo" and am."idMarca" = $8;

您正在执行函数中的SELECT,但没有对结果执行任何操作。您需要像在函数中一样执行SELECT INTO。