The SSMS Tools Pack created by Mladen Prajdić is a great extension for the SQL Server Management Studio. In this post I will explain how you can use it to create test data from a small subset of your production database.
From a bug in production…
Bugs happen, especially when complex data is involved. The most annoying bugs require a specific set of values to show up. Finding those involves most often a lot of debugging or a good logging infrastructure. When you find the offending combination, you could copy the whole production database and hand it to the developers. Data protection and the size of the database make this approach most often impossible, even if we ignore best practices and security. In this situation the SSMS Tools Pack can help you.
You start with a select that gives you the problematic rows from the first table in your production database. You can repeat the following steps with all other tables until you have all the data you need.
The extension created a new context menu entry in the result view called “Script Grid Results to SQL Editor“:
When clicked, a new SQL Editor window opens with a script like this one:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
--------------- #tmp_GridResults_1 --------------- SELECT * INTO #tmp_GridResults_1 FROM ( SELECT N'263' AS [Id], NULL AS [Title], N'Jean' AS [FirstName], N'E' AS [MiddleName], N'Trenary' AS [LastName], N'[email protected]' AS [EmailAddress] UNION ALL SELECT N'78' AS [Id], NULL AS [Title], N'Reuben' AS [FirstName], N'H' AS [MiddleName], N'D''sa' AS [LastName], N'[email protected]' AS [EmailAddress] UNION ALL SELECT N'242' AS [Id], NULL AS [Title], N'Deborah' AS [FirstName], N'E' AS [MiddleName], N'Poe' AS [LastName], N'[email protected]' AS [EmailAddress] UNION ALL SELECT N'125' AS [Id], NULL AS [Title], N'Matthias' AS [FirstName], N'T' AS [MiddleName], N'Berndt' AS [LastName], N'[email protected]' AS [EmailAddress] UNION ALL SELECT N'278' AS [Id], NULL AS [Title], N'Garrett' AS [FirstName], N'R' AS [MiddleName], N'Vargas' AS [LastName], N'[email protected]' AS [EmailAddress] UNION ALL SELECT N'239' AS [Id], NULL AS [Title], N'Mindy' AS [FirstName], N'C' AS [MiddleName], N'Martin' AS [LastName], N'[email protected]' AS [EmailAddress] UNION ALL SELECT N'184' AS [Id], NULL AS [Title], N'John' AS [FirstName], N'Y' AS [MiddleName], N'Chen' AS [LastName], N'[email protected]' AS [EmailAddress] UNION ALL SELECT N'87' AS [Id], NULL AS [Title], N'Cristian' AS [FirstName], N'K' AS [MiddleName], N'Petculescu' AS [LastName], N'[email protected]' AS [EmailAddress] UNION ALL SELECT N'174' AS [Id], NULL AS [Title], N'Benjamin' AS [FirstName], N'R' AS [MiddleName], N'Martin' AS [LastName], N'[email protected]' AS [EmailAddress] UNION ALL SELECT N'284' AS [Id], N'Mr.' AS [Title], N'Tete' AS [FirstName], N'A' AS [MiddleName], N'Mensa-Annan' AS [LastName], N'[email protected]' AS [EmailAddress] ) t; SELECT [Id], [Title], [FirstName], [MiddleName], [LastName], [EmailAddress] FROM #tmp_GridResults_1 GO DROP TABLE #tmp_GridResults_1 GO |
The script creates a temporary table, creates an Insert statement for every row in your result window, selects everything from the temporary table and then drops the temporary table. If you execute it, it will give you the same output as you had with your initial query.
Depending on the nature of the data, you can change the script to obfuscate or modify the values that are not needed to reproduce the bug.
… to the developer database
That script from above has some interesting capabilities. You can run in in any database and you will get the same result as in the production database. Therefore, you can give that to a developer who can run it to see the problematic data.
So far so good, but that script has one more trick to offer. All you need to do is to add an Insert statement right before the Select statement to persist this data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
... INSERT INTO [dbo].[Employee] ([Id] ,[Title] ,[FirstName] ,[MiddleName] ,[LastName] ,[EmailAddress]) SELECT [Id], [Title], [FirstName], [MiddleName], [LastName], [EmailAddress] FROM #tmp_GridResults_1 GO ... |
If the script with the little modification is executed, the data is saved in the table of the developer database. From there it can be used with the application and further debugged to find out where exactly the problem occurs. The same works with the test database, where you can write an automated test that will check if the problem is fixed (and stays fixed).
Conclusion
Instead of creating a full copy of the production database you can use the SSMS Tools Pack to only get the few necessary rows out of the table and into the developer database. That is a lot faster and most likely more compliant with your data protection rules than sending your full production database to your developers.