简体   繁体   English

从PostgreSQL表中提取特定的列并对其值进行更新

[英]Extract specific Columns from PostgreSQL table and Do an update on its values

I have a PostgreSQL database and I need to do an update over values of specific Columns. 我有一个PostgreSQL数据库,我需要对特定列的值进行更新。 The number of columns is so big and I need to do the same operation to different table So better to extract them dynamically. 列数很大,我需要对不同的表执行相同的操作,因此最好动态提取它们。

More specifically I want to extract from the table all the columns whose names ends with "_suffix" and do an update on their values. 更具体地说,我想从表中提取名称以“ _suffix”结尾的所有列,并对它们的值进行更新。 I started trying to make a script but I don't know if it is the right road! 我开始尝试编写脚本,但我不知道这是否是正确的道路!

 SELECT columns.column_name
 FROM information_schema.columns
 WHERE columns.table_name = 'myInitialTable' AND columns.column_name like '%\_suffix%' AND columns.table_schema = 'public';

I created a view of this query and I used it in the following function : 我创建了此查询的视图,并在以下函数中使用了它:

CREATE OR REPLACE FUNCTION updatetable() RETURNS int4 AS 
$BODY$
DECLARE r RECORD;
BEGIN
    FOR r IN SELECT * from v_reduced_table LOOP
    update myInitialTable 
    set r.column_name = case
            when r.column_name = '' then NULL
            when r.column_name = 'value1' or r.column_name = 'value2' then  'xxxxx'
            else r.column_name end;
END LOOP;
return 1;
END;
$BODY$
LANGUAGE plpgsql;

SELECT updatetable() as output;

this query do a loop on every column ending with suffix and updates its values. 该查询在以后缀结尾的每一列上执行循环,并更新其值。 but when I run it I get 但是当我运行它时,我得到了

ERROR:  syntax error at or near "$1"
LINE 1: update myInitialTable set  $1  = case when  $2  = '' then NULL when  ...

Any help is appreciated :) 任何帮助表示赞赏:)

In your function you need to use dynamic commands . 在您的函数中,您需要使用动态命令 The funcion format() is often very helpful. 函数format()通常非常有帮助。

Example data: 示例数据:

create table my_table(col1_suffix text, col2_suffix text, col3_suffix text);
insert into my_table values ('a', 'b', 'c');

Example function: 示例功能:

CREATE OR REPLACE FUNCTION update_my_table() RETURNS void AS 
$BODY$
DECLARE r RECORD;
BEGIN
    FOR r IN
        SELECT columns.column_name
        FROM information_schema.columns
        WHERE columns.table_name = 'my_table' 
        AND columns.column_name like '%\_suffix%' 
        AND columns.table_schema = 'public'
    LOOP
        EXECUTE(FORMAT($f$
            UPDATE my_table
            SET %s = CASE
                WHEN '%s' = 'col1_suffix' THEN 'col1'
                WHEN '%s' = 'col2_suffix' OR '%s' = 'col3_suffix' THEN 'xxxxx'
            END;$f$, r.column_name, r.column_name, r.column_name, r.column_name));
    END LOOP;
END;
$BODY$
LANGUAGE plpgsql;

Usage: 用法:

select update_my_table();
select * from my_table;

 col1_suffix | col2_suffix | col3_suffix 
-------------+-------------+-------------
 col1        | xxxxx       | xxxxx
(1 row)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Postgresql中加入2表,但另一个表将仅提取其列 - Join 2 Table in postgresql but the other table only will extract its columns 如何在不将TABLE1的列设置为特定的单个select语句的情况下,使用TABLE2中的值更新TABLE1? - How do I update TABLE1 with values from TABLE2 without setting columns from TABLE1 to specific, individual select statements? 使用self中的值更新PostgreSQL表 - Update PostgreSQL table with values from self 如何将不同表中的特定列添加到 postgresql 中的现有表中? - How do I add specific columns from different tables onto an existing table in postgresql? 如果列与PostgreSQL匹配,则使用来自另一个表的数据更新表的列 - Update a column of a table with data from another table if columns match PostgreSQL PostgreSQL- Function 在表 A 上更新表 B 的 3 个共享列 - PostgreSQL- Function to update 3 shared columns from table B on table A 如何使用 Postgresql 将列更新为另一个表中的值列表? - How do I update a column to be a list of values from another table using Postgresql? 迭代更新表中的特定列(执行批量更新) - Update specific columns in a table iteratively (Do a bulk update) 更新查询,该查询将使用同一表的另一条记录中的值设置记录中的特定列 - Update query that will set specific columns in a record with values from another record of same table 如何从分组行中提取值到 Oracle SQL 中的特定列? - How to extract values from grouped rows to specific columns in Oracle SQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM