Nested SQL Select Statements Using WITH Alias

i find myself using this type of SQL selection a lot when refactoring old web apps using polluted databases (Don’t forget the ; at the end of the statement):

WITH crazyunionselects as (

SELECT
username
,password
,id
FROM [database].[dbo].[usernamestableone]
where active= 1

union

SELECT
username
,password
,id
FROM [database].[dbo].[usernamestabletwo]
where active= 1

) select * from crazyunionselects 

where username= @username and password = @password ;

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s