Today we are going to discuss with you two examples of Oracle for in loop. I saw the relevant information about the two examples of Oracle for in loop on the relevant website two days ago, I think it will be helpful to the computer community in practice and I will share it with you.
Oracle for in loop one:
1. Build the environment
- create table ETL_DATE(DATE_CHAR VARCHAR2(8),DATE_DATE DATE);
- select * from etl_date;
2. Code
- 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. Delete the environment
- drop table etl_date;
Note: It is only applicable to cycles within January.
Oracle for in loop II:
1. Build the environment
- 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. Code
- 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. Clear the environment
- drop table SQLTEXT;
- drop table HZ;
- powershell
The above content is an introduction to the Oracle for in loop examples. I hope you will have some gains.