0

I want to insert table lokasi from function... but when i call these function there are error... Please your answer

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');

ERROR: operator does not exist: integer + character varying

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 Answer 1

1

eks is an integer whilst aeksemplar is a string. You need a cast by your addition:

tot := eks + CAST(aeksemplar AS INT)

Better would be to either do all these castings at the top of the finction or, if possible, change the argument types so that the casting is unecessary.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.