Here is a stored procedure that replaces customer specific data with random strings. There is a function that generates strings with as many words as specified in the parameter. For example, name will have only two words, whereas address could have 4 words and so on.
This function uses a for loop to iterate through records. A for loop is more expensive than using a cursor, but it is used as this is only a one time operation. customer_prj is the customer table.
Here’s the code!
declare
str1 varchar(2000);
recCust customer_prj%ROWTYPE;
idx number;
width number;
FUNCTION GenerateRandomPhrase
(
words_count IN NUMBER
)
RETURN varchar2
IS RandStr varchar2(2000);
BEGIN
idx := 0;
RandStr := '';
for idx IN 1..words_count
loop
width := 5;
if (mod(idx, 2) = 0) then
width := 8;
end if;
select dbms_random.string('L', width) INTO str1 from dual;
RandStr := RandStr || str1 || ' ';
end loop;
RETURN(RTrim(InitCap(RandStr)));
END;
FUNCTION CreateRandomCustomer
RETURN customer_prj%ROWTYPE
IS RandCust customer_prj%ROWTYPE;
BEGIN
RandCust.name := GenerateRandomPhrase(2);
RandCust.contact_person := GenerateRandomPhrase(2);
RandCust.address := GenerateRandomPhrase(4);
RandCust.street := concat(GenerateRandomPhrase(2), 'St');
RandCust.e_mail := concat(GenerateRandomPhrase(1), '@anonymous.com');
RETURN(RandCust);
END;
begin
for row in (SELECT * from customer_prj) loop
DBMS_OUTPUT.PUT_LINE(row.name);
recCust := CreateRandomCustomer();
update customer_prj
set customer_prj.name = recCust.name,
customer_prj.address = recCust.address,
customer_prj.street = recCust.street,
customer_prj.e_mail = recCust.e_mail,
customer_prj.contact_person = recCust.contact_person
where customer_prj.CUSTOMER_ID = row.CUSTOMER_ID;
end loop;
end;
/