SP中的多个选择以返回一个记录集

时间:2014-12-04 12:32:08

标签: sql sql-server stored-procedures

我正在使用MS SQL并且有一段代码需要多个基本的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 DriverID=@driverid
   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

1 个答案:

答案 0 :(得分:3)

假设它们每个只返回一行,您可以使用CROSS JOIN来组合它们:

SELECT *
FROM (SELECT [JM ID] FROM Drivers WHERE DriverID=@driverid) 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;

如果他们返回多个值,那么您需要解释结果如何组合成一行。