我有一个问题,将选定的多行存储在变量(或我不知道的表变量)中。我认为有可能使其工作。这是我的代码
SELECT RECIPIENT, COUNT (*) N_CNT
FROM (SELECT RECIPIENT,
SERVICE_TYPE,
MPS_SWTCH_CD,
MPS_FTR_CD1
FROM ABA_RM_INB_USAGE
WHERE SERVICE_TYPE = 100
OR SERVICE_TYPE = 0
AND MPS_SWTCH_CD = 'T11'
AND MPS_FTR_CD1 = 1033)
GROUP BY RECIPIENT
此查询显示已过滤的行,我需要将该行插入到另一个表中。对不起,英语不好
答案 0 :(得分:1)
我不确定,您想要实现什么。
如果您要从一个表中进行选择并插入到另一个表中,则可以在您的选择周围包裹一个插入语句:
INSERT INTO TABLE_RECIPIENT_COUNT
SELECT RECIPIENT, COUNT (*) N_CNT
FROM (SELECT RECIPIENT,
SERVICE_TYPE,
MPS_SWTCH_CD,
MPS_FTR_CD1
FROM ABA_RM_INB_USAGE
WHERE SERVICE_TYPE = 100
OR SERVICE_TYPE = 0
AND MPS_SWTCH_CD = 'T11'
AND MPS_FTR_CD1 = 1033)
GROUP BY RECIPIENT;
如果要在plsql脚本(或过程..)中执行此操作,则可以定义类型或遍历游标。这是一个如何遍历游标并处理数据的示例:
DECLARE
CURSOR cur
IS
SELECT RECIPIENT, COUNT (*) N_CNT
FROM (SELECT RECIPIENT,
SERVICE_TYPE,
MPS_SWTCH_CD,
MPS_FTR_CD1
FROM ABA_RM_INB_USAGE
WHERE SERVICE_TYPE = 100
OR SERVICE_TYPE = 0
AND MPS_SWTCH_CD = 'T11'
AND MPS_FTR_CD1 = 1033)
GROUP BY RECIPIENT;
BEGIN
FOR item IN cur -- if you realy want to insert all results you should read about BULK-COLLECT and FORALL ;-)
LOOP
dbms_output.put_line('Result: ' || item.RECIPIENT || ', ' || item.N_CNT);
INSERT INTO TABLE_RECIPIENT_COUNT
VALUES (item.RECIPIENT, item.N_CNT);
END LOOP loop_emp;
END;