按月份表 遍历查询并编写存储过程ord_dev_3 新建表ord_dev_3056 并将数据插入 月份表从202206开始 并且月份不大月v_dt 现有代码如下 SELECT dt INTO v_dt FROM (SELECT MAX(to_number(SUBSTR(TABLE_NAME, '15'))) dt FROM SYS.ALL_TABLES a WHERE a.owner = upper('jour1') AND a.table_name LIKE upper('om_subscriber_202___')); SELECT a.order_id, a.done_date, a.cust_id, a.subscriber_ins_id, a.access_num, c.dev_id, c.dev_name, c.corp_org, c.company_name, c.department_code, c.department_name, c.remarks FROM JOUR1.Om_Subscriber_202206 a LEFT JOIN JOUR1.om_order_202206 b ON a.order_id = b.order_id LEFT JOIN params1.sec_developer c ON b.dev_id = c.dev_id WHERE 1 = 1 AND a.access_num LIKE '192%' AND a.action = 0;
时间: 2024-04-13 15:28:19 浏览: 107
你可以按照以下方式编写存储过程 `ord_dev_3`,并创建表 `ord_dev_3056` 来插入从202206开始的月份表数据,直到不大于给定日期 `v_dt`:
```sql
CREATE OR REPLACE PROCEDURE ord_dev_3 AS
v_dt NUMBER;
v_month NUMBER;
BEGIN
SELECT MAX(to_number(SUBSTR(TABLE_NAME, '15')))
INTO v_dt
FROM SYS.ALL_TABLES a
WHERE a.owner = upper('jour1')
AND a.table_name LIKE upper('om_subscriber_202___');
FOR v_month IN 202206..v_dt LOOP
EXECUTE IMMEDIATE 'INSERT INTO ord_dev_3056 (order_id, done_date, cust_id, subscriber_ins_id, access_num, dev_id, dev_name, corp_org, company_name, department_code, department_name, remarks)
SELECT a.order_id,
a.done_date,
a.cust_id,
a.subscriber_ins_id,
a.access_num,
c.dev_id,
c.dev_name,
c.corp_org,
c.company_name,
c.department_code,
c.department_name,
c.remarks
FROM JOUR1.Om_Subscriber_' || v_month || ' a
LEFT JOIN JOUR1.om_order_' || v_month || ' b
ON a.order_id = b.order_id
LEFT JOIN params1.sec_developer c
ON b.dev_id = c.dev_id
WHERE 1 = 1
AND a.access_num LIKE ''192%''
AND a.action = 0';
END LOOP;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Data inserted successfully!');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
ROLLBACK;
END;
/
```
在上述代码中,我们首先查询最大的月份表名,将其转换为数字,并存储在变量 `v_dt` 中。然后,我们使用一个循环来遍历从202206到 `v_dt` 的月份。在每次循环中,我们使用动态 SQL 插入数据到表 `ord_dev_3056` 中,使用对应的月份表。
请确保替换正确的表名和列名,并根据你的需求进行适当的修改。希望这对你有帮助!如果你还有其他问题,请随时提问。
阅读全文