In the early stages of application design DBA or a developer creates a data model. Unfortunately many models work very well with a handful of rows but fail miserably when the application grows by leaps and bounds. This is why it is important to populate your data model with data and stress test it prior to making it available for users. Test data doesn't have to be perfect; indeed, you can duplicate the same record, or a few different records, to test the performance of your queries.
In this article, I offer a script for generating INSERT statements for every table in your database. The script is fairly simple -- it relies on three system tables: sysusers, sysobjects and syscolumns. It generates an INSERT statement duplicating the top row in your table. However, it can be easily altered to fit your needs.
SET NOCOUNT ON DECLARE @table VARCHAR(200), @owner VARCHAR(100), @sql VARCHAR(2000), @sql1 VARCHAR(2000) DECLARE @schema TABLE ( table_name VARCHAR(200), column_name VARCHAR(200)) INSERT @schema SELECT c.name + '.' + a.name, b.name FROM sysobjects a INNER JOIN syscolumns b ON b.id = a.id AND a.type = 'u' AND a.name <> 'dtproperties' INNER JOIN sysusers c ON c.uid = a.uid ORDER BY a.name, b.colid DECLARE table_cursor CURSOR FOR SELECT DISTINCT table_name FROM @schema OPEN table_cursor FETCH NEXT FROM table_cursor INTO @table WHILE @@FETCH_STATUS = 0 BEGIN SELECT @sql1 = '' SELECT @sql1 = @sql1 + ', '+ column_name FROM @schema WHERE table_name = @table SELECT @sql1 = SUBSTRING(@sql1, 3, LEN(@sql1)-2) SELECT @sql = 'INSERT ' + @table + '( ' + @sql1 + ' ) ' + CHAR(10) + ' SELECT TOP 1 ' + @sql1 + ' FROM ' + @table SELECT @sql FETCH NEXT FROM table_cursor INTO @table END CLOSE table_cursor DEALLOCATE table_cursor
In the pubs database, the output will be similar to the following:
INSERT dbo.authors( au_id, au_lname, au_fname, phone, address, city, state, zip, contract ) SELECT TOP 1 au_id, au_lname, au_fname, phone, address, city, state, zip, contract FROM dbo.authors INSERT dbo.discounts( discounttype, stor_id, lowqty, highqty, discount ) SELECT TOP 1 discounttype, stor_id, lowqty, highqty, discount FROM dbo.discounts INSERT dbo.employee( emp_id, fname, minit, lname, job_id, job_lvl, pub_id, hire_date ) SELECT TOP 1 emp_id, fname, minit, lname, job_id, job_lvl, pub_id, hire_date FROM dbo.employee
About the Author
Baya Pavliashvili is a MCSE, MCSD and MCDBA with five years experience with SQL Server.
For More Information
- Feedback: E-mail the editor with your thoughts about this tip.
- More tips: Hundreds of free SQL Server tips and scripts.
- Tip contest: Have a SQL Server tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
- Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
- Forums: Ask your technical SQL Server questions--or help out your peers by answering them--in our active forums.
- Best Web Links: SQL Server tips, tutorials, and scripts from around the Web.