Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sarahmfr
Posting Yak Master
214 Posts |
Posted - 2011-10-08 : 16:39:49
|
| I have a lesson section tableA lesson table and lesson content tableLesson section table lessonid sectionid start and end dates Lesson table lessonid lesson name etcLesson content table lessonid contentid contenttype etcI want to duplicate all rows for lessons brooking to a particular section and related record to another section lessonid in lesson table is identity I way to do that without using a vursorsarah |
|
|
sarahmfr
Posting Yak Master
214 Posts |
Posted - 2011-10-08 : 16:45:37
|
| Note the contentid in lesson contentid in lesson content table is identity And lessonid in lessoncontent table is a foreign key pointing to lessonid in lesson tableI know that I could had the section I'd in lessons table and got rid of lessonsevtiom table nut we had this way because we were not going tobduplicate the contents on duplicating th lesson but they decided not to do thatsarah |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-09 : 12:43:38
|
store your sectionid in a variable as belowDECLARE @ParentSectionID int,@ChildSectionID intSET @ParentSectionID = <your value whose lessions are to copied>,@ChildSectionID = <your sectionid for which you need to copy values>INSERT INTO [lession section]SELECT ls.lessionId,@ChildSectionID,ls.start,ls.endFROM [lession section] lsWHERE ls.sectionid = @ParentSectionID ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sarahmfr
Posting Yak Master
214 Posts |
Posted - 2011-10-09 : 17:03:27
|
| This I know I want to insert identity recordsand recall the new identity lessonid's to which it corresponds to old lesson idsso as to insert lessoncontents of old lessons to new lessons without using a cursorMy question is how to map the new lessonid to orginal lessonid exampleuse trygoDECLARE @InsertedRows TABLE (newLessonID INT, orglessonid int)INSERT dbo.Child1 (LessonTitle, LessonDesc) OUTPUT inserted.LessonID as newlessonid INTO @InsertedRowsSELECT LessonTitle,LessonDescFROM Child1 AS I inner join Master1 as M on I.lessonid=M.lessonIDwhere SectionID=1insert into Master1 (lessonid,sectionid)(select newLessonID,2 from @InsertedRows)example master 1lessonid sectionid adjustdate scheduletime1 1 0 NULL2 1 1 NULL3 1 1 NULLChild1Lessonid Lessontitle Lessondesc1 Less1Sec1 Lesson1for section 12 Less2Sec1 Lesson 2 for Section 13 Less3Sec1 Lesson 3 for Section 1anscestor1LessonContentID LessonID ComponentID Comments1 1 O_23 NULL2 1 O_24 NULL3 1 O_25 NULL4 2 O_26 NULL5 2 O_27 NULLafter running the previous sql statementsmaster1lessonid sectionid adjustdate scheduletime1 1 0 NULL2 1 1 NULL3 1 1 NULL4 2 0 NULL5 2 0 NULL6 2 0 NULLchild1Lessonid Lessontitle Lessondesc1 Less1Sec1 Lesson1for section 12 Less2Sec1 Lesson 2 for Section 13 Less3Sec1 Lesson 3 for Section 14 Less1Sec1 Lesson1for section 15 Less2Sec1 Lesson 2 for Section 16 Less3Sec1 Lesson 3 for Section 1I want to have in the insertedrows table a map between orginal lessonid new lessonid1 42 53 6so as to continue to populate ancestor1with the corresponding records thanks a lotsarah |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-10 : 01:35:10
|
do as an update. UPDATE irSET ir.orglessonid = c1.orglessonidFROM @Inserted_rows irINNER JOIN (SELECT MIN(lessonid) as orglessonid,MAX(lessonid) as newlessonid,Lessontitle,LessondescFROM Child1 GROUP BY Lessontitle,Lessondesc)c1ON c1.newlessonid = ir.newLessonid ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|