2014-12-04 78 views
0

Im使用MS SQL,並有一段代碼需要多個基本SP來運行我想要做的是將它們壓縮到一個。在SP中選擇多個返回一個記錄集

ALTER PROCEDURE SP_JOB_IMPORT_MULTICALL 
(
@driverid as int, 
@vehicleid as int, 
@place as nvarchar(50) 
) 
AS 
DECLARE @imval as int 

BEGIN 
    SET @imval = (SELECT ad.ImportValue FROM Admin AS ad WHERE ad.ID=1) +1 
END 

BEGIN 
    SELECT DriverID,[JM ID] FROM Drivers WHERE [email protected] 
    SELECT [Unit ID],[External JM ID] FROM Vehicles WHERE [Unit ID] = @VehicleID 
    SELECT ImportValue FROM admin WHERE ID=1 
    SELECT Dwelltime FROM Places WHERE Placename = @place 
    SELECT [JM External ID] FROM Places WHERE Placename = @place 
END 
    UPDATE ADMIN SET ImportValue = @IMVAL WHERE ID=1 


    RETURN 

我怎麼能做到這一點,並返回它作爲一個記錄集即

驅動ID,JM ID,組ID,外部JM ID,進口額,停留時間,JM外部ID

1 ------------ 1 -------- 1 -------- 1 ----------------- --- 1 ----------------- 1 -------------- 1

回答

3

假設他們每個只返回一行,你可以用CROSS JOIN來組合它們:

SELECT * 
FROM (SELECT [JM ID] FROM Drivers WHERE [email protected]) d CROSS JOIN 
    (SELECT [Unit ID],[External JM ID] FROM Vehicles WHERE [Unit ID] = @VehicleID) v CROSS JOIN 
    (SELECT ImportValue FROM admin WHERE ID=1) a CROSS JOIN 
    (SELECT Dwelltime FROM Places WHERE Placename = @place) p CROSS JOIN 
    (SELECT [JM External ID] FROM Places WHERE Placename = @place) p2; 

如果它們返回多個值,那麼您需要解釋如何將結果合併到一行中。

+1

@ user3652973。 。 。我誤解了這一點。我的意思是一行(和一行)。 – 2014-12-04 13:21:19

+0

非常感謝 – user3652973 2014-12-04 13:22:40

+0

@GordonLinoff我很好奇,如果在休息時使用null的聯盟會更好? – brykneval 2014-12-04 13:24:17

相關問題