添加记录后从函数返回错误

时间:2019-03-18 09:19:24

标签: postgresql

我要添加评论,您需要返回添加的评论的号码

DO
    $do$
        BEGIN
            IF EXISTS (SELECT * FROM COMMENTS WHERE PARENT_ID = '791') THEN 
                UPDATE COMMENTS SET HAS_CHILD = TRUE WHERE COMMENT_ID = '791';
            END IF;
            RETURN QUERY
            INSERT INTO COMMENTS(USER_ID, PRODUCT_ID, PARENT_ID, REPLY_ID, USER_NAME, USER_AVATAR, USER_COMMENT, DATE_COMMENT, HAS_CHILD)
                VALUES('7', '40', 791, 791, 'Name', 'https://...', 'like product', '1552899310', FALSE)
                    RETURNING COMMENT_ID;
        END
    $do$

但是我收到错误消息ERROR: cannot use RETURN QUERY in a non-SETOF function

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

您的函数不应返回查询,而应返回一个变量:

INSERT INTO COMMENTS(USER_ID, PRODUCT_ID, PARENT_ID, REPLY_ID, USER_NAME, USER_AVATAR, USER_COMMENT, DATE_COMMENT, HAS_CHILD)
VALUES('7', '40', 791, 791, 'Name', 'https://...', 'like product', '1552899310', FALSE)
RETURNING COMMENT_ID INTO var;

RETURN var;