I have four tables (complete data definition and test data at end of post):- kt_ClientMedications
- kt_ClientMedicationInstructions (foreign key ClientMedicationId)
- kt_ClientMedicationScriptDrugs (foreign key ClientMedicationInstructionId)
- kt_ClientMedicationScripts
I need to insert a copy of all rows in kt_ClientMedicationInstructions with a given ClientMedicationsId into kt_ClientMediationsInstructions. I then need to update the ClientMedicationInsturctionId in kt_ClientMedicationScriptDrugs table to point to the correct new instruction record(s).If there is only one record that I need to copy this would be simple:declare @ClientMedicationId int = 72558declare @ClientMedicationInsturctionId intset @ClientMedicationInsturctionId = (select ClientMedicationInstructionId from kt_ClientMedicationInstructions where ClientMedicationId = @ClientMedicationId)insert into kt_ClientMedicationInstructionsselect ClientMedicationId , MedicationDescription , Active , Schedule from kt_ClientMedicationInstructions where ClientMedicationInstructionId = @ClientMedicationInsturctionId update kt_ClientMedicationScriptDrugs set ClientMedicationInstructionId = @@IDENTITY where ClientMedicationInstructionId = @ClientMedicationInsturctionId
But there can be multiple kt_ClientMedicaitonInstructions records. I thought of using the OUTPUT clause on the insert to return the new ids into a table variable. But I couldn't figure out how to associate each new record's ClientMedicaitonInstructionId with the originating record's ClientMedicaitonInstructionId when I went to update the records in kt_ClientMedicationScriptDrugs.I ended up writing a cursor to process one insert at a time (see code at end of post). We are only talking about 1 to 4 records so this is really not a performance problem. But I want to learn and wondered if there was any way to do this without a cursor.Note: I also change the Active flag on the original ClientMedicationInstructions record to N and change the ScriptEventType on the ClientMedicationScripts table to C.Thanks,LaurieTest tables:-- kt_ClientMedicationsCREATE TABLE [dbo].[kt_ClientMedications]( [ClientMedicationId] [int] IDENTITY(1,1) NOT NULL, [MedicationNameId] [int] NOT NULL, CONSTRAINT [kt_ClientMedications_PK] PRIMARY KEY ( [ClientMedicationId] ASC))-- kt_ClientMedicationInstructionsCREATE TABLE [dbo].[kt_ClientMedicationInstructions]( [ClientMedicationInstructionId] [int] IDENTITY(1,1) NOT NULL, [ClientMedicationId] [int] NOT NULL, [MedicationDescription] [varchar](100) NULL, [Active] [char] (1) null, [Schedule] [int] null, CONSTRAINT [kt_ClientMedicationInstructions_PK] PRIMARY KEY ( [ClientMedicationInstructionId] ASC))ALTER TABLE [dbo].[kt_ClientMedicationInstructions] WITH NOCHECK ADD CONSTRAINT [kt_ClientMedications_ClientMedicationInstructions_FK] FOREIGN KEY([ClientMedicationId])REFERENCES [dbo].[kt_ClientMedications] ([ClientMedicationId])ALTER TABLE [dbo].[kt_ClientMedicationInstructions] CHECK CONSTRAINT [kt_ClientMedications_ClientMedicationInstructions_FK]-- kt_ClientMedicationScriptsCREATE TABLE [dbo].[kt_ClientMedicationScripts]( [ClientMedicationScriptId] [int] IDENTITY(1,1) NOT NULL, [ScriptEventType] [char](1) NOT NULL, CONSTRAINT [kt_ClientMedicationScripts_PK] PRIMARY KEY ( [ClientMedicationScriptId] ASC))-- kt_ClientMedicationScriptDrugsCREATE TABLE [dbo].[kt_ClientMedicationScriptDrugs]( [ClientMedicationScriptDrugId] [int] IDENTITY(1,1) NOT NULL, [ClientMedicationScriptId] [int] NULL, [ClientMedicationInstructionId] [int] NOT NULL, [Pharmacy] [decimal](10, 2) NULL, CONSTRAINT [kt_ClientMedicationScriptDrugs_PK] PRIMARY KEY ( [ClientMedicationScriptDrugId] ASC))ALTER TABLE [dbo].[kt_ClientMedicationScriptDrugs] WITH NOCHECK ADD CONSTRAINT [kt_ClientMedicationInstructions_ClientMedicationScriptDrugs_FK] FOREIGN KEY([ClientMedicationInstructionId])REFERENCES [dbo].[kt_ClientMedicationInstructions] ([ClientMedicationInstructionId])ALTER TABLE [dbo].[kt_ClientMedicationScriptDrugs] CHECK CONSTRAINT [kt_ClientMedicationInstructions_ClientMedicationScriptDrugs_FK]ALTER TABLE [dbo].[kt_ClientMedicationScriptDrugs] WITH NOCHECK ADD CONSTRAINT [kt_ClientMedicationScripts_ClientMedicationScriptDrugs_FK] FOREIGN KEY([ClientMedicationScriptId])REFERENCES [dbo].[kt_ClientMedicationScripts] ([ClientMedicationScriptId])ALTER TABLE [dbo].[kt_ClientMedicationScriptDrugs] CHECK CONSTRAINT [kt_ClientMedicationScripts_ClientMedicationScriptDrugs_FK]--Load Test DataSET IDENTITY_INSERT kt_ClientMedications oninsert into kt_ClientMedications(ClientMedicationId, MedicationNameId)select 34749, 278 union allselect 71717, 55814 union allselect 72558, 20865 SET IDENTITY_INSERT kt_ClientMedications offSET IDENTITY_INSERT kt_ClientMedicationInstructions oninsert into kt_ClientMedicationInstructions(ClientMedicationInstructionId, ClientMedicationId, MedicationDescription, Active, Schedule)select 71514, 34749, 'Cogentin 0.5 mg tablet', 'Y', 4861 union allselect 139070, 71717, 'clonidine HCl 0.3 mg tablet', 'Y', 4863 union allselect 139071, 71717, 'clonidine HCl 0.3 mg tablet', 'Y', 4864 union allselect 140145, 72558, 'Focalin 5 mg tablet', 'Y', 4861 union allselect 140146, 72558, 'Focalin 5 mg tablet', 'Y', 10831 SET IDENTITY_INSERT kt_ClientMedicationInstructions offSET IDENTITY_INSERT kt_ClientMedicationScripts oninsert into kt_ClientMedicationScripts(ClientMedicationScriptId, ScriptEventType)Select 164828, 'R' union allSelect 266592, 'R' union allSelect 270626, 'R' SET IDENTITY_INSERT kt_ClientMedicationScripts offSET IDENTITY_INSERT kt_ClientMedicationScriptDrugs oninsert into kt_ClientMedicationScriptDrugs(ClientMedicationScriptDrugId, ClientMedicationScriptId, ClientMedicationInstructionId, Pharmacy)select 202259, NULL, 71514, 0 union allselect 202441, 164828, 71514, 30 union allselect 364566, NULL, 139070, 0 union allselect 364567, NULL, 139071, 0 union allselect 364614, 266592, 139070, 30 union allselect 364615, 266592, 139071, 15 union allselect 366954, NULL, 140145, 0 union allselect 366955, NULL, 140146, 0 union allselect 372083, 270626, 140145, 30 union allselect 372084, 270626, 140146, 330 SET IDENTITY_INSERT kt_ClientMedicationScriptDrugs off
Cursor Solution:declare @ClientMedicationId int = 71717declare @DataToProcess table (ClientMedicationScriptDrugId int, ClientMedicationInstructionId int, ClientMedicationScriptId int) insert into @DataToProcessselect cmsd.ClientMedicationScriptDrugId , cmi.ClientMedicationInstructionId , cmsd.ClientMedicationScriptId from kt_ClientMedications cm join kt_ClientMedicationInstructions cmi on cm.ClientMedicationId = cmi.ClientMedicationId join kt_ClientMedicationScriptDrugs cmsd on cmsd.ClientMedicationInstructionId = cmi.ClientMedicationInstructionId and cmsd.ClientMedicationScriptId is not null where cm.ClientMedicationId = @ClientMedicationId SELECT * FROM @DataToProcess declare cur cursor read_only for Select ClientMedicationScriptDrugId , ClientMedicationInstructionId , ClientMedicationScriptId from @DataToProcess declare @ClientMedicationScriptDrugId intdeclare @ClientMedicationInstructionId intdeclare @ClientMedicationScriptId intOPEN cur FETCH NEXT FROM cur INTO @ClientMedicationScriptDrugId , @ClientMedicationInstructionId , @ClientMedicationScriptId WHILE @@FETCH_STATUS = 0 BEGIN insert into kt_ClientMedicationInstructions select ClientMedicationId , MedicationDescription , Active , Schedule --, ClientMedicationInstructionId from kt_ClientMedicationInstructions where ClientMedicationInstructionId = @ClientMedicationInstructionId update kt_ClientMedicationScriptDrugs set ClientMedicationInstructionId = @@IDENTITY where ClientMedicationScriptDrugId = @ClientMedicationScriptDrugId update kt_ClientMedicationInstructions set Active = 'N' where ClientMedicationInstructionId = @ClientMedicationInstructionId update kt_ClientMedicationScripts set ScriptEventType = 'C' where ClientMedicationScriptId = @ClientMedicationScriptId FETCH NEXT FROM cur INTO @ClientMedicationScriptDrugId , @ClientMedicationInstructionId , @ClientMedicationScriptId END CLOSE curDEALLOCATE cur
Expected output @ClientMedicationId = 71717 kt_ClientMedicationsInstructions Before:ClientMedicationInstructionId ClientMedicationId Active Schedule----------------------------- ------------------ ------------------------------ ------ -----------139070 71717 clonidine HCl 0.3 mg tablet Y 4863139071 71717 clonidine HCl 0.3 mg tablet Y 4864kt_ClientMedicationsInstructions After:ClientMedicationInstructionId ClientMedicationId Active Schedule----------------------------- ------------------ ------------------------------ ------ -----------139070 71717 clonidine HCl 0.3 mg tablet N 4863139071 71717 clonidine HCl 0.3 mg tablet N 4864140147 71717 clonidine HCl 0.3 mg tablet Y 4863140148 71717 clonidine HCl 0.3 mg tablet Y 4864kt_ClientMedicationScriptDrugs BeforeClientMedicationScriptDrugId ClientMedicationScriptId ClientMedicationInstructionId Pharmacy---------------------------- ------------------------ ----------------------------- ----------364566 NULL 139070 0.00364567 NULL 139071 0.00364614 266592 139070 30.00364615 266592 139071 15.00kt_ClientMedicationScriptDrugs After:ClientMedicationScriptDrugId ClientMedicationScriptId ClientMedicationInstructionId Pharmacy---------------------------- ------------------------ ----------------------------- ----------364566 NULL 139070 0.00364567 NULL 139071 0.00364614 266592 140147 30.00364615 266592 140148 15.00