• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

Combining two tables into single view or table

I have two tables.  Many of the columns are named the same.  I need to make a combined table or view with data from both tables.  How is the best done?  Something like:

CREATE VIEW myComboView
AS
   SELECT ColumnA, ColumnB
   From
      table1, table2

Suppose that table 1 and table 2 look like:

Table1
(ColumnA int, ColumnB int, ONEColumn int)

Table2
(ColumnA int, CloumnB int, TWOColumn int
0
HyperBPP
Asked:
HyperBPP
  • 2
2 Solutions
 
BrandonGalderisiCommented:
You want to UNION them.
CREATE VIEW myComboView
AS
   SELECT ColumnA, ColumnB, 1
   From table1 
   union all
   SELECT ColumnA, ColumnB, 2
   From table2 

Open in new window

0
 
Patrick MatthewsCommented:
CREATE VIEW myComboView
AS
   SELECT ColumnA, ColumnB, ONEColumn AS ColumnC
   From      table1
   UNION ALL
   SELECT ColumnA, ColumnB, TWOColumn AS ColumnC
   from table2
0
 
BrandonGalderisiCommented:
As matthewspatrick points out, you need to have a name for ALL columns in the first part of the union.

my code updated.
CREATE VIEW myComboView
AS
   SELECT ColumnA, ColumnB, 1 as ColumnC
   From table1 
   union all
   SELECT ColumnA, ColumnB, 2
   From table2 

Open in new window

0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now