Writing Functions and Stored Procedures in SQL Server
Meghan Kwartler
IT Consultant
What?
Routines that
EXECUTE
SELECT
, INSERT
, UPDATE
, DELETE
, and other SP statements) Why?
UDFs
SELECT
execute allowed INSERT
, UPDATE
, DELETE
SPs
SELECT
to executeINSERT
, UPDATE
, DELETE
allowedTRY...CATCH
-- First four lines of code
-- SP name must be unique
CREATE PROCEDURE dbo.cuspGetRideHrsOneDay
@DateParm date,
@RideHrsOut numeric OUTPUT
AS
.......
CREATE PROCEDURE dbo.cuspGetRideHrsOneDay
@DateParm date,
@RideHrsOut numeric OUTPUT
AS
SET NOCOUNT ON
BEGIN
SELECT
@RideHrsOut = SUM(
DATEDIFF(second, PickupDate, DropoffDate)
)/ 3600
FROM YellowTripData
WHERE CONVERT(date, PickupDate) = @DateParm
RETURN
END;
Output parameters
Return value
Writing Functions and Stored Procedures in SQL Server