简体   繁体   English

如何基于Postgres中的列名更新列?

[英]How to update column based on column name in postgres?

I've narrowed it down to two possibilities - DynamicSQL and using a case statement. 我将其缩小为两种可能性-DynamicSQL和使用case语句。

However, I've failed with both of these. 但是,我都失败了。

I simply don't understand dynamicSQL, and how I would use it in my case. 我只是不了解dynamicSQL,以及我将如何使用它。

This is my attempt using case statements; 这是我使用case语句的尝试; one of many failed variations. 许多失败的变体之一。

SELECT column_name,
CASE WHEN column_name = 'address' THEN (**update statement gives syntax error within here**)
END
FROM information_schema.columns
WHERE table_name = 'employees';

As an overview, I'm using Axios to talk to my Node server, which is making calls to my Heroku database using Massivejs. 作为概述,我正在使用Axios与我的Node服务器通信,后者使用Massivejs调用Heroku数据库。

Maybe this isn't the way to go - so here's my main problem: 也许这不是要走的路,所以这是我的主要问题:

I've ran into troubles because the values I'm planning on using as column names are sent to my server as strings. 我遇到了麻烦,因为我计划用作列名的值以字符串形式发送到我的服务器。 The exact call that I've been trying to use is 我一直在尝试使用的确切电话是

update employees
set $1 = $2
where employee_id = $3;

Once again, I'm passing into those using massive. 再一次,我要传给那些使用大量软件的人。

I get the error back { error: syntax error at or near "'address'"} because my incoming values are strings. 因为我的输入值是字符串,所以我得到了错误信息{ error: syntax error at or near "'address'"} My thought process was that the above statement would allow me to use variables because 'address' is encapsulated by quotes. 我的想法是,上面的语句将允许我使用变量,因为'address'由引号引起来。

But alas, my thought process has failed me. 但是a,我的思考过程使我失败了。

This seems to be close to answering my question, but I can't seem to figure out what to do in my case if using dynamic SQL. 这似乎很接近回答我的问题,但是如果使用动态SQL,我似乎无法弄清楚该怎么办。

How to use dynamic column names in an UPDATE or SELECT statement in a function? 如何在函数的UPDATE或SELECT语句中使用动态列名?

Thanks in advance. 提前致谢。

I will show you a way to do this by using a function. 我将向您展示一种使用函数的方法。 First we create the employees table : 首先,我们创建employee表:

CREATE TABLE employees(
id BIGSERIAL PRIMARY KEY,
column1 TEXT,
column2 TEXT
);

Next, we create a function that requires three parameters: 接下来,我们创建一个需要三个参数的函数:

columnName - the name of the column that needs to be updated columnName需要更新的列的名称

columnValue - the new value to which the column needs to be updated columnValue列需要更新为的新值

employeeId - the id of the employee that will be updated employeeId将更新的员工的ID

By using the format function we generate the update query as a string and use the EXECUTE command to execute the query. 通过使用format函数,我们将更新查询生成为字符串,并使用EXECUTE命令执行查询。

Here is the code of the function. 这是函数的代码。

CREATE OR REPLACE FUNCTION update_columns_on_employee(columnName TEXT, columnValue TEXT, employeeId BIGINT)
  RETURNS VOID AS
  $$
  DECLARE update_statement TEXT := format('UPDATE EMPLOYEES SET %s = ''%s'' WHERE id = %L',columnName, columnValue, employeeId);
  BEGIN
    EXECUTE update_statement;
end;
$$ LANGUAGE plpgsql;

Now, lets insert some data into the employees table 现在,让我们将一些数据插入到employees表中

  INSERT INTO employees(column1, column2) VALUES ('column1_start_value','column2_start_value');

So now we currently have an employee with an id value of 1 who has 'column1_start_value' value for the column1 , and 'column2_start_value' value for column2 . 因此,现在我们当前有一个id值为1的员工,其column1的值为“ column1_start_value”, column2的值为“ column2_start_value”。

If we want to update the value of column2 from 'column2_start_value' to 'column2_new_value' all we have to do is execute the following call 如果我们想将column2的值从'column2_start_value'更新为'column2_new_value',我们要做的就是执行以下调用

SELECT * FROM update_columns_on_employee('column2','column2_new_value',1);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM