user3598297 Japs user3598297 Japs - 10 months ago 42
SQL Question

Copy selected data from one table to another in SQL Server

I have wrote query to fetch the data.

select Terr, Product from sdata, md where sdata.PSetID = md.PsetID;


I want to copy this fetched data to new table.
I tried following query but didn't worked.

select * into ttl from
(select Terr, Product from sdata, md where sdata.PSetID = md.PsetID);


What i am doing wrong?

Answer Source

You need to add alias name to the sub-select

SELECT *
INTO   ttl
FROM   (SELECT Terr,
               Product
        FROM   sdata
               INNER JOIN md
                       ON sdata.PSetID = md.PsetID) A; --Here

Or simply

SELECT Terr,
       Product
INTO   ttl
FROM   sdata
       INNER JOIN md
               ON sdata.PSetID = md.PsetID