将 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
___ *
___ ___