Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
891 views
in Technique[技术] by (71.8m points)

postgresql - Inserting DEFAULT value into a column when a parameter is NULL

I would like to write a stored procedure like this:

CREATE OR REPLACE FUNCTION my_function(param_1 text, param_2 text DEFAULT NULL::text) RETURNS bigint AS
$$
DECLARE ret bigint;
BEGIN
    INSERT INTO my_table(val_1, val_2) VALUES (param_1, param_2);

    -- do some more stuff

    RETURN ret;
END;
$$
LANGUAGE plpgsql;

However, I would like to use val_2 column's DEFAULT value instead of NULL - if NULL is provided as the param_2 value.

Something like this:

INSERT INTO my_table(val_1, val_2) VALUES (param_1, COALESCE(param_2, DEFAULT));

is obviously wrong, since the INSERT statement specification explicitly states an expression OR DEFAULT can be used, DEFAULT itself is not available in expressions.

I found two solutions myself but I'm not satisfied with them.

  1. Select the DEFAULT value from the information schema and use it in the COALESCE expression.

I'm no expert but it seems like there should be a simpler and more elegant way to do it.

  1. Use INSERT and then UPDATE

Like this:

-- ...
INSERT INTO my_table(val_1) VALUES (param_1)
RETURNING id INTO id_var;

IF (param_2) IS NOT NULL THEN
    UPDATE my_table SET val_2 = param_2 WHERE id = id_var;
END IF;
-- ...

There is however a catch in this solution. The actual table of the production system has some intricate triggers which run on UPDATE statements on this table so I would generally like to avoid using updates if possible.

Generally, I'll possibly stick to the second solution but that would possibly require adding some hacks to the aforementioned triggers. But if there is a way to avoid this - I will be very grateful for pointing it out.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is a bit long for a comment.

One method would be to declare the column NOT NULL. Inserting the NULL value would generate a constraint violation, which you can catch in the insert using on constraint.

This seems like a correct approach. If the column has a default value, then you probably do not want it to be NULL.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

2.1m questions

2.1m answers

60 comments

56.6k users

...