user3598297 Japs user3598297 Japs - 1 month ago 12
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

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