Postgres if语句内部函数插入值

时间:2017-10-16 14:22:54

标签: postgresql if-statement sql-insert plpgsql

是否可以使此功能正常工作?我在Postgres函数中的Insert Into查询中有If语句。

你有什么方法?

    CREATE FUNCTION proc_api_consumer_audit_insert()
    RETURNS TRIGGER AS $api_consumer$
    BEGIN
        INSERT INTO api_consumer_audit(api_consumer_id, change_type, changed_by, changed_date, business_id_old, business_id_new, name_old, name_new, api_callback_url_old, api_callback_url_new, application_base_url_old, application_base_url_new, authorization_callback_url_old, authorization_callback_url_new, status_old, status_new) 
        VALUES(NEW.id, 'INSERT', (IF NEW.created_by=null THEN CURRENT_USER ELSE NEW.created_by END IF;), CURRENT_TIMESTAMP, null, NEW.business_id, null, NEW.name, null, NEW.api_callback_url, null, NEW.application_base_url, null, NEW.authorization_callback_url, null, NEW.status);
        RETURN NEW;
    END;
    $api_consumer$ LANGUAGE plpgsql;

1 个答案:

答案 0 :(得分:2)

SQL中没有<span th:each="n: ${#numbers.sequence(1, 4)}" th:text="${list[n].fieldOne}" /> <span th:each="n: ${#numbers.sequence(5, 8)}" th:text="${list[n].fieldTwo}" /> 。一般情况下,会使用IF表达式,但在您的情况下,CASE要短得多:

coalesce()

与CASE表达式相同的内容如下所示:

INSERT INTO api_consumer_audit(...) 
VALUES (NEW.id, 
        'INSERT', 
        coalesce(new.created_by, current_user),
        ...);