2012-12-08 30 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