我很困在这里,如果有可能需要一些指导!
我有这张桌子:
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
我知道如何一个接一个地做到这一点,但我需要在这里进行一些迭代以某种方式将它放入一个程序中,所以当我调用它来立即增加这些行时!
答案 0 :(得分: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
答案 1 :(得分: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;