Two Ways to Concatenate SQL Server Tables or Views

Suppose you have two SQL Server tables that have the same schema-definition (or views for that matter) and you need to concatenate them into one table, here are two ways to go about it:

1) The more traditional stepwise way uses SELECT INTO followed by INSERT INTO

SELECT * INTO #concatenated  FROM Table1

INSERT INTO #concatenated SELECT * FROM Table2

2) The (in my opinion) somewhat cooler, one-liner approach uses UNION ALL

SELECT * INTO #concatenated FROM Table1

                  UNION ALL SELECT * FROM Table2

The choice is all yours.

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