调用函数Postgresql

时间:2011-04-01 04:40:38

标签: sql function postgresql

我想从函数中插入表lokasi ...但是当我调用这些函数时出现错误...请回答

CREATE OR REPLACE FUNCTION insert_lokasi2
  (anip character varying, aeksemplar character varying)
RETURNS boolean AS
$BODY$

DECLARE
  eks integer;
  tot integer;
  nilai boolean;
  eks1 integer;
  eks2 integer;
  tot2 integer;

BEGIN

   select sum(CAST(eksemplar AS INT)) 
     INTO eks 
     from lokasi 
    where nip = anip;

   tot := eks + aeksemplar;

   select CAST(eksemplar AS INT) 
     INTO eks1 
     from sensus 
    where nip = anip;

   select CAST(eksemplar2 AS INT) 
     INTO eks2 
     from sensus 
    where nip = anip;

   tot2 := eks1 + eks2;

   IF (tot <> tot2) THEN
     nilai := false;
   else 
     nilai := true;
   END IF;

   RETURN nilai;

END 

$BODY$

  LANGUAGE 'plpgsql' VOLATILE

  COST 100;

  ALTER FUNCTION insert_lokasi2(character varying, character varying) OWNER TO postgres;

  select * from insert_lokasi2('10.1010.4703','1');

错误:运算符不存在:整数+字符变化

LINE 1: SELECT   $1  +  $2 
                     ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
QUERY:  SELECT   $1  +  $2 
CONTEXT:  PL/pgSQL function "insert_lokasi2" line 12 at assignment

1 个答案:

答案 0 :(得分:1)

eks是一个整数,而aeksemplar是一个字符串。您需要添加一个演员:

tot := eks + CAST(aeksemplar AS INT)

最好是在结尾处做所有这些演员,或者,如果可能的话,更改参数类型,以便演员不必要。