I’ve got two tables – tonight I’m honestly believing I’m having a blonde moment.
Headings
HeadingID, HeadingName, TopicID
SubHeadings
SubHeadingID, SubHeadingName, HeadingID
What I’d like to do is to duplicate all entries in Headings and SubHeadings where TopicID = 1 to = 2.
I’ve written:
INSERT INTO Headings (HeadingName, TopicID)
SELECT HeadingName, 2 FROM Headings WHERE TopicID = 1 //(Duplicating Headings and assigning TopicID 2).
I’m stuck on the SubHeadings; I’d like to put in the new HeadingID that has just been created and I’ve got no idea. I can do the rest of the fields with the same Insert/Select statement however I’m stumped.
Can someone please help me out with an example or some documentation please?
Simplified the data looks like this:
HeadingID = 1
HeadingName = 'News'
TopicID = 1
HeadingID = 2
HeadingName = 'Sport'
TopicID = 1
SubHeadingID = 1
SubHeadingName = 'Local'
HeadingID = 1
SubHeadingID = 2
SubHeadingName = 'National'
HeadingID = 1
SubHeadingID = 3
SubHeadingName = 'International'
HeadingID = 1
SubHeadingID = 4
SubHeadingName = 'Baseball'
HeadingID = 2
SubHeadingID = 5
SubHeadingName = 'Cricket'
HeadingID = 2
Following query will help you 🙂