N
N
nurzhannogerbek2019-06-10 05:58:21
PostgreSQL
nurzhannogerbek, 2019-06-10 05:58:21

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            |

I have a procedure that looks like this:
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;

The essence of the procedure is as follows:
1) If the values ​​that we want to add to the table are unique within the SURVEY_ID and EMPLOYEE columns, they must be written to the table.
2) If the values ​​that we want to add to the table are not unique within the SURVEY_ID and EMPLOYEE columns, then we do not need to add them to the table. In this case, you need to update the value of the ORGANIZATION_NAME column in cases where the value in the STATUS column of an existing record is false.
CALL creator(
    'd5f9c639-13e6-42c1-9043-30783981724b',
    ARRAY['Mark', 'Bob', 'Kate'],
    ARRAY['Google', 'Google', 'HP']
);

In other words, by calling this procedure like this, I expected the following result:
| 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                |

When trying to call the procedure in the above way, an error appears:
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

1 answer(s)
D
d-stream, 2019-06-10
@nurzhannogerbek

What will show

SELECT 
      SURVEY_IDENTIFIER AS SURVEY_ID,
      EMPLOYEE FROM UNNEST(ARRAY[EMPLOYEES], ARRAY[ORGANIZATION_NAMES]) u(EMPLOYEE, ORGANIZATION_NAME)

?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question