sql - For循环遍历临时表

标签 sql postgresql plpgsql

我在函数(postgresql)中有一个临时表

create temporary table temp_table (
id serial
,breakup_id integer
,enquiry_id integer
,estimate_id integer
,customer_name CHARACTER VARYING
,month integer
,year integer
,amount numeric
) on commit drop;

我需要for循环这个临时表来使用breakup_id更新amount列。如何在postgresql函数中做到这一点?

最佳答案

如果您对金额值有一些复杂的逻辑,请使用

do 
$$ 
declare _r record; 
begin 
  for _r in (select * from temp_table) loop 
    update temp_table set amount='complicated calculated values' where id = _r.id; 
  end loop; 
end; 
$$
;

否则使用UPDATE temp_table set amount =/*简单值*/where id=..

最后 - 请记住临时表并不意味着保存数据 - 例如,您将无法使用其他后端从中读取数据......

关于sql - For循环遍历临时表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41954473/

相关文章:

sql - 如何从具有不同条件的相同数据的联合中获得单个结果

sql - Activeadmin 自定义过滤器失败

mysql - 如何避免添加重复的外键约束

sql - 使用一个查询中的 id 值到另一表中具有相同 id 的对应列

postgresql - 不同的 PostgreSQL 版本

sql - PostgreSQL CASE 语句

sql - 在 PostgreSQL 中隔开整数值

linux - 无法连接到服务器 : postgresql on ubuntu in windows subsystem for linux

sql - 在 PL/PgSQL EXECUTE 动态查询中正确插入文字

python - Postgresql hmac-sha1 签名与 python 签名不同