I am created an online staff rota application. I have a stored procedure for each day of the week with an auto increment primary key for the Rota table. Each day within the selected week will have the same rota ID as a foreign key.
I have no problem with the first selected day but the following days return NULL for the foreign key RotaID as I don't know how to pass the RotaID into the other stored procedures to ensure the RotaID remains the same for the 7 days of the weekly Rota.
CREATE PROCEDURE [usp_RotaDay1]
@Week_Begin datetime,
@Week_End datetime,
@1ShiftDate datetime,
@1Day nchar (10),
AS
BEGIN
SET NOCOUNT ON;
DECLARE @RotaID int
IF NOT EXISTS
(
SELECT * FROM Rota
WHERE Week_Begin = @Week_Begin
AND
Week_End = @Week_End
)
BEGIN
INSERT INTO Rota
(
[Week_Begin],
[Week_End]
)
VALUES
(
@Week_Begin,
@Week_End
)
END
SELECT @RotaID = SCOPE_IDENTITY()
IF NOT EXISTS
(
SELECT * FROM DayShift
WHERE ShiftDate = @1ShiftDate
)
BEGIN
INSERT INTO DayShift
(
[RotaID],
[ShiftDate],
[Day]
)
VALUES
(
@RotaID,
@1ShiftDate,
@1Day
)
END
SET NOCOUNT OFF;
END
As you can see I have the RotaID declared in the Rota table and then passed as the foreign key in the DayShift table.
I would like to know if it is possible to pass this through to my other stored procedures which are similar to this one.