0

I want to create a new table with plpgsql, but I want that the user gives the name of the table.My code generate a table with the name of tbname

this is my code:

CREATE OR REPLACE FUNCTION "Object_gen"(tablename text)
  RETURNS void AS
$BODY$DECLARE
tbname text;
BEGIN
tbname:=tablename;
CREATE TABLE tbname(Surrogat uuid);
END$BODY$
  LANGUAGE plpgsql;

How can I solve my problem? somehow that the name of my table comes from the user

4

1 回答 1

0

您需要使用动态 SQL:

CREATE OR REPLACE FUNCTION "Object_gen"(tablename text)
  RETURNS void AS
$BODY$
BEGIN
  execute 'CREATE TABLE '||tablename||' (Surrogat uuid)';
END
$BODY$
LANGUAGE plpgsql;
于 2013-01-08T15:07:17.323 回答