Stored Procedure
Create a Stored Procedure named cuspSumRideHrsSingleDay
in the dbo
schema that accepts a date and returns the total ride hours for the date passed.
CREATE PROCEDURE dbo.cuspSumRideHrsSingleDay
-- Declare the input parameter
@DateParm date,
-- Declare the output parameter
@RideHrsOut numeric OUTPUT
AS
-- Don't send the row count
SET NOCOUNT ON
BEGIN
-- Assign the query result to @RideHrsOut
SELECT
@RideHrsOut = SUM(DATEDIFF(second, StartDate, EndDate))/3600
FROM CapitalBikeShare
-- Cast StartDate as date and compare with @DateParm
WHERE CAST(StartDate AS date) = @DateParm
RETURN
END
CRUD
Create a stored procedure named cusp_RideSummaryCreate
in the dbo
schema that will insert a record into the RideSummary
table.
CREATE PROCEDURE dbo.cusp_RideSummaryCreate
(@DateParm date, @RideHrsParm numeric)
AS
BEGIN
SET NOCOUNT ON
-- Insert into the Date and RideHours columns
INSERT INTO dbo.RideSummary(Date, RideHours)
-- Use values of @DateParm and @RideHrsParm
VALUES(@DateParm, @RideHrsParm)
-- Select the record that was just inserted
SELECT
-- Select Date column
Date,
-- Select RideHours column
RideHours
FROM dbo.RideSummary
-- Check whether Date equals @DateParm
WHERE Date = @DateParm
END;
Use SP to UPDATE
Create a stored procedure named cuspRideSummaryUpdate
in the dbo
schema that will update an existing record in the RideSummary
table.
CREATE PROCEDURE dbo.cuspRideSummaryUpdate
-- Specify @Date input parameter
(@Date date,
-- Specify @RideHrs input parameter
@RideHrs numeric(18,0))
AS
BEGIN
SET NOCOUNT ON
-- Update RideSummary
UPDATE RideSummary
-- Set
SET
Date = @Date,
RideHours = @RideHrs
-- Include records where Date equals @Date
WHERE Date = @Date
END;
Comments
Post a Comment