SELECT INTO USING UNION QUERY

后端 未结 5 1594
轻奢々
轻奢々 2020-12-23 11:11

I want to create a new table in SQL Server with the following query. I am unable to understand why this query doesn\'t work.

Query1: Works

SELECT * F         


        
相关标签:
5条回答
  • 2020-12-23 11:30
    INSERT INTO #Temp1
    SELECT val1, val2 
    FROM TABLE1
     UNION
    SELECT val1, val2
    FROM TABLE2
    
    0 讨论(0)
  • 2020-12-23 11:34

    You have to define a table alias for a derived table in SQL Server:

    SELECT x.* 
      INTO [NEW_TABLE]
      FROM (SELECT * FROM TABLE1
            UNION
            SELECT * FROM TABLE2) x
    

    "x" is the table alias in this example.

    0 讨论(0)
  • 2020-12-23 11:46

    Here's one working syntax for SQL Server 2017:

    USE [<yourdb-name>]
    GO
    
    SELECT * INTO NEWTABLE 
    FROM <table1-name>
    UNION ALL
    SELECT * FROM <table2-name>
    
    0 讨论(0)
  • 2020-12-23 11:47

    You can also try:

    create table new_table as
    select * from table1
    union
    select * from table2
    
    0 讨论(0)
  • 2020-12-23 11:50
    select *
    into new_table
    from table_A
    UNION
    Select * 
    From table_B
    

    This only works if Table_A and Table_B have the same schemas

    0 讨论(0)
提交回复
热议问题