Skip to main content
Tweeted twitter.com/StackDBAs/status/1019371926186323973
wording; spelling
Source Link
Nick Chammas
  • 14.8k
  • 17
  • 76
  • 123

Perform "UNION ALL" iregardlessUNION ALL regardless of column order in the two joined tables

SELECT * INTO TABLE1 
FROM Table2
UNION ALL 
SELECT * FROM Table3;
GO

I am using this query to stack two tables together into one table.

  These tables should have the columns in the exact same order for this query to execute succesfullysuccessfully. I

I am wondering whetherif there is a trick we can do so that it works iregardlessregardless of the column order, I am surprised if. Is there is not a way for "SQLto instruct SQL Server Mgmt" to do findautomatically line up the corresponding columncolumns from table2, in table1, if they have the sameboth tables by name. and perform the UNION?

In SAS, the trick is to write a "OUTER UNION CORR"OUTER UNION CORR query, for example.

Thanks a billion!

Perform "UNION ALL" iregardless of column order in the two joined tables

SELECT * INTO TABLE1 
FROM Table2
UNION ALL 
SELECT * FROM Table3;
GO

I am using this query to stack two tables together into one table.

  These tables should have the columns in the exact same order for this query to execute succesfully. I am wondering whether there is a trick we can do so that it works iregardless of the column order, I am surprised if there is not a way for "SQL Server Mgmt" to do find the corresponding column from table2, in table1, if they have the same name.

In SAS, the trick is to write a "OUTER UNION CORR" query, for example.

Thanks a billion!

Perform UNION ALL regardless of column order in the two joined tables

SELECT * INTO TABLE1 
FROM Table2
UNION ALL 
SELECT * FROM Table3;
GO

I am using this query to stack two tables together into one table. These tables should have the columns in the exact same order for this query to execute successfully.

I am wondering if there is a trick we can do so that it works regardless of the column order. Is there a way to instruct SQL Server to automatically line up the columns from both tables by name and perform the UNION?

In SAS, the trick is to write a OUTER UNION CORR query, for example.

re-tag
Link
added 20 characters in body
Source Link
garik
  • 6.8k
  • 10
  • 43
  • 56

SELECT * INTO TABLE1 FROM Table2 UNION ALL SELECT * FROM Table3; GO

SELECT * INTO TABLE1 
FROM Table2
UNION ALL 
SELECT * FROM Table3;
GO

I am using this query to stack two tables together into one table.

These tables should have the columns in the exact same order for this query to execute succesfully. I am wondering whether there is a trick we can do so that it works iregardless of the column order, I am surprised if there is not a way for "SQL Server Mgmt" to do find the corresponding column from table2, in table1, if they have the same name.

In SAS, the trick is to write a "OUTER UNION CORR" query, for example.

Thanks a billion!

SELECT * INTO TABLE1 FROM Table2 UNION ALL SELECT * FROM Table3; GO

I am using this query to stack two tables together into one table.

These tables should have the columns in the exact same order for this query to execute succesfully. I am wondering whether there is a trick we can do so that it works iregardless of the column order, I am surprised if there is not a way for "SQL Server Mgmt" to do find the corresponding column from table2, in table1, if they have the same name.

In SAS, the trick is to write a "OUTER UNION CORR" query, for example.

Thanks a billion!

SELECT * INTO TABLE1 
FROM Table2
UNION ALL 
SELECT * FROM Table3;
GO

I am using this query to stack two tables together into one table.

These tables should have the columns in the exact same order for this query to execute succesfully. I am wondering whether there is a trick we can do so that it works iregardless of the column order, I am surprised if there is not a way for "SQL Server Mgmt" to do find the corresponding column from table2, in table1, if they have the same name.

In SAS, the trick is to write a "OUTER UNION CORR" query, for example.

Thanks a billion!

Source Link
Loading