Let's EXEC!

Writing Functions and Stored Procedures in SQL Server

Meghan Kwartler

IT Consultant

Ways to EXECute

  • No output parameter or return value
  • Store return value
  • With output parameter
  • With output parameter & store return value
  • Store result set
Writing Functions and Stored Procedures in SQL Server

No output parameter or return value

EXEC dbo.cusp_TripSummaryUpdate 
     @TripDate = '1/5/2017'
     @TripHours = '300'

With output parameter

DECLARE @RideHrs as numeric(18,0)

EXEC dbo.cuspSumRideHrsOneDay 
     @DateParm = '1/5/2017', 
     @RideHrsOut = @RideHrs OUTPUT
SELECT @RideHrs as TotalRideHrs
+--------------+
| TotalRideHrs |
|--------------+
| 77733        |
+--------------+
Writing Functions and Stored Procedures in SQL Server

With return value

Declare @ReturnValue as int

EXEC @ReturnValue = 
     dbo.cusp_TripSummaryUpdate 
     @TripDate = '1/5/2017', 
     @TripHours = 300

Select @ReturnValue as ReturnValue
+-------------+
| ReturnValue |
|-------------+
| 0           |
+-------------+

With return value & output parameter

Declare @ReturnValue as int
Declare @RowCount as int

EXEC @ReturnValue = 
     dbo.cusp_TripSummaryDelete 
     @TripDate = '1/5/2017', 
     @RowCountOut = @RowCount OUTPUT

Select @ReturnValue as ReturnValue, 
     @RowCount as RowCount
+-------------+----------+
| ReturnValue | RowCount |
|-------------+----------|
| 0           | 1        |
+-------------+----------|
Writing Functions and Stored Procedures in SQL Server

EXEC & store result set

DECLARE @TripSummaryResultSet as TABLE(
        TripDate date,
        TripHours numeric(18,0))

INSERT INTO @TripSummaryResultSet EXEC cusp_TripSummaryRead @TripDate = '1/5/2017'
SELECT * FROM @TripSummaryResultSet
+------------+-----------+
| TripDate   | TripHours |
|------------+-----------|
| 2017-01-05 | 200       |
+------------+-----------|
Writing Functions and Stored Procedures in SQL Server

Time to EXEC your SPs!

Writing Functions and Stored Procedures in SQL Server

Preparing Video For Download...