I'm attempting to track items in commercial building construction (specifically concrete cladding).
- A project can have multiple blocks (a block being a single standing building)
- A block can have multiple elevations (building face; north, east, etc)
- An elevation can have many floors (a tall building)
- A floor can have many panels (a panel being a section of concrete cladding)
To make things easier when building the panels in the database so they can be tracked, I want to be able copy a block (since 9 times out of 10, each block is the same with minor changes)
As far as tables go -
Blocks
Blocks.BlockID,
Blocks.BlockName,
Blocks.BlockDescription,
Projects.ProjectID
Elevations
Elevations.ElevationID,
Elevations.ElevationName,
Elevations.ElevationDescription,
Blocks.BlockID
Floors
Floors.FloorID,
Floors.FloorName,
Floors.FloorDescription,
Elevations.ElevationID
Panels
Panels.PanelID,
Panels.PanelName,
Panels.PanelDescription,
Floors.FloorID
If I copy a block it needs to
- Copy the current block, but the user should be able to choose Blocks.name.
- Copy all of the elevations on that block
- Copy all floors on each of those elevations
- Copy all panels on each of those floors
Let me know if you need to know anything else, thanks for any help in advance!
My Attempt (after previous advice)
DECLARE @blockToCopy int = 1
DECLARE @BlockOutput table
( BlockID int);
DECLARE @ElevationsOutput table
( ElevationID int, BlockID int );
DECLARE @FloorsOutput table
( FloorID int, ElevationID int );
DECLARE @ItemsOutput table
( ItemID int, FloorID int );
INSERT INTO Blocks
(ProjectID,BlockName,BlockDescription)
OUTPUT
INSERTED.BlockID
INTO @BlockOutput
SELECT ProjectID,'NewNameTest','NewDescTest'
from Blocks
WHERE BlockID=@blockToCopy
INSERT INTO Elevations
(BlockID,ElevationName,ElevationDescription)
OUTPUT
INSERTED.ElevationID,
INSERTED.BlockID
INTO @ElevationsOutput
SELECT (SELECT BlockID from @BlockOutput),ElevationName,ElevationDescription
from Elevations
WHERE BlockID=@blockToCopy
INSERT INTO Floors
(ElevationID,FloorName,FloorDescription)
OUTPUT
INSERTED.FloorID,
INSERTED.ElevationID
INTO @FloorsOutput
SELECT (SELECT top 1 ElevationID from @ElevationsOutput order by ElevationID desc), FloorName,FloorDescription
from Floors
WHERE ElevationID in (SELECT ElevationID from @ElevationsOutput)
It goes wrong after the second cascade (where trying to copy all floors within each elevation). I know why it's going wrong (instead of listing the original IDs alongside the new IDs to copy to, I'm outputting the new IDs by themselves so it has nothing to copy because they don't exist yet) but I don't know how to fix it.