2017-10-04 73 views
2

我一個存儲過程中有這樣的查詢中使用三個select語句,其完美的作品:SQL:在一個查詢

select dbo.fn_GetSid(@Cid,sid) as SIdPrfx, 
     dbo.fn_GetPId(@Cid, sid, pid) as PIdPrefix, 
     * 
from #tempsafety 
where (createddate >= @dateFrom or @dateFrom is null) and 
     (createddate<= @dateTo or @dateTo is null) 
order by createddate desc 

但我想改變順序,把SIdPrfx和PIdPrefix中的結束選擇輸出

回答

3

我們可以簡單地這樣做:

SELECT 
    *, 
    dbo.fn_GetSid(@Cid,sid) as SIdPrfx, 
    dbo.fn_GetPId(@Cid, sid, pid) as PIdPrefix 
FROM #tempsafety 
-- -- -- 
+2

大聲笑,最簡單的解決方案,它的工作:)假設OP已經嘗試這 – Andomar

+0

以及我試過,但像這樣select * from #tempsafety,dbo.fn_GetSid(.... – alxem

1

你也許能夠讓他們進入最後一個席位與outer apply

select * 
from #tempsafety 
outer apply 
     (
     select dbo.fn_GetSid(@Cid,sid) as SIdPrfx, 
       dbo.fn_GetPId(@Cid, sid, pid) as PIdPrefix 
     ) sub 
where (createddate >= @dateFrom or @dateFrom is null) and 
     (createddate<= @dateTo or @dateTo is null) 
order by createddate desc