Good morning Gurus!
I wrote my first stored procedured and I'd like your expert opinion on it. Just in case I need to do any fine tuning. It is a combination of insert and update. At 24 months, if an Instructor has participated in teaching a class either as a primary Instructor or an Assistant, a new certification record is inserted with a certification status = 1 then the previous certification is deactivated by updating certification status = 2.
The ExportHspAutoRecerts is a view based on class count for the Instructor. It detects if they ever participated in a class between their last certification date and getdate().
This stored procedure seems to to work great and I included it a DTS Package that exports a text file for printing new cards and exports a report that lists all Instructors who have been recertified. I plan to schedule a DTS run to automate it for end of month. Thanks!!
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/*
Creates a new Hunter Safety Program certification. The new record is created at then end of month if
Instructor has participated in teaching a class within 24 months of their last active certification date.
*/
CREATE PROCEDURE [usp_HspAutoRecertification]
AS
SET NOCOUNT ON
DECLARE @Error INT
DECLARE @RowCount INT
IF NOT EXISTS (SELECT * FROM ExportHspAutoRecerts)
SELECT @Error = @@ERROR, @RowCount = @@ROWCOUNT
IF @Error > 0 OR @RowCount = 0
BEGIN
RAISERROR('No recertifications present.', 16, 1 )
END
ELSE
BEGIN
INSERT INTO [HS_PROG].[dbo].[Instructor_Certifications]
( [instID],
[certificationType],
[certificationDate],
[racfID] )
SELECT
ExportHspAutoRecerts.[instID],
ExportHspAutoRecerts.[certificationType],
ExportHspAutoRecerts.[currCertDate] AS certificationDate,
ExportHspAutoRecerts.[recertJob] AS racfID
FROM ExportHspAutoRecerts
END
--Deactivates previous new Hunter Safety Program certification.
UPDATE [HS_PROG].[dbo].[Instructor_Certifications]
SET certificationStatus = 2
FROM Instructor_Certifications IC INNER JOIN ExportHspAutoRecerts ER
ON IC.certificationID = ER.certificationID
SELECT @Error = @@ERROR, @RowCount = @@ROWCOUNT
IF @Error > 0 OR @RowCount <> 1
BEGIN
RAISERROR('Certification was not deactivated.', 16, 1 )
END
IF @Error > 0 or @RowCount <>1
RETURN 1 --Error
ELSE
RETURN 0 --Success
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
I wrote my first stored procedured and I'd like your expert opinion on it. Just in case I need to do any fine tuning. It is a combination of insert and update. At 24 months, if an Instructor has participated in teaching a class either as a primary Instructor or an Assistant, a new certification record is inserted with a certification status = 1 then the previous certification is deactivated by updating certification status = 2.
The ExportHspAutoRecerts is a view based on class count for the Instructor. It detects if they ever participated in a class between their last certification date and getdate().
This stored procedure seems to to work great and I included it a DTS Package that exports a text file for printing new cards and exports a report that lists all Instructors who have been recertified. I plan to schedule a DTS run to automate it for end of month. Thanks!!
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/*
Creates a new Hunter Safety Program certification. The new record is created at then end of month if
Instructor has participated in teaching a class within 24 months of their last active certification date.
*/
CREATE PROCEDURE [usp_HspAutoRecertification]
AS
SET NOCOUNT ON
DECLARE @Error INT
DECLARE @RowCount INT
IF NOT EXISTS (SELECT * FROM ExportHspAutoRecerts)
SELECT @Error = @@ERROR, @RowCount = @@ROWCOUNT
IF @Error > 0 OR @RowCount = 0
BEGIN
RAISERROR('No recertifications present.', 16, 1 )
END
ELSE
BEGIN
INSERT INTO [HS_PROG].[dbo].[Instructor_Certifications]
( [instID],
[certificationType],
[certificationDate],
[racfID] )
SELECT
ExportHspAutoRecerts.[instID],
ExportHspAutoRecerts.[certificationType],
ExportHspAutoRecerts.[currCertDate] AS certificationDate,
ExportHspAutoRecerts.[recertJob] AS racfID
FROM ExportHspAutoRecerts
END
--Deactivates previous new Hunter Safety Program certification.
UPDATE [HS_PROG].[dbo].[Instructor_Certifications]
SET certificationStatus = 2
FROM Instructor_Certifications IC INNER JOIN ExportHspAutoRecerts ER
ON IC.certificationID = ER.certificationID
SELECT @Error = @@ERROR, @RowCount = @@ROWCOUNT
IF @Error > 0 OR @RowCount <> 1
BEGIN
RAISERROR('Certification was not deactivated.', 16, 1 )
END
IF @Error > 0 or @RowCount <>1
RETURN 1 --Error
ELSE
RETURN 0 --Success
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO