谁能告诉我为什么不将此函数插入表中?
CREATE OR REPLACE FUNCTION insert_one(_temp VARCHAR(1000))
RETURNS void AS $$
DECLARE
TEMP INT := NULL;
BEGIN
SELECT "temptable"."id" INTO TEMP FROM "temptable" WHERE "tmpstr" = _temp;
IF TEMP IS NULL THEN
INSERT INTO "temptable"("tmpstr") values(_temp);
SELECT CURRVAL("id") FROM "temptable" INTO TEMP;
END IF;
END;
$$ LANGUAGE plpgsql;
答案 0 :(得分:2)
您可能会遇到错误,因为没有名为id
的序列。
您可能正在寻找
INSERT INTO temptable (tmpstr)
VALUES (_temp)
ON CONFLICT DO NOTHING
RETURNING id;