PostgreSQL在函数中声明了变量

时间:2013-10-29 08:45:13

标签: database function postgresql variables

我使用Potgresql并编写了函数

CREATE OR REPLACE FUNCTION staging.shape_commit(layer_id integer)
  RETURNS integer AS
$BODY$
declare     
  layer_name text;

begin
layer_name:=(select shape.layer_name from staging.shape where shape.id=layer_id);
delete  from layer_name;
return layer_name;
end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION staging.shape_commit(integer)
  OWNER TO test;

layer_name是变量,实际上它是表名。但是脚本将其忽略为变量并用作字符串

Cam有人帮帮我吗?

1 个答案:

答案 0 :(得分:0)

您的问题不是很明确,但我怀疑您确实希望将文本转换为表名。为此,您需要动态SQL。而不是:

delete  from layer_name;

尝试更多类似的内容:

execute $x$delete from $x$ || layer_name::regclass;

http://www.postgresql.org/docs/current/static/plpgsql-statements.html