T-SQL: WITH

Performing Recursive Queries.

Prior to SQL Server 2005, performing recursive actions on data required the use of temporary tables, cursive, or some other memory-intensive method.
Now, performing such operations requires much less code and avoids the previous performance increase.

For this example, we’ll begin with a generic table containing information about people.

WITH-Beginning-ResultSet

On the “Person” table, we’ll first list all values from the “firstname” field.
Next, from the previous list, we’ll only return those values that begin with the letter ‘s’.

WITH-code

Here is the final resultset.

WITH-Final-Results

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s