Ayo EXEC!

Menulis Function dan Stored Procedure di SQL Server

Meghan Kwartler

IT Consultant

Cara EXECute

  • Tanpa parameter output atau nilai kembali
  • Simpan nilai kembali
  • Dengan parameter output
  • Dengan parameter output & simpan nilai kembali
  • Simpan hasil set
Menulis Function dan Stored Procedure di SQL Server

Tanpa parameter output atau nilai kembali

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

Dengan parameter output

DECLARE @RideHrs as numeric(18,0)

EXEC dbo.cuspSumRideHrsOneDay 
     @DateParm = '1/5/2017', 
     @RideHrsOut = @RideHrs OUTPUT
SELECT @RideHrs as TotalRideHrs
+--------------+
| TotalRideHrs |
|--------------+
| 77733        |
+--------------+
Menulis Function dan Stored Procedure di SQL Server

Dengan nilai kembali

Declare @ReturnValue as int

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

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

Dengan nilai kembali & parameter output

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        |
+-------------+----------+
Menulis Function dan Stored Procedure di SQL Server

EXEC & simpan 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       |
+------------+-----------+
Menulis Function dan Stored Procedure di SQL Server

Saatnya EXEC SP Anda!

Menulis Function dan Stored Procedure di SQL Server

Preparing Video For Download...