# SQL Random Row

## Use T-SQL to Pull a Random Row from a SQL Database

*I would like to use a single, random row of data for each execution of a test that is data bound to a SQL Database.*

## Solution

This is possible with T-SQL. Here's how to do it:

1. <a href="/features/data-driven-testing/sql-database-example" target="_blank">Add a SQL Database Source</a> to the project. Here's what the sample database looks like in SQL Server Management Studio Express:

   ![Create DB](https://www.telerik.com/teststudio/documentation/assets/f2f86c432efc968f871d24ddd84ac516/fig1.png)

2. <a href="/features/data-driven-testing/bind-test-data-source" target="_blank">Bind the Test</a> to the SQL Database. Here's what the Setup Binding dialog looks like before T-SQL is enabled:

   ![Bind test](https://www.telerik.com/teststudio/documentation/assets/f921ab55d85c8f5a2200f478ea631157/fig2.png)

3. Check **Use T-SQL**. Enter the following code into the **T-SQL Editor** section and click **Update**:

```SQL
SELECT TOP 1 [Name], [City], [Email], [Message]
FROM [myDataBase].[dbo].[table]
Where Email Like '%domain%'
ORDER By NEWID()
```

* Line 1 indicates how many rows and which columns to use.

* Line 2 indicates the database and table name.

* Line 3 indicates whether to filter based on text matching criteria. In this case, since all entries in the Email column contain domain, all rows are returned.

* Line 4 sorts the rows by a unique identifier, which essentially randomizes them in the database.

4. Click **Update again** to see the randomization in action.

   ![Random row](https://www.telerik.com/teststudio/documentation/assets/0ad50e29173c5ea0f6ef54a953699e6a/fig3.png)

5. The following test navigates to a site containing a sample contact form. Note that the Enter text steps already have the corresponding data columns <a href="/features/data-driven-testing/attach-columns-input-values" target="_blank">Attached to Input Values</a>.

![Bind columns](https://www.telerik.com/teststudio/documentation/assets/2e30f83eac60b8b90639648bbaf2a33c/fig4.png)

6. Run the test. A random row will be used to data drive the test each time you execute it.

   ![Test execution](https://www.telerik.com/teststudio/documentation/assets/f228045708a409302a3766fcc04877e1/fig5.png)
