sql - 带光标的 Redshift 存储过程循环
问题描述
我正在尝试遍历表的所有行t1
并在将行插入新表之前逐行应用一些逻辑member_visits
:
as
$$
DECLARE
cur cursor for
select prs_nat_key,pos_cd,pos_nm, serv_from_dt, serv_to_dt
from t1
order by prs_nat_key, serv_from_dt, serv_to_dt;
counter int;
prs_nat_key varchar;
pos_cd varchar;
pos_nm varchar;
serv_from_date date;
serv_to_date date;
BEGIN
counter = 1;
drop table if exists member_visits;
CREATE TEMP TABLE member_visits(prs_nat_key varchar, pos_cd varchar, pos_nm varchar,
serv_from_date varchar, serv_to_date varchar, visit_index int);
WHILE cur: -- loop through cursor until no more rows in table to loop through
(prs_nat_key, pos_cd, pos_nm, serv_from_date, serv_to_date) = fetch next from cur; -- assign variables the values of the next iteration
/* apply some logic here that adjusts the counter variable, serv_from_date, serv_to_date based on values from the cursor and exisiting values within member_visits*/
INSERT member_visits VALUES (prs_nat_key, pos_cd, pos_nm, serv_from_date, serv_to_date, counter);
--maybe end while loop?
END;
$$
language plpgsql;
关于如何做到这一点的任何建议?
解决方案
推荐阅读
- javascript - JSON自动解析为Javascript中的对象
- python - 如何在运行 QMainWindow 时运行 QColorDialog 窗口?
- c# - 如何处理用户单击导致 Console.WriteLine 挂起的 C# 控制台应用程序窗口
- javascript - 是否有等效于 php 的 strtok 函数的 javascript?
- angularjs - “未能实例化模块 jobApp 由于:错误:[$injector:modulerr]”
- ruby-on-rails - 如何绕过 Rails 服务器对 js 运行时的限制?
- meteor - Meteor Blaze 在表格中显示对象的动态值
- r - 从相关矩阵中过滤 p 值而不丢失行名和列名
- r - 按下操作按钮后清空由 reactiveValues() 创建的数据框
- arrays - 为什么将可分配数组作为自动数组传递给形状错误的子例程时,gfortran 的运行时边界检查不起作用?