Link to home
Start Free TrialLog in
Avatar of tmajor99
tmajor99

asked on

SQL Select Query to save results to another table

I need a SQL Select command that will save my results to a SQL Server table.   I want the results from the query below inserted into a new SQL Server table.

SELECT        PRODUCT, DESCRIPTION, HIERARCHY
FROM            F_MASTER
WHERE        (HIERARCHY NOT IN
                             (SELECT        GLBL_PROD_HIER
                               FROM            F_STEP_Level4
                               WHERE        (F_MASTER.HIERARCHY = GLBL_PROD_HIER)))
SOLUTION
Avatar of Walter Ritzel
Walter Ritzel
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<minor addition to the above correct answers>

If you're going to be creating tables on the fly in this manner and planning on running it multiple times, make sure the table does not exist before you create it, or else it will throw an error..
IF OBJECT_ID('new_table') IS NOT NULL
   DROP TABLE new_table
GO

SELECT PRODUCT, DESCRIPTION, HIERARCHY
into new_table   --- blah blah blah

Open in new window

Or if you go the pre-existing table and INSERT, might not be a bad idea to remove all previous rows first
TRUNCATE TABLE new_table

INSERT INTO new_table --- blah blah blah

Open in new window

Using into clause should do it -
SELECT        PRODUCT, DESCRIPTION, HIERARCHY
INTO NEW_TABLE

 FROM            F_MASTER
 WHERE        (HIERARCHY NOT IN
                              (SELECT        GLBL_PROD_HIER
                                FROM            F_STEP_Level4
                                WHERE        (F_MASTER.HIERARCHY = GLBL_PROD_HIER)))
tmajor99, do you still need help with this question?