如何在 plpgsql 过程中参数化 DDL 语句?也就是说,我想使用org_label
参数作为角色和模式名称,但下面的代码从字面上理解它,我最终得到了角色“org_label”:
create or replace procedure create_org_schema (org_label varchar(63)) -- max identifier len in pgsql is 63
language plpgsql AS $$ BEGIN
create role org_label IN GROUP organization;
GRANT org_label TO ardoq_api_customers; -- so that AUTHORIZATION... below works
CREATE SCHEMA AUTHORIZATION org_label;
SET ROLE org_label;
GRANT USAGE on SCHEMA org_label to ardoq_api_customers; -- so that we can list it
RESET ROLE;
END $$;
我试图将第一行更改为:
EXECUTE 'create role $1 IN GROUP organization' USING org_label;
但这失败了:
ERROR: syntax error at or near "$1" at character 13 QUERY: create role $1 IN GROUP organization