postgesql 9.6.17
[Renaming multiple columns in PostgreSQL中有结果行
它们包含一些命令,例如
alter table .....
alter table .....
如何立即在sql中执行它们,如
SELECT
EXEC SQL 'ALTER TABLE ' || tab_name || ' RENAME COLUMN '
|| quote_ident(column_name) || ' TO '
|| lower(quote_ident( column_name)) || ';' commit
FROM (
SELECT
quote_ident(table_schema) || '.' || quote_ident(table_name) as tab_name,
column_name
FROM information_schema.columns
WHERE
table_schema = 'public'
) sub;
但示例↑失败
您可以使用DO语句执行此操作。如下所示:
DO $$
DECLARE rec TEXT;
BEGIN
FOR r in SELECT
'ALTER TABLE ' || tab_name || ' RENAME COLUMN '
|| quote_ident(column_name) || ' TO '
|| lower(quote_ident( column_name))
FROM (
SELECT
quote_ident(table_schema) || '.' ||
quote_ident(table_name) as tab_name,
column_name
FROM information_schema.columns
WHERE
table_schema = 'public'
) sub
LOOP
EXECUTE rec;
END LOOP;
END $$;
更多详细信息,请参见以下链接:https://www.postgresql.org/docs/9.6/sql-do.html