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)))
tmajor99Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Walter RitzelSenior Software EngineerCommented:
just do this:
- create the table
- use the following command:
insert into <table name> (<field1>, <field2>, <field3>)
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)))

Open in new window

Mark ElySenior Coldfusion DeveloperCommented:
INSERT INTO Name of yourTable
     (PRODUCT, DESCRIPTION, HIERARCHY)
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)))
Walter RitzelSenior Software EngineerCommented:
Or if you want to create a new table from this select:
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)))

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Jim HornMicrosoft SQL Server Data DudeCommented:
<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

awking00Information Technology SpecialistCommented:
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)))
Vitor MontalvãoMSSQL Senior EngineerCommented:
tmajor99, do you still need help with this question?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.