SQL Insert into one table from another

Posted on 2011-04-18
Last Modified: 2012-05-11
I need select the RecID field from one table then do an insert into another table fore each row that was found.  Any examples of that?

My select will be:
Select RecID from Meta where username = 'jdoe'

I want to loop through those then do this insert:
Insert into Publishers (fkRecID, PRO_Name) values (RecID_From_Loop, 'XYZ')
Question by:JT_SIRO
    LVL 4

    Expert Comment

    INSERT INTO Publishers (fkRecID, PRO_Name)
         VALUES ( (SELECT RecID FROM Meta WHERE username = 'jdoe'), 'XYZ')
    LVL 4

    Accepted Solution

    I think last one does not work, maybe this one will.

    INSERT INTO Publishers (fkRecID, PRO_Name)
         SELECT RecID, 'XYZ' FROM Meta WHERE username = 'jdoe';

    Open in new window

    LVL 32

    Expert Comment


    Insert into Publishers (fkRecID, PRO_Name)
    select RecID_From_Loop, 'XYZ'
    from Meta where username = 'jdoe'
    LVL 37

    Expert Comment

    by:Aaron Tomosky
    I think ewangoya is correct. No "values" when using an insert select.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Creating and Managing Databases with phpMyAdmin in cPanel.
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
    Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now