【发布时间】:2018-09-19 10:07:18
【问题描述】:
我有两张桌子。
A表
-------------------------------------------
id | date | shiftId | activityId |
-------------------------------------------
1 2018-09-09 1 100
2 2018-09-09 1 101
表 B
------------------------------------------------------------------
id | reading | resourceId |date | shiftId | activityId
------------------------------------------------------------------
1 10.0 10 2018-09-09 1
2 11.0 11 2018-09-09 1
现在,我想从表 A 更新表 B activityId,在 shiftid 和 shiftDate 上查询表 A。
如何编写存储过程循环更新表B?
我试过下面的查询。
create or replace FUNCTION Update_TableB_ActivityId()
Returns Void as $$
Declare
rec RECORD;
query text;
activityId integer;
BEGIN
query := 'select * from TableB where "activityId" is null;
FOR rec IN execute query
LOOP
execute 'select "activityId" from TableB where "date"=rec."date"' into activityId;
execute 'Update TableB set "activityId"=activityId where "id"=rec."id"';
END LOOP;
END;
$$ LANGUAGE plpgsql;
【问题讨论】:
-
Mysql 还是 postgresql ?到目前为止,您尝试了什么?
-
向我们展示您当前的存储过程尝试。
标签: mysql sql postgresql