我们今天是要和大家一起讨论的是Oracle for in loop 两例,我前两天在相关网站看见Oracle for in loop 两例的相关资料,觉得在实际操作中会对计算机一族有所帮助,就拿出来供大家分享。
Oracle for in loop 壹:
1、搭建环境
- create table ETL_DATE(DATE_CHAR VARCHAR2(8),DATE_DATE DATE);
- select * from etl_date;
2、代码
- DECLARE
- v_date date;
- BEGIN
- EXECUTE IMMEDIATE 'truncate table etl_date';
- for v_date in 20091001 .. 20091021 LOOP
- INSERT INTO etl_date
- (date_char, date_date)
- SELECT v_date, to_date(v_date, 'YYYY-MM-DD') FROM dual;
- END LOOP;
- COMMIT;
- END;
- select * from etl_date;
3、删除环境
- drop table etl_date;
注:仅适用于在一月之内的循环。
Oracle for in loop 贰:
1、搭建环境
- create table SQLTEXT(TEXT VARCHAR2(100));
- create table HZ(HZ_NAME VARCHAR2(3));
- INSERT INTO hz(HZ_NAME)values(' ');
- INSERT INTO hz(HZ_NAME)values('PRE');
- INSERT INTO hz(HZ_NAME)values('CUR');
- INSERT INTO hz(HZ_NAME)values('INS');
- INSERT INTO hz(HZ_NAME)values('UPD');
- select * from HZ;
2、代码
- declare
- P_TABLE_NAME varchar2(100) := 'CFA';
- begin
- for HZ in (select HZ_NAME from HZ) LOOP
- insert into sqltext
- select 'CREATE TABLE ' || REPLACE(P_TABLE_NAME, 'EDW', 'TMP') ||
- HZ.HZ_NAME || ' AS select * from ' || P_TABLE_NAME ||
- ' where ROWNUM<1'
- from dual;
- END LOOP;
- end;
- select * from SQLTEXT;
3、清空环境
- drop table SQLTEXT;
- drop table HZ;
- powershell
以上的相关内容就是对Oracle for in loop 两例的介绍,望你能有所收获。
【编辑推荐】