You are on page 1of 5

6/6/2014 How to easily populate a table with random data

http://it.toolbox.com/blogs/db2luw/how-to-easily-populate-a-table-with-random-data-7888 1/5
Create an Account Log In
Blogs Discussions Research Directory
Toolbox f or IT Topics Database Blogs
Tweet 2 0 0
How to easily populate a table with random
data
Chris Eaton Feb 22, 2006 | Comments (13)
Have you ever wanted to quickly populate a test table with data? Well here is a
single SQL statement that you can use as a basis to add in dummy data into any
table with as many rows as you need.
First let's pick a sample table to populate as follows:
CREATE TABLE EMPLOYEE (
EMP_ID integer
DATE_OF_BIRTH date
SALARY integer
EMP_NAME char(10)
)
Above we have 3 of the more common data types (integer, character, date) but with
the method below you will see how you can modify the SQL for other data types
(timestamp, varchar, smallint, decimal, etc)
The first thing we need to do is start the insert statement with
INSERT INTO EMPLOYEE (EMP_ID, DATE_OF_BIRTH, SALARY, EMP_NAME)
Next we want to use a common table expression (CTE) to generate employee
numbers starting at 1 and going up to the number of rows I want to put in the table.
So for example, if I want 1000 rows in the table I would use the following CTE:
WITH EMP_IDS(EMP_ID) AS
( VALUES(1) UNION ALL
SELECT EMP_ID+1 FROM EMP_IDS WHERE EMP_ID < 1000 )
The above starts with a CTE with a single value of 1. Then union all's that with a new
row which is the previous row value plus 1. The union all's continue until the EMP_ID
reaches the max value you want which in this case is 1000.
The next thing to do is to select from this table of 1000 rows and add in random data
for the other columns. First select the EMP_ID value
SELECT EMP_ID,
1 Recommend Share
Your email address FOLLOW
BEGIN NOW
DB2 Downloads
Categories
Basics GO
An Expert's Guide to DB2 Technology
by Chris Eaton
Chris is a DB2 for Linux, UNIX and Windows lifer having
worked at IBM on DB2 since its inception. Follow along
as Chris shares ... more
Receive the latest blog posts:
Share Your Perspective
Share your professional knowledge and
experience with peers. Start a blog on Toolbox for
IT today!

6/6/2014 How to easily populate a table with random data
http://it.toolbox.com/blogs/db2luw/how-to-easily-populate-a-table-with-random-data-7888 2/5
More White Papers
13 Comments
Read 13 comments
Now generate random birth dates. Let's assume that employees are at least 18
years old but not more than 65 years old (no offense to anyone outside this range
but I'm using this to show you how you can create random dates within a given
range). The following starts with the current date and then subtracts from that a
number ranging from 18 years to 65 years (18 + 47 = 65).
CURRENT DATE - ((18 * 365) + RAND()*(47*365)) DAYS,
Now we want a random salary. Let's assume that everyone makes more than
$50,000 but less than $140,000.
INTEGER(50000 + RAND()*90000),
Now for the most difficult part. We want to generate a random character string of up
to 10 characters. For this we will first generate a random number that is up to 10
digits long, convert that number into a character string (the same numbers but now
as a CHAR datatype) and then we will use the TRANSLATE function to randomly
convert those numbers into characters.
TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000 )), 'abcdefghij', '1234567890' )
This will give you a random string made up of letters from a through j. If you want to
get fancy you could put in fewer than 10 characters in the to string so that it also
puts spaces in the text string to make it appear like it is multiple words. Or you could
include upper case letters as well. Finally you have to put in the FROM clause in
which you select from EMP_IDS.
Here is the full statement.
INSERT INTO EMP (EMP_ID, DATE_OF_BIRTH, SALARY, EMP_NAME)
WITH EMP_IDS(EMP_ID) AS
( VALUES(1) UNION ALL
SELECT EMP_ID+1 FROM EMP_IDS WHERE EMP_ID < 1000 )
SELECT EMP_ID,
CURRENT DATE - ((18 * 365) + RAND()*(47*365)) DAYS,
INTEGER(50000 + RAND()*90000),
TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000 )), 'abcdefgHij', '1234567890' )
FROM EMP_IDS
One SQL statement to populate 1000 rows of random data.
Popular White Paper On This Topic
The Not So Distant Future of Big Data
Related White Papers
DB2 10 - A Smarter Database for a Smarter Planet
DB2DART - Hints & Tips for the DBA
DB2 10 for z/OS - Secrets of Scalability
Unknown User May 31, 2008
Great! However it seems that there is a minor inaccuracy in the code as when I'm
6/6/2014 How to easily populate a table with random data
http://it.toolbox.com/blogs/db2luw/how-to-easily-populate-a-table-with-random-data-7888 3/5
trying to increase the max value to 500000, I get SQL0433N error - too long value
"ajjjjjjjjjj "...
Unknown User May 31, 2008
"EMP_NAME char(11)" solved my problem
Unknown User Jun 26, 2008
Hi Chris,
I have tried to run this on SQL server 2005 but it seems the "WITH" is not
recognized by SQL management Studio 2005.
The function "TRANSLATE" is also not working in the same. Can you provide me
the corresponding SQL function for this.
Chris Eaton Jun 27, 2008
Hi Arindom, this statement should do the trick
SELECT MORE_POWERFUL_RDBMS
FROM IBM
WHERE SQL = 'More Robust'
AND SCALABILITY= 'Higher'
AND PERFORMANCE = 'Faster'
:-)
Sorry but I don't know SQL Server syntax well enough to suggest an alternative.
Unknown User Sep 4, 2008
This sample was just what I was looking for. Thanks for posting this!
CURRENT DATE - ((18 * 365) + RAND()*(47*365)) DAYS,
I remorked this into a TIMESTAMP I needed:
CURRENT TIMESTAMP - ((18 * 365) + RAND()*(47*365)) DAYS
Apr 30, 2009
i tried following your code and ended up with errors in sql server 2005
that's the code for my employeedetail table
INSERT INTO Employeedetail (empid, empname, empadd, emptel, empposition,
empsalary) WITH empid(empid) AS
( VALUES(1) UNION ALL
SELECT empid+1 FROM empid WHERE empid
Chris Eaton Apr 30, 2009
Hi Haider, unfortunately SQL Server 2005 is not as advanced in the SQL space as
DB2 is. Sorry I can't help. But if you want to get DB2 in there to make it work you
can certainly get a free copy of Express-C.
dzodzo action Aug 2, 2009
Hi Chris,
I think You have wrong syntax...
each row should be separate by coma...
CREATE TABLE EMPLOYEE (EMP_ID integer,DATE_OF_BIRTH date,SALARY integer,EMP_NAME char(10));
6/6/2014 How to easily populate a table with random data
http://it.toolbox.com/blogs/db2luw/how-to-easily-populate-a-table-with-random-data-7888 4/5
SUBMIT PREVIEW
Prasanna SK Jan 11, 2010
Is there a way to generate random data and insert them into a table of a DB in DB2
Chris Eaton Jan 11, 2010
Yes, read this blog posting to see how.
USER_1877751 Jan 28, 2010
hey guys this query is not working in oracle, when i tried its prompting ORA-32033 :
unsupported column aliasing
Chris Eaton Jan 28, 2010
What does work in Oracle :-)
Oracle does not support the WITH clause until 11gR2. So you will have to use a
PL/SQL procedure in some kind of loop to add random data in 11gR1 and prior
releases.
Matt Rand Feb 15, 2010
Hi Chris,
I ran across your article thinking it was perfect, but like several others I discovered it
does not work on Oracle. However I came up with an alternate solution which seems
to work perfectly on Oracle and thought I would share for those who might need it.
Begin
For IDS in 1..100000
Loop
Insert into VALS(ID,VALS)
values(IDS, dbms_random.string('U',10));
If mod(IDS, 100000) = 0 then
Commit;
End if;
End loop;
End;
Hopefully this helps those Oracle folks out there. It's not as elegent as Chris's here,
but it gets the job done.
Leave a Comment
Connect to this blog to be notified of new entries.
Name

Your email address
You are not logged in.
Sign In to post unmoderated comments.
Join the community to create your free profile today.
Want to read more from Chris Eaton? Check out the blog archive.
Archive Category: Solve Problems
Keyword Tags: DB2 Common table expression sample data recursive query
6/6/2014 How to easily populate a table with random data
http://it.toolbox.com/blogs/db2luw/how-to-easily-populate-a-table-with-random-data-7888 5/5
Browse all IT Blogs
We Recommend
Everything You Need to Know about the
DataStage 8 Certification Exam
Testing: Sample Test Case Description
Document
Scheduled Base-touches with CRM
Ten Reasons to Implement ERP During
the Economic Downturn
What Role Does 3D Printing Play in an
ERP-Managed Manufacturing Process?
Connecting to Amazon AWS from
Windows to a Linux AMI
From Around The Web
Disclaimer: Blog contents express the viewpoints of their independent authors and are not reviewed f or
correctness or accuracy by Toolbox f or IT. Any opinions, comments, solutions or other commentary
expressed by blog authors are not endorsed or recommended by Toolbox f or IT or any vendor. If you f eel a
blog entry is inappropriate, click here to notif y Toolbox f or IT.
From Around The Web
Recommended by
Recommended by
Collaboration Tools
Discussion Groups
Blogs
Wiki
Toolbox for IT
My Home
Topics
People
Companies
Jobs
White Paper Library
Follow Toolbox.com
Toolbox for IT on
Twitter
Toolbox.com on Twitter
Toolbox.com on
Facebook
Data Center
Data Center
Development
C Languages
Java
Visual Basic
Web Design & Development
Enterprise Applications
CRM
ERP
PeopleSoft
SAP
SCM
Siebel
Enterprise Architecture & EAI
Enterprise Architecture & EAI
Information Management
Business Intelligence
Database
Data Warehouse
Knowledge Management
Oracle
IT Management & Strategy
Emerging Technology & Trends
IT Management & Strategy
Project & Portfolio Management
Cloud Computing
Cloud Computing
Networking & Infrastructure
Hardware
Networking
Communications Technology
Operating Systems
Linux
UNIX
Windows
Security
Security
Storage
Storage
Topics on Toolbox for IT Toolbox.com
About
News
Privacy
Terms of Use
Work at Toolbox.com
Advertise
Contact us
Provide Feedback
Help Topics
Technical Support
PCMag Digital Group
AdChoice
Other Communities
Toolbox for HR
Toolbox for Finance
Copyright 1998-2014 Ziff Davis, LLC (Toolbox.com). All rights reserved. All product names are trademarks of their respective companies. Toolbox.com is not
affiliated with or endorsed by any company listed at this site.

You might also like