使用变量列名称更新 Table 的存储过程
Stored Procedure to Update Table with a Variable Column Name
我正在尝试在 plpgsql 中编写存储过程来简单地更新 table 中的整数值。我有一个 table(称为条件),其中包含四列整数类型,称为结构、缺失、修复和化妆品,它们接受 0、1、0r 2(请参阅下面的 table)。
id | factor_id | structure | missing | repair | cosmetic
----+-----------+-----------+---------+--------+----------
2 | 4 | 0 | 0 | 1 | 2
3 | 5 | 0 | 0 | 0 | 0
4 | 6 | 0 | 0 | 0 | 1
5 | 7 | 1 | 0 | 0 | 1
6 | 8 | 1 | 0 | 0 | 1
我想编写一个更新程序来处理这三列,因此我需要 运行 一些动态 sql。这是更新声明:
UPDATE conditions SET (structure/missing/repair/cosmetic) = 1 WHERE id = 2 AND factor_id = 4;
根据我看到的帖子,我用EXECUTE格式函数写了这段代码:
CREATE OR REPLACE PROCEDURE spup_conditions
(
_col VARCHAR,
_parcel INT,
_factor INT,
_value INT
)
AS $$
DECLARE
_colval text := _col;
BEGIN
EXECUTE
format('UPDATE conditions SET %I = WHERE parcel_id = AND
factor_id = ', _colval)
USING _value, _parcel, _factor;
COMMIT;
END;
$$ LANGUAGE plpgsql;
该过程被 PostgreSQL 接受,但是当我调用它时
CALL spup_conditions('cosmetic', 1, 2, 4)
PostgreSQL 说调用了该过程,但是当我查看 table 时,该值没有改变。感谢您的帮助。
更新
这是我修改后的 table 和程序以及我添加的另外两个操作
id | parcel_id | factor_id | visible | structure | missing | repair | cosmetic | status | completed
----+-----------+-----------+---------+-----------+---------+--------+----------+--------+-----------
1 | 71 | 3 | f | 0 | 0 | 1 | 0 | f |
2 | 71 | 4 | f | 0 | 0 | 1 | 2 | |
3 | 71 | 5 | f | 0 | 0 | 0 | 0 | |
4 | 71 | 6 | f | 0 | 0 | 0 | 1 | |
5 | 71 | 7 | f | 1 | 0 | 0 | 1 | |
6 | 71 | 8 | f | 1 | 0 | 0 | 1 | |
7 | 71 | 9 | f | 0 | 0 | 0 | 1 | |
8 | 71 | 10 | f | 0 | 0 | 1 | 2 | |
9 | 71 | 11 | f | 0 | 0 | 0 | 1 | |
10 | 71 | 12 | t | 0 | 0 | 0 | 0 | |
11 | 71 | 13 | f | 0 | 0 | 0 | 1 | |
12 | 71 | 14 | f | 0 | 0 | 0 | 1 | |
13 | 71 | 15 | f | 0 | 0 | 0 | 0 | |
14 | 71 | 16 | f | 0 | 0 | 0 | 1 | |
CREATE OR REPLACE PROCEDURE blight.spup_conditions
(
_col text,
_value INT,
_parcel INT,
_factor INT
)
AS $$
DECLARE
_colval text := _col;
BEGIN
EXECUTE
format('UPDATE blight.conditions SET %I =
WHERE parcel_id = AND factor_id = ', _colval)
USING _value, _parcel, _factor;
EXECUTE
format('UPDATE blight.conditions SET status = FALSE
WHERE parcel_id = AND factor_id = ')
USING _parcel, _factor;
EXECUTE
format('update blight.conditions set completed = NULL
WHERE parcel_id = AND factor_id = ')
USING _parcel, _factor;
COMMIT;
END;
$$ LANGUAGE plpgsql;
如果您添加参数名称,您将看到您的程序正在尝试执行以下更新
CALL spup_conditions('cosmetic', 1, 2, 4);
CALL spup_conditions(
_col => 'cosmetic',
_parcel => 1,
_factor => 2,
_value => 4);
UPDATE conditions
SET cosmetic = 4
WHERE parcel_id = 1
AND factor_id = 2;
您的 table 中没有符合这些条件的行。
您的过程和调用中有 2 个错误。
- where 子句没有 parcel_id 列 - 更改为 id
format('UPDATE conditions_so SET %I = WHERE id = AND factor_id = ', _colval)
- 对于您的测试数据,没有 id=1 的行
call spup_conditions('cosmetic', 2, 4, 4);
我正在尝试在 plpgsql 中编写存储过程来简单地更新 table 中的整数值。我有一个 table(称为条件),其中包含四列整数类型,称为结构、缺失、修复和化妆品,它们接受 0、1、0r 2(请参阅下面的 table)。
id | factor_id | structure | missing | repair | cosmetic
----+-----------+-----------+---------+--------+----------
2 | 4 | 0 | 0 | 1 | 2
3 | 5 | 0 | 0 | 0 | 0
4 | 6 | 0 | 0 | 0 | 1
5 | 7 | 1 | 0 | 0 | 1
6 | 8 | 1 | 0 | 0 | 1
我想编写一个更新程序来处理这三列,因此我需要 运行 一些动态 sql。这是更新声明:
UPDATE conditions SET (structure/missing/repair/cosmetic) = 1 WHERE id = 2 AND factor_id = 4;
根据我看到的帖子,我用EXECUTE格式函数写了这段代码:
CREATE OR REPLACE PROCEDURE spup_conditions
(
_col VARCHAR,
_parcel INT,
_factor INT,
_value INT
)
AS $$
DECLARE
_colval text := _col;
BEGIN
EXECUTE
format('UPDATE conditions SET %I = WHERE parcel_id = AND
factor_id = ', _colval)
USING _value, _parcel, _factor;
COMMIT;
END;
$$ LANGUAGE plpgsql;
该过程被 PostgreSQL 接受,但是当我调用它时
CALL spup_conditions('cosmetic', 1, 2, 4)
PostgreSQL 说调用了该过程,但是当我查看 table 时,该值没有改变。感谢您的帮助。
更新 这是我修改后的 table 和程序以及我添加的另外两个操作
id | parcel_id | factor_id | visible | structure | missing | repair | cosmetic | status | completed
----+-----------+-----------+---------+-----------+---------+--------+----------+--------+-----------
1 | 71 | 3 | f | 0 | 0 | 1 | 0 | f |
2 | 71 | 4 | f | 0 | 0 | 1 | 2 | |
3 | 71 | 5 | f | 0 | 0 | 0 | 0 | |
4 | 71 | 6 | f | 0 | 0 | 0 | 1 | |
5 | 71 | 7 | f | 1 | 0 | 0 | 1 | |
6 | 71 | 8 | f | 1 | 0 | 0 | 1 | |
7 | 71 | 9 | f | 0 | 0 | 0 | 1 | |
8 | 71 | 10 | f | 0 | 0 | 1 | 2 | |
9 | 71 | 11 | f | 0 | 0 | 0 | 1 | |
10 | 71 | 12 | t | 0 | 0 | 0 | 0 | |
11 | 71 | 13 | f | 0 | 0 | 0 | 1 | |
12 | 71 | 14 | f | 0 | 0 | 0 | 1 | |
13 | 71 | 15 | f | 0 | 0 | 0 | 0 | |
14 | 71 | 16 | f | 0 | 0 | 0 | 1 | |
CREATE OR REPLACE PROCEDURE blight.spup_conditions
(
_col text,
_value INT,
_parcel INT,
_factor INT
)
AS $$
DECLARE
_colval text := _col;
BEGIN
EXECUTE
format('UPDATE blight.conditions SET %I =
WHERE parcel_id = AND factor_id = ', _colval)
USING _value, _parcel, _factor;
EXECUTE
format('UPDATE blight.conditions SET status = FALSE
WHERE parcel_id = AND factor_id = ')
USING _parcel, _factor;
EXECUTE
format('update blight.conditions set completed = NULL
WHERE parcel_id = AND factor_id = ')
USING _parcel, _factor;
COMMIT;
END;
$$ LANGUAGE plpgsql;
如果您添加参数名称,您将看到您的程序正在尝试执行以下更新
CALL spup_conditions('cosmetic', 1, 2, 4);
CALL spup_conditions(
_col => 'cosmetic',
_parcel => 1,
_factor => 2,
_value => 4);
UPDATE conditions
SET cosmetic = 4
WHERE parcel_id = 1
AND factor_id = 2;
您的 table 中没有符合这些条件的行。
您的过程和调用中有 2 个错误。
- where 子句没有 parcel_id 列 - 更改为 id
format('UPDATE conditions_so SET %I = WHERE id = AND factor_id = ', _colval)
- 对于您的测试数据,没有 id=1 的行
call spup_conditions('cosmetic', 2, 4, 4);