Answer the question
In order to leave comments, you need to log in
How to fix a problem in the procedure for adding records?
Hello! Please help me figure it out. There is a RELATIONSHIP table in a PostgreSQL database where the STATUS and SEND columns default to false .
| SURVEY_ID | EMPLOYEE | STATUS (default: false) | SEND (default: false) | ORGANIZATION_NAME |
|--------------------------------------|----------|-------------------------|-----------------------|-------------------|
| d5f9c639-13e6-42c1-9043-30783981724b | Mark | false | false | Apple |
| d5f9c639-13e6-42c1-9043-30783981724b | Bob | true | false | Google |
CREATE OR REPLACE PROCEDURE creator(SURVEY_IDENTIFIER uuid, EMPLOYEES VARCHAR[], ORGANIZATION_NAMES VARCHAR[]) AS $FUNCTION$
BEGIN
INSERT INTO RELATIONSHIP (SURVEY_ID, EMPLOYEE, ORGANIZATION_NAME)
SELECT
SURVEY_IDENTIFIER AS SURVEY_ID,
EMPLOYEE FROM UNNEST(ARRAY[EMPLOYEES], ARRAY[ORGANIZATION_NAMES]) u(EMPLOYEE, ORGANIZATION_NAME)
ON CONFLICT ON CONSTRAINT RELATIONSHIP_UNIQUE_KEY
DO UPDATE SET ORGANIZATION_NAME = u.ORGANIZATION_NAME
WHERE NOT STATUS;
END;
$FUNCTION$ LANGUAGE plpgsql;
CALL creator(
'd5f9c639-13e6-42c1-9043-30783981724b',
ARRAY['Mark', 'Bob', 'Kate'],
ARRAY['Google', 'Google', 'HP']
);
| SURVEY_ID | EMPLOYEE | ORGANIZATION_NAME | STATUS (default: false) | SEND (default: false) | ORGANIZATION_NAME |
|--------------------------------------|----------|-------------------|-------------------------|-----------------------|-------------------|
| d5f9c639-13e6-42c1-9043-30783981724b | Mark | Google | false | false | Google |
| d5f9c639-13e6-42c1-9043-30783981724b | Bob | Apple | true | false | Apple |
| d5f9c639-13e6-42c1-9043-30783981724b | Kate | HP | false | false | HP |
SQL Error [42601]: ERROR: INSERT has more target columns than expressions.
WHERE: PL/pgSQL function creator(uuid,character varying[],character varying[]) line 3 at SQL statement.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question