从现有表中过滤SQL DDL语句的列名

问题描述 投票:0回答:1

是否有可能在psql中对列名本身进行过滤?我想在单独的架构中按原样生成一个原始版本的有限版本(具有几百列)la(伪代码):

create table why.am_i_doing_this
    select *
    from original.table 
    where column_name_of_the_table not in ('column_1', 'column_2' );
sql postgresql dynamic-sql
1个回答
0
投票

您可以在DO语句中使用动态SQL进行此操作:

DO
$$
BEGIN
   RAISE NOTICE '%', -- for debugging
   -- EXECUTE
   (
   SELECT 'CREATE TABLE why.am_i_doing_this AS SELECT '
       || string_agg(column_name, ', ' ORDER  BY ordinal_position)
       || ' FROM original.table'
   FROM   information_schema.columns
   WHERE  table_schema = 'original'
   AND    table_name = 'table'
   AND    column_name NOT IN ('column_1', 'column_2')
   );
END
$$;

一旦确信DDL语句是好的,请删除该行:

RAISE NOTICE '%', 

并取消引用下面的行:

 EXECUTE

这基于information schema view information_schema.columns。或者,您可以使用information_schema.columns

相关:

© www.soinside.com 2019 - 2024. All rights reserved.