Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8361129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:37:54+00:00 2026-06-09T11:37:54+00:00

I have a working INSERT INTO SELECT query which inserts records into a new

  • 0

I have a working INSERT INTO SELECT query which inserts records into a new table based on the results of the select. Simple.

INSERT INTO my_table (a, b, c)
SELECT a, b, c FROM my_table 
WHERE x=y;

How do I get the newly created primary key IDs from the INSERT part of this query so that I can use them to update another table? (The primary key is the only unique reference)

I have read that INSERT INTO SELECT only supports inserting into one table per query, so that’s out of the question. How can this be accomplished (using MyISAM engine)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:37:54+00:00Added an answer on June 9, 2026 at 11:37 am

    YOu can select your data in a temp Table, then run cursor on that while inserting rows u ll have PK

         DECLARE @tempTable TABLE(
        a VARCHAR(MAX),
        b VARCHAR(MAX),
        c VARCHAR(MAX)
        )
    
        INSERT INTO @tempTable(a,b,c)
        SELECT a,b,c FROM my_table WHERE x=y
    
    DECALRE @a DATATYPE;
    Declare @b DATATYPE;
    Declare @c DATATYPE;
    
    DECLARE cursor CURSOR FOR
        SELECT * FROM @tempTable
    
    OPEN cursor 
    
    FETCH NEXT FROM PropertyCursor INTO @a, @b,@c
    WHILE (@@FETCH_STATUS = 0)
    BEGIN
     INSERT INTO my_table(a,b,c)
    VALUES(@a,@b,@c)
    DECALRE @PrimaryKey INT = SCOPE_IDENTITY();//THis line will give u the PK so u can work with it and then below move to next row
    FETCH NEXT FROM cursor INTO @a, @b,@c
    END
    
    CLOSE cursor 
    DEALLOCATE cursor 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a select SQL query which use parallelism, something like this INSERT/*+ APPEND
I am trying to run a query: INSERT INTO `ProductState` (`ProductId`, `ChangedOn`, `State`) SELECT
I have a stupid little SQL query which isn't working and i can't figure
Now that I have a read-only application working, I am working on the insert
I have a trigger for insert/update/delete. That is working fine. Also, I need the
I am working on a app in CI, I have a model that inserts
I have working website in PHP with a MySQL backend which has several tables
I have a third party library which I finally have working within my MonoTouch
I'm pretty new to NHibernate. I have working many to many mappings. they look
I’m working on optimizing some of my queries and I have a query that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.