2011-02-12 31 views
9

是他們更有效的方式嗎?TSQL - 設置多個變量的有效方式

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) 

乾杯, -R

回答

20

,我覺得做一個查詢是一樣好,你會得到它:

select 
    @ShippingL = ShippingL, 
    @ShippingB = ShippingB, 
    @ShippingH = ShippingH, 
    @ShippingW = ShippingW 
from 
    AuctionProducts 
where 
    ProductID = @ProductID 

我會想象這是比快4倍您發佈的代碼。另外,請確保您在AuctionProducts表的ProductID列上定義了一個索引。

+3

是的,速度快4倍,因爲它只有一個表訪問。 – gbn 2011-02-12 10:00:17