TSQL - 设置多个变量的有效方法

Rya*_*Rya 9 t-sql performance

他们是一个更有效的方式吗?

set @ShippingL = (select ShippingL from AuctionProducts where ProductID = @ProductID)
set @ShippingB = (select ShippingB from AuctionProducts where ProductID = @ProductID)
set @ShippingH = (select ShippingH from AuctionProducts where ProductID = @ProductID)
set @ShippingW = (select ShippingW from AuctionProducts where ProductID = @ProductID)
Run Code Online (Sandbox Code Playgroud)

干杯,-R

she*_*tie 20

我认为做一个查询就像你会得到它一样好:

select 
  @ShippingL = ShippingL,
  @ShippingB = ShippingB,
  @ShippingH = ShippingH,
  @ShippingW = ShippingW 
from 
  AuctionProducts 
where
  ProductID = @ProductID 
Run Code Online (Sandbox Code Playgroud)

我想这比你发布的代码快4倍.此外,请确保在AuctionProducts表的ProductID列上定义了索引.

  • 是的,快4倍,因为它只能访问一个表. (3认同)