Creation process of Oracle for in loop

Source: Internet
Author: User

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

 
 
  1. create table ETL_DATE(DATE_CHAR VARCHAR2(8),DATE_DATE DATE);  
  2. select * from etl_date;  

2. Code

 
 
  1. DECLARE  
  2. v_date date;  
  3. BEGIN  
  4. EXECUTE IMMEDIATE 'truncate table etl_date';  
  5. for v_date in 20091001 .. 20091021 LOOP  
  6. INSERT INTO etl_date  
  7. (date_char, date_date)  
  8. SELECT v_date, to_date(v_date, 'YYYY-MM-DD') FROM dual;  
  9. END LOOP;  
  10. COMMIT;  
  11. END;  
  12. select * from etl_date;  

3. Delete the environment

 
 
  1. drop table etl_date; 

Note: It is only applicable to cycles within January.

Oracle for in loop II:

1. Build the environment

 
 
  1. create table SQLTEXT(TEXT VARCHAR2(100));  
  2. create table HZ(HZ_NAME VARCHAR2(3));  
  3. INSERT INTO hz(HZ_NAME)values(' ');  
  4. INSERT INTO hz(HZ_NAME)values('PRE');  
  5. INSERT INTO hz(HZ_NAME)values('CUR');  
  6. INSERT INTO hz(HZ_NAME)values('INS');  
  7. INSERT INTO hz(HZ_NAME)values('UPD');  
  8. select * from HZ;  

2. Code

 
 
  1. declare  
  2. P_TABLE_NAME varchar2(100) := 'CFA';  
  3. begin  
  4. for HZ in (select HZ_NAME from HZ) LOOP  
  5. insert into sqltext  
  6. select 'CREATE TABLE ' || REPLACE(P_TABLE_NAME, 'EDW', 'TMP') ||  
  7. HZ.HZ_NAME || ' AS select * from ' || P_TABLE_NAME ||  
  8. ' where ROWNUM<1'  
  9. from dual;  
  10. END LOOP;  
  11. end;  
  12. select * from SQLTEXT;  

3. Clear the environment

 
 
  1. drop table SQLTEXT;  
  2. drop table HZ;  
  3. powershell   

The above content is an introduction to the Oracle for in loop examples. I hope you will have some gains.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.