简体   繁体   中英

PLSQL Oracle: How to insert data in a table using stocked procedures

I'm trying to solve a problem by few hours, but I can't handle it. I would appreciate if you could help me with some ideas of solving.

I have 2 tables: 1. table called LASTNAME with one column n(varchar2) which stores some last names; 2. table called FIRSTNAME with one column p(varchar2) which stores some first names. I must generate 2000 combinations of complete names(last names and first names) with the data from this two tables.

My idea was:

select DISTINCT * 
from LASTNAME 
   CROSS JOIN FIRSTNAME 
WHERE ROWNUM <= 2000;  

to generate all combinations.

I must put these 2000 full names in another table called students. I'll give you the describe of students:

Name         Null     Type         
------------ -------- ------------ 
REG_NUMBER  NOT NULL  CHAR(4)      
LASTNAME              VARCHAR2(20) 
FIRSTNAME             VARCHAR2(10) 
YEAR                  NUMBER(1) 
GROUP                 CHAR(2)   
SCHOLARSHIP           NUMBER(6,2) 
DATE_OF_BIRTH         DATE      

For the new students that I'll put in the table students I must generate random values for group (between 1-7), for year(between 1-3) and for scholarship.

All these operations must be done using stored procedures.

You can insert data in your table with a single SQL statement like the following:

insert into students (  REG_NUMBER,      
                        LASTNAME, 
                        FIRSTNAME, 
                        "YEAR", 
                        "GROUP",   
                        SCHOLARSHIP, 
                        DATE_OF_BIRTH
                      )
select to_char(rownum, 'fm0000'),
       n,
       p,
       floor(dbms_random.value(1, 4)),
       floor(dbms_random.value(1, 8)),
       round(dbms_random.value(1, 9999),2),
       null
from (
        select distinct *
        from LASTNAME 
           CROSS JOIN FIRSTNAME
     ) 
WHERE ROWNUM <= 2000;

The statement simply uses your starting data, adding some random values for the remaining fields; I left birthdate null, but you can use another random value to fill this field, if you need.

Assuming that reg_number is a primary key, I use the rownum to fill it; the format mask 'fm0000' says to format the number in exactly 4 digits with leading zeros ('0001', '0002', ...), preventing leading spaces.

This is a simple SQL statement; you can wrap it in a procedure if you need.

Please notice that your table students has columns named with reserved words (like GROUP ), so I had to use quotes; it would be better to avoid such column names.

If you want random name pairs (including the possibility of duplicate name pairs) then you can do:

INSERT INTO STUDENTS (
  REG_NUMBER,      
  LASTNAME, 
  FIRSTNAME,
  YEAR,
  "GROUP",
  SCHOLARSHIP,
  DATE_OF_BIRTH
)
SELECT  TO_CHAR(rownum, 'fm0000'),
        n,
        p,
        FLOOR( DBMS_RANDOM.VALUE(1, 4) ),
        FLOOR(DBMS_RANDOM.VALUE(1, 8)),
        ROUND(DBMS_RANDOM.VALUE(0, 9999), 2),
        ADD_MONTHS( TRUNC(SYSDATE), -12*18 )
          + FLOOR( DBMS_RANDOM.VALUE(0,365*4+1) )
FROM    ( SELECT FLOOR(DBMS_RANDOM.VALUE(0,(SELECT COUNT(*) FROM LASTNAME)))+1 AS lrn,
                 FLOOR(DBMS_RANDOM.VALUE(0,(SELECT COUNT(*) FROM FIRSTNAME)))+1 AS frn
          FROM   DUAL
          CONNECT BY LEVEL <= 2000
        ) r
        INNER JOIN
        ( SELECT n, ROWNUM AS rn FROM LASTNAME ) l
        ON ( r.lrn = l.rn )
        INNER JOIN
        ( SELECT p, ROWNUM AS rn FROM FIRSTNAME ) f
        ON ( r.frn = f.rn );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM