0

Hello everyone in SQL if am trying to union two tables in a new one , and am taking the columns names from these table which names its going to take for the new table and why ?

Code:

CREATE  TABLE New AS 
Select Phonenumber from Data 
union 
select PhoneNumber from INFO

Incorrect syntax near the keyword 'AS'

Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52
Samar Alaa
  • 13
  • 1

5 Answers5

2

Your query looks correct, assuming your database support create table as. However, new could easily be a reserved word in your database. I would recommend a more informative name. Something like this:

CREATE TABLE PhoneNumbers AS
    SELECT Phonenumber FROM Data 
    UNION
    SELECT PhoneNumber FROM INFO;

In SQL Server (the tag that was recently added), the correct syntax uses INTO:

SELECT Phonenumber FROM Data 
INTO PhoneNumbers
UNION
SELECT PhoneNumber FROM INFO;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1

Your error suggests me SQL Server DBMS if so then you use SELECT . . . INTO statement :

SELECT Phonenumber INTO New_table 
FROM Data 
UNION
SELECT PhoneNumber 
FROM INFO;

If the new_table already exists then you can do instead :

INSERT INTO  New_table (Phonenumber)
    SELECT Phonenumber INTO New_table 
    FROM Data 
    UNION
    SELECT PhoneNumber 
    FROM INFO;
Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52
  • new_table doesnt exist yet i am creating a new table so i will have to use create table right ? – Samar Alaa Feb 05 '19 at 12:31
  • @SamarAlaa. . . If the `new_table` doesn't exists then use first version of query. – Yogesh Sharma Feb 05 '19 at 12:32
  • it worked , thank you so much , i have another question , when we do so for multiple columns which Headers its going to take the one for the 1st table or the second one and why ,, for example if we have table 1 has Name Phone address , table 2 has FullName Phonenumber Homeaddress , which headers it going to take for the new table and why ?? – Samar Alaa Feb 05 '19 at 12:42
  • @SamarAlaa. . . In that case you have to explicitly re-name column_name & yes the first `SELECT` statement column_name will use as final `Column_name`. – Yogesh Sharma Feb 05 '19 at 12:45
  • 1
    so if i didnt name the coloumns it will take the Names in the 1st selecte query ! , perfect thank you so much again u r a life saver :) – Samar Alaa Feb 05 '19 at 12:53
1

you could use parenthesis

SELECT x.* 
  INTO [NEW_TABLE]
  FROM 
(Select Phonenumber from Data 
 union      
 select PhoneNumber from INFO
 ) x
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
0

For you use union, all the selection groups that you want to use union must have the same number of fields with their own names.

Try:

CREATE TABLE New_table SELECT * FROM (
    SELECT Phonenumber FROM Data 
    UNION
    SELECT PhoneNumber FROM INFO
 );
Roberto Góes
  • 778
  • 1
  • 13
  • 20
  • i wrote this exactly and it gives an error ' msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'select'. Msg 102, Level 15, State 1, Line 6 Incorrect syntax near ')' ' – Samar Alaa Feb 05 '19 at 12:37
0

You can use with clause like this:

with cte
as
(

Select Phonenumber from [Data] 
union 
select PhoneNumber from .[INFO]
)

SELECT Phonenumber INTO dbo.NEW_TABLE
FROM cte;
Jande
  • 1,695
  • 2
  • 20
  • 32