查詢結果是多行明細時,取第一行的SQL腳本
select top 1 GoodsCode,PhaseFactory,* from TBOPhasein where GoodsCode = 'D69588' order by BillMakeDate desc
select top 1 PhaseFactory from TBOPhasein where GoodsCode = 'D69588' order by BillMakeDate desc
查詢結果是多行明細時,取第二行的SQL腳本
declare @LastbatchPhaseFactory varchar(10)
select top 1 @LastbatchPhaseFactory = a.PhaseFactory
from( select top 2 * from TBOPhasein where GoodsCode = 'D69588' order by BillMakeDate desc) a
order by a.BillMakeDate asc
select isnull(@LastbatchPhaseFactory,'')
或者
select top 1 a.PhaseFactory
from( select top 2 * from TBOPhasein where GoodsCode = 'D69588' order by BillMakeDate desc) a
order by a.BillMakeDate asc