[英]Argument not taking the value from Postgres function
I have a simple Postgres function where I want to take table_name as a parameter and pass it into an argument and delete the data from table by condition.我有一个简单的 Postgres function,我想将 table_name 作为参数并将其传递给参数并按条件从表中删除数据。
CREATE OR REPLACE FUNCTION cdc.audit_refresh(tablename text)
RETURNS integer AS
$$
BEGIN
delete from tablename where id<4;
RETURN(select 1);
END;
$$ LANGUAGE plpgsql;
select cdc.audit_refresh('cdc.adf_test');
But it throws out an error that tablename ERROR: relation "tablename" does not exist in the delete statement.(refer snapshot)但它抛出一个错误,即表名错误:删除语句中不存在关系“表名”。(参考快照)
What you want to achieve is to execute Dynamic SQL statements.你想要实现的是执行 Dynamic SQL 语句。 You can do this with
EXECUTE
.您可以使用
EXECUTE
此操作。 See more here 在这里查看更多
CREATE OR REPLACE FUNCTION audit_refresh(tablename text)
RETURNS integer AS
$$
DECLARE
stmt TEXT;
BEGIN
stmt = 'delete from '||tablename||' where id<4;';
EXECUTE stmt;
RETURN 1;
END
$$ LANGUAGE plpgsql;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.