insert using condition postgres 1

insert using condition postgres

WITH get_cust_code_for_cust_id AS (
    SELECT cust_code FROM cust WHERE cust_id=11

INSERT INTO public.table_1(
    cust_code, issue, status, created_on)
    VALUES (SELECT cust_code FROM get_cust_code_for_cust_id, 'New Issue', 'Open', current_timestamp)

Here is what the above code is Doing:
1. It’s creating a CTE (Common Table Expression) called get_cust_code_for_cust_id.
2. It’s selecting the cust_code from the cust table where the cust_id is 11.
3. It’s inserting the cust_code into the table_1 table.

The above code works fine. However, I’m wondering if there’s a better way to do this.

I’m thinking that I could use a function instead of a CTE.

Here’s what I’m thinking:

Similar Posts