我有一个简单的 Products 数据表,其中包含 100k 条记录(仅生成一些随机数据):
set nocount on
create table dbo.temp_Products
(
[ProductID] int,
[Type] tinyint,
[Price] float,
[Weight] float
)
declare @rowcnt int = 0
while (@rowcnt <= 100000)
begin
insert into dbo.temp_Products
select @rowcnt, 1+rand()*4, 1+rand()*100, 1+rand()*10
set @rowcnt = @rowcnt + 1
end
Run Code Online (Sandbox Code Playgroud)
以及进行简单逻辑计算的标量值函数:
create function dbo.usvf_CalculateShipping
(
@PricePerKG float = 1,
@Type tinyint,
@WeightInKG float = 1
)
returns float
as
begin
return /*get the appropriate factor to apply*/
case
when @Type = 1 then 0.1 …Run Code Online (Sandbox Code Playgroud)