2012-12-08 28 views
1

我這裏挺困的,如果有可能的話需要一些指導!Oracle SQL小程序實現

我有這個表:

doctor_details (doctor_id, name, surname, date_of_birth) 

,我只是想創建,增加了其30名醫生在以下形式的過程:

doctor_id name surname date_of_birth 
------------------------------------------- 
01   John-01 Surname-01 2001-01-01 
02   John-02 Surname-02 2002-01-01 
03   John-03 Surname-03 2003-01-01 

我知道如何做一個其中一個很明顯,但我需要一些迭代來將它放到一個過程中,所以當我將它稱爲立即增加這些行時!

+1

你必須有一個過程?這可以使用單個SQL語句完成。 –

+0

你究竟試過了什麼?請顯示一些代碼! – Yahia

+0

那麼,事情是,我需要調用一個過程,以便自動執行此操作 – Anysiya

回答

0

下面是測試代碼::

drop table doctor_details; 
    create table doctor_details 
    (doctor_id varchar2(20), 
    name varchar2(100), 
    surname varchar2(100), 
    date_of_birth date); 

    drop sequence doctor_id_seq; 
    create sequence doctor_id_seq 
    start with 1 
    maxvalue 99999999 
    cache 100; 



    alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'; 



     create procedure doctors_details_add as 
    begin 
       for x in 1..30 loop 
       insert into doctor_details (doctor_id, name, surname, date_of_birth) 
       values 
       (lpad(doctor_id_seq.nextval,2,'0'), 
       'John'||'-'||lpad(doctor_id_seq.currval,2,'0'), 
       'Surname'||'-'||lpad(doctor_id_seq.currval,2,'0'), 
       add_months(to_date('2001-01-01','YYYY-MM-DD'),12*doctor_id_seq.currval) 
       ); 
       end loop; 
    end doctors_details_add; 
+0

我收到一個錯誤。順便說一句,我有日期爲DATE類型。這是錯誤:ORA-01858:發現算術字符的非算術字符 ORA-06512:給「doctors_details_add」,第4行 ORA-06512:第2行 過程已退出。 斷開與數據庫的連接 – Anysiya

+0

試試這段代碼請 –

+0

這是一個很好的 – Anysiya

0

你可以用1個sql語句來做到這一點。

SQL> create table doctors(doctor_id number, name varchar2(100), surname varchar2(100), date_of_birth date); 

Table created. 

SQL> create sequence doctor_id_seq start with 1 cache 100; 

Sequence created. 

SQL> insert into doctors 
    2    (doctor_id, name, surname, date_of_birth) 
    3 select doctor_id_seq.nextval, 'John-' || doctor_id_seq.currval, 'Surname-' || doctor_id_seq.currval, to_date('01-jan-2001', 'dd-mon-yyyy') + dbms_random.value(1, 3000) 
    4 from dual 
    5 connect by level <= 30/*rows to gen*/; 

30 rows created. 

SQL> col name format a20 
SQL> col surname format a20 
SQL> select * from doctors; 

DOCTOR_ID NAME     SURNAME    DATE_OF_B 
---------- -------------------- -------------------- --------- 
     2 John-2    Surname-2   09-JAN-01 
     3 John-3    Surname-3   22-FEB-06 
     4 John-4    Surname-4   09-SEP-01 
     5 John-5    Surname-5   17-DEC-01 
     6 John-6    Surname-6   28-JUN-05 
     7 John-7    Surname-7   21-SEP-06 
     8 John-8    Surname-8   16-SEP-02 
     9 John-9    Surname-9   05-MAY-04 
     10 John-10    Surname-10   06-OCT-07 
     11 John-11    Surname-11   05-JUN-02 
     12 John-12    Surname-12   16-NOV-06 
     13 John-13    Surname-13   18-SEP-05 
     14 John-14    Surname-14   16-MAY-06 
     15 John-15    Surname-15   02-OCT-05 
     16 John-16    Surname-16   11-JAN-04 
     17 John-17    Surname-17   01-FEB-08 
     18 John-18    Surname-18   15-FEB-06 
     19 John-19    Surname-19   05-MAY-02 
     20 John-20    Surname-20   15-SEP-02 
     21 John-21    Surname-21   26-NOV-08 
     22 John-22    Surname-22   18-MAR-01 
     23 John-23    Surname-23   03-SEP-01