更改名为“xxx”的每列

时间:2016-02-24 15:21:12

标签: sql postgresql stored-procedures

我正在尝试创建一个PostgreSQL存储过程,该过程必须将默认值'now()'设置为每个表中名为'CreationDate'的每个列。

CREATE OR REPLACE FUNCTION set_creation_date() RETURNS
void AS $$
DECLARE
     t pg_tables%ROWTYPE;
BEGIN
     FOR t IN SELECT "tablename" FROM pg_tables WHERE "schemaname" = 'public'  LOOP
     IF EXISTS (select * from information_schema.columns where table_name = t."tablename"
    and column_name = 'CreationDate') THEN
    EXECUTE FORMAT('ALTER TABLE %I ALTER COLUMN "CreationDate" SET DEFAULT now()', t."tablename");
    END IF;
     END LOOP;
END;
$$ LANGUAGE plpgsql;

但是没有列受到影响。怎么了?

2 个答案:

答案 0 :(得分:3)

看起来它每次都试图改变t."tablename"。请尝试以下方法:

EXECUTE FORMAT('ALTER TABLE %I ALTER COLUMN "CreationDate" SET DEFAULT now()', t."tablename");

我没有可供测试的PostgreSQL服务器,所以如果语法不正确,请告诉我。

答案 1 :(得分:0)

我终于解决了。错误发生在pg_tables%ROWTYPE中:使用调试器我意识到字段'tablename'不存在,所以我将't'声明为文本。 我创建了一个带参数的函数,您必须传递模式名称和列名。我会在这里发布给任何需要它的人

    DROP FUNCTION IF EXISTS set_creation_date(character varying, character varying);

CREATE OR REPLACE FUNCTION set_creation_date(_schema_name character varying, _column_date character varying)
  RETURNS void AS
$BODY$
DECLARE
     t TEXT;
BEGIN
     FOR t IN SELECT "tablename" FROM pg_tables WHERE "schemaname" = _schema_name  LOOP
     IF EXISTS (select * from information_schema.columns where table_name = t and column_name = _column_date) THEN
    EXECUTE FORMAT('ALTER TABLE %I ALTER COLUMN %I SET DEFAULT now()', t, _column_date);
    END IF;
     END LOOP;
END;
$BODY$
  LANGUAGE plpgsql;