0

cursreg returns all rows on "continguts" that not have registers on classcont. (this is ok) But i need to insert on "classcont" the int recived on function (classificado int) for each result.

CREATE OR REPLACE FUNCTION fclassifica(clasificado int) RETURNS void AS $$
DECLARE
registre RECORD;
        cursreg CURSOR FOR SELECT c.idcontingut FROM continguts as c
WHERE NOT EXISTS (SELECT * FROM classcont as ca
WHERE c.idcontingut = ca.contingut);
BEGIN
OPEN cursreg;
LOOP
FETCH FROM cursreg INTO registre;
INSERT INTO classcont (class,contingut)
VALUES (clasificado, registre.idcontingut);
RAISE NOTICE 'La classificació de ID ha estat modificada' ;

END LOOP;
CLOSE cursreg;
RETURN ;
END;
$$ LANGUAGE plpgsql;

1 Answer 1

1

No need for a cursor or even PL/pgSQL

CREATE OR REPLACE FUNCTION fclassifica(p_clasificado int) 
  RETURNS void 
AS
$$
  INSERT INTO classcont (class, contingut)
  select p_clasificado, c.idcontingut
  FROM continguts as c
  WHERE NOT EXISTS (SELECT * 
                    FROM classcont as ca
                    WHERE c.idcontingut = ca.contingut);
$$
language sql;
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.