Pass UUID value as a parameter to the function

我是研究僧i 提交于 2019-12-02 08:28:29

You need a simple cast to make sure PostgreSQL understands, what you want to insert:

INSERT INTO testz values(p_id::uuid, p_name); -- or: CAST(p_id AS uuid)

Or (preferably) you need a function, with exact parameter types, like:

CREATE OR REPLACE FUNCTION testz(p_id uuid, p_name text)
RETURNS VOID AS
$BODY$
BEGIN
    INSERT INTO testz values(p_id, p_name);
END;
$BODY$
LANGUAGE PLPGSQL;

With this, a cast may be needed at the calling side (but PostgreSQL usually do better automatic casts with function arguments than inside INSERT statements).

SQLFiddle

If your function is that simple, you can use SQL functions too:

CREATE OR REPLACE FUNCTION testz(uuid, text) RETURNS VOID
LANGUAGE SQL AS 'INSERT INTO testz values($1, $2)';
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!