SQL:在单个查询中使用三个SELECT语句

时间:2017-10-04 09:44:31

标签: sql-server select

我在一个完美运行的商店程序中有这个查询:

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放在选择输出的末尾

2 个答案:

答案 0 :(得分:3)

我们可以这样做:

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

答案 1 :(得分: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