有没有办法通过使用 select 语句(或以其他方式动态生成)来生成注释文本?
背景:
我有一个相当大的查询存储在表 table_a 中以便重用它。在表的注释中,我存储了创建日期(以及数据状态的日期)。我使用 DbVisualizer (DbVisualizer-Variables) 的功能生成此日期::
comment on table table_a is 'Date: ${dbvis-date}$'
现在我使用这个table_a来计算一些东西并将其存储在表table_b中。
我现在想把table_a的注释作为table_b的注释。 可以按照How to retrieve the comment of a PostgreSQL database?中的描述获取评论。 :
select description from pg_description
join pg_class on pg_description.objoid = pg_class.oid
where relname = 'table_a'
所以,如果我可以在 comment
中使用 select-statement,这将是一件容易的事情 - 但显然这是不允许的......
--doesn't work
comment on 'table_b' is (select description from pg_description
join pg_class on pg_description.objoid = pg_class.oid
where relname = 'table_a')
我使用 Postgresql 9.1
最佳答案
您可以在 anonymous block 内使用 EXECUTE
:
DO
$$
DECLARE
comment_text text;
BEGIN
comment_text := (
select description
from pg_description
join pg_class on pg_description.objoid = pg_class.oid
where relname = 'table_a');
EXECUTE('COMMENT ON TABLE table_b IS ''' || comment_text || '''');
END;
$$;
关于sql - 有什么方法可以使用 select 语句生成 postgres 注释吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28501396/