將 TVF 結果寫入變數
還記得你在本章稍早建立的資料表值函式 SumStationStats 嗎?它接受一個 datetime 參數,並回傳起始站為每一個起點站、且開始日期符合輸入參數時的騎乘次數與總騎乘時間。現在請執行 SumStationStats,並把結果儲存在一個資料表變數中。
本練習屬於課程
在 SQL Server 中撰寫函式與預存程序
練習說明
- 建立一個名為
@StationStats的資料表變數,包含欄位StartStation、RideCount和TotalDuration。 - 執行
SumStationStats函式,並傳入'3/15/2018'作為輸入參數。 - 使用
INSERT INTO將函式的結果寫入@StationStats資料表變數。 - 從該資料表變數選取所有紀錄。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
-- Create @StationStats
___ ___ ___(
StartStation nvarchar(100),
RideCount int,
TotalDuration numeric)
-- Populate @StationStats with the results of the function
___ ___ ___
SELECT TOP 10 *
-- Execute SumStationStats with 3/15/2018
FROM dbo.___('3/15/2018')
ORDER BY RideCount DESC
-- Select all the records from @StationStats
___ *
___ ___