Assume that the diagram consists of the following tables:
Baz
BazID (PK, Identity) Description
FooTemplate (Baz can have zero for many FooTemplates)
FooTemplateID (PK, Identity) BazID (FK) Description NextGenerationDate
BarTemplate (FooTemplate can have zero for many BarTemplates)
BarTemplateID (PK, Identity) FooTemplateID (FK) Description
Foo (Baz can have zero before many Foos)
FooID (PK, Identity) BazID (FK) Description
Bar (A Foo can have zero for many bars)
BarID (PK, Identity) FooID (FK) Description
Each day, the stored procedure will be executed to generate Foo and Bar objects for the associated Baz object, which passed the next generation date.
The first part of this procedure looks something like this:
DECLARE @GeneratedFooIDList TABLE (INT FooID); INSERT Foo (BazID, Description) OUTPUT inserted.FooID INTO @GeneratedFooIDList SELECT BazID Description FROM FooTemplate WHERE NextGenerationDate < GETDATE()
My question is, what kind of statement can I execute to create the correct Bar entities, and are they linked correctly with the newly created Foo objects?
EDIT: The procedure will run on a server running SQL Server 2005.
EDIT2: Thanks everyone for the help. After carefully studying the information, I chose another solution. I changed the primary key in the Foo table so that it would no longer be an automatically generated identity column, so that intermediate insertion into the temporary table could be done to capture the corresponding FooTemplateID along with the FooID