Assume a schema that consists of the following tables:
Baz
BazID (PK, Identity)
Description
FooTemplate (A Baz may have zero to many FooTemplates)
FooTemplateID (PK, Identity)
BazID (FK)
Description
NextGenerationDate
BarTemplate (A FooTemplate may have zero to many BarTemplates)
BarTemplateID (PK, Identity)
FooTemplateID (FK)
Description
Foo (A Baz may have zero to many Foos)
FooID (PK, Identity)
BazID (FK)
Description
Bar (A Foo may have zero to many Bars)
BarID (PK, Identity)
FooID (FK)
Description
Each day a stored procedure will execute to generate Foo
and Bar
entities for an associated Baz
entity which have passed their next generation date.
The first part of this procedure looks a little 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 statement can I now execute to generate the proper Bar
entities and have them properly associated with the newly created Foo
entities?
EDIT: The procedure will be executing on a server running SQL Server 2005.
EDIT2: Thanks to everybody for the help. After considering the information carefully, I have opted for another solution. I have changed the primary key in the Foo table to no longer be an automatically generated identity column, this way an intermediary insert into a temporary table could be executed to capture the relevant FooTemplateID along with the FooID