1

私はここでかなり立ち往生しています。可能であれば、いくつかのガイダンスが必要です!

私はこのテーブルを持っています:

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つずつ行う方法を知っていますが、何らかの形でプロシージャに入れるためにここでいくつかの反復が必要なので、呼び出すとすぐにこれらの行を増やすことができます!

4

2 に答える 2

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;
于 2012-12-08T09:25:07.907 に答える
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
于 2012-12-08T08:56:21.967 に答える