Edit: This problem is most likely not solvable. There is another related problem that might be. See my other topic: Foreign key assign (part 2 ... More Complicated) (See post in that topic by visakh16 explaining why this is ONE not solvable).I have these two tables:CREATE TABLE Medications( MedicationId int IDENTITY(1,1) PRIMARY KEY NOT NULL, StartDate datetime, EndDate datetime,) CREATE TABLE Instructions( InstructionId int IDENTITY(1,1) PRIMARY KEY NOT NULL, MedicationId int foreign key references Medications(MedicationId) not null, StartDate datetime, EndDate datetime)
I need to do an insert into both tables. When inserting into the Instructions table I need to use the correct foreign key assignment.So I have this data:CREATE TABLE InputData( [GroupID] [int] NULL, [OrderNumber] [int] NOT NULL, [EventType] [varchar](1) NULL, [StartDate] [datetime] NULL, [EndDate] [datetime] NULL) select 178248, 3, 'C', '2008-10-02 00:00:00.000', '2008-12-23 14:00:47.000' union allselect 178248, 5, 'C', '2009-03-16 00:00:00.000', '2009-06-14 00:00:00.000' union allselect 178248, 1, 'N', '2008-05-05 00:00:00.000', '2008-07-10 00:00:00.000' union allselect 5834, 3, 'R', '2010-01-19 16:57:06.000', '2010-01-26 00:00:00.000' union allselect 5834, 1, 'N', '2010-01-19 00:00:00.000', '2010-01-19 16:28:02.000' union allselect 5834, 5, 'C', '2010-03-02 00:00:00.000', '2010-03-09 00:00:00.000' union allselect 178248, 4, 'R', '2008-12-23 14:00:47.000', '2009-03-16 00:00:00.000' union allselect 178248, 6, 'R', '2009-06-17 14:19:08.000', '2009-09-15 00:00:00.000' union allselect 178248, 2, 'C', '2008-07-10 00:00:00.000', '2008-10-02 00:00:00.000' union allselect 5834, 2, 'R', '2010-01-19 16:28:02.000', '2010-01-19 16:57:06.000' union allselect 5834, 4, 'C', '2010-01-26 00:00:00.000', '2010-02-25 00:00:00.000'
The Medications table has one record for each N(ew) order. So I use this insert statement:insert into Medications (StartDate,EndDate)select StartDate , EndDate from InputData where EventType = 'N'
Now I need to insert rows into the Instructions table. I want a row in this table for all N(ew) and C(hanged) records. The foreign key in this table should point back to the record in the Medications table based on the GroupId.I have solved the problem by adding GroupId to the Medicaitons table:CREATE TABLE Medications( MedicationId int IDENTITY(1,1) PRIMARY KEY NOT NULL, StartDate datetime, EndDate datetime, GroupId int)
And then write my insert statement like so:insert into Instructions (MedicationId,StartDate,EndDate)select m.MedicationId , i.StartDate , i.EndDate from InputData i join Medications m on i.GroupID = m.GroupId where i.EventType in ('N', 'C')
And I get the results I want:Medication table:MedicationId StartDate EndDate GroupId------------ ----------------------- ----------------------- -----------2 2010-01-19 00:00:00.000 2010-01-19 16:28:02.000 58341 2008-05-05 00:00:00.000 2008-07-10 00:00:00.000 178248(2 row(s) affected)Instructins Table:InstructionId MedicationId StartDate EndDate------------- ------------ ----------------------- -----------------------15 1 2008-10-02 00:00:00.000 2008-12-23 14:00:47.00016 1 2009-03-16 00:00:00.000 2009-06-14 00:00:00.00017 1 2008-05-05 00:00:00.000 2008-07-10 00:00:00.00018 1 2008-07-10 00:00:00.000 2008-10-02 00:00:00.00019 2 2010-01-19 00:00:00.000 2010-01-19 16:28:02.00020 2 2010-03-02 00:00:00.000 2010-03-09 00:00:00.00021 2 2010-01-26 00:00:00.000 2010-02-25 00:00:00.000(7 row(s) affected)
But the tables are actually vender tables (I have been tasked with doing the conversion of our old system data into this new system) and I really don't want to change them. I would have to remove the GroupId column when I have completed the insert. The conversion might be a mutli stage thing and adding/deleting a column from the table could be either inconvenient or not possible. I am sure the application will fail big time if it is run against a modified table.I have glanced at the output clause but it looks like I can only output data that is also inserted into the table.So is there a way to do this that does not include modifying the tables.ThanksLaurie