Pergunta

I have a table "User" (idUser, name, departmentId) and another table "Department" (idDepartment, name).

I would like to insert random data into table User, but it would be necessary to consider only the departments previously inserted.

I think it would be something like:

INSERT INTO User (idUser, name, departmentId)
VALUES (seq_user.nextVal, 'random name', FK_RANDOM_DEPARTMENT);

How can I generate this 'random name' and only use departments that I have already inserted?

Thanks in advance!

Foi útil?

Solução

INSERT INTO User 
(idUser, name, departmentId)
VALUES 
(seq_user.nextVal,
 DBMS_RANDOM.STRING('L', 20), /*20 is a number of characters in a string*/
 (SELECT * FROM (SELECT idDepartment FROM Department ORDER BY DBMS_RANDOM.VALUE) WHERE ROWNUM = 1)
);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top