插入随机名称PL / SQL

时间:2017-03-04 21:54:58

标签: sql oracle

如何将随机名称插入另一个表? 例如:

select firstname,lastname from table1;
create table2 (newfirstname varchar2(20),newlastname varchar2(20));
insert into table2 select newfirstname, random(newlastname);

如何使用table1的信息生成新人?
所以,我的问题是,如何将table1中的随机“lastnames”插入到 table2从table2获取一个新的全名(newfirstname + newlastname) 来自table1的(名字+姓氏)。 我正在使用sql developer,pl / sql。

3 个答案:

答案 0 :(得分:0)

我相信你应该在table2中有数字ID字段,比如table2.id,然后是

select  
(select MIN(table2.id) from table2) as minid, (select MAX(table2.id) from table2) as maxid, 
(select table2.firstname from table2 where table2.id >= dbms_random.value(minid,maxid) order by table2.id limit 1) as rnd_firstname, 
(select table2.lastname from table2 where table2.id >= dbms_random.value(minid,maxid) order by table2.id limit 1) as rnd_lastname;

将为您提供table2中firstname + lastname的随机组合。

答案 1 :(得分:0)

insert into table2
select A.firstname, B.lastname
  from table1 A, table1 B
 where not exists(select 1 from table1 C
                   where C.firstname=A.firstname and C.lastname=B.lastname)

如果需要,添加随机排序和/或计数限制。

答案 2 :(得分:0)

  SELECT
    aa.nome,
    aa.sobrenome,
    bb.nome,
    bb.sobrenome,
    CASE 
        WHEN DBMS_RANDOM.VALUE(1,50) > 20 THEN aa.nome || bb.sobrenome
        ELSE
         bb.nome || aa.sobrenome
    END AS new_name
  FROM
    (
      SELECT
          SubStr(b.nome,1,InStr(b.nome,' ')) nome,  
          SubStr(b.nome,InStr(b.nome,' '))  sobrenome,
          sexo
      FROM 
          cadastro b
      WHERE
          empresa_id=4 
      ORDER BY 
          DBMS_RANDOM.VALUE(1,800000)
    ) aa,
    (
      SELECT 
          SubStr(b.nome,1,InStr(b.nome,' ')) nome,  
          SubStr(b.nome,InStr(b.nome,' '))  sobrenome,
          sexo
      FROM 
          cadastro b
      WHERE
          empresa_id=4               
      ORDER BY
        DBMS_RANDOM.VALUE(1,800000 )
    ) bb
  WHERE  
      aa.sexo=bb.sexo