2017-10-17 66 views
0

我一直在試圖使用存儲過程來計算服務的總成本,以在單獨的存儲過程中設置「成本」參數的值。使用存儲過程設置參數值

計算存儲過程按預期工作(當我運行它時返回正確的值),並且第二個存儲過程正在執行而沒有錯誤,但第一個存儲過程的值沒有被結轉(返回0在我的決賽桌上)。

這裏有兩個存儲過程:

ALTER PROCEDURE [dbo].[calculateCost] 
    (@VIN INT, 
     @BookingDate DATE, @BookingTime TIME(7), 
     @ReturnDate DATE, @ReturnTime TIME(7) 
    ) 
AS 
BEGIN 
    SET NOCOUNT ON; 

    SELECT 
     (CASE WHEN (DATEDIFF(hour, @BookingTime, @ReturnTime) * Vehicle.HourlyRate) <= 0 
       THEN 0 
       ELSE (DATEDIFF(hour, @BookingTime, @ReturnTime) * Vehicle.HourlyRate) 
     END) + DATEDIFF(day, @BookingDate, @ReturnDate) * Vehicle.DailyRate AS Cost 
    FROM 
     dbo.Vehicle 
    WHERE 
     VIN = @VIN 
END 

第二個存儲過程:

ALTER PROCEDURE [dbo].[updateBooking] 
    (@BookingID INT, @CustomerID INT, @VIN INT, 
     @BookingDate DATE, @BookingTime TIME(7), 
     @ReturnDate DATE, @ReturnTime TIME(7), 
     @OdoReadingInbound INT, 
     @Discount FLOAT, 
     @ObservationsIn NVARCHAR(50), 
     @EmpID INT 
    ) 
AS 
BEGIN 
    SET NOCOUNT ON; 

    DECLARE @Cost money 

    EXEC @Cost = dbo.calculateCost @BookingID, @BookingDate, @BookingTime, @ReturnDate, @ReturnTime 

    UPDATE dbo.Booking 
    SET CustomerID = @CustomerID, 
     VIN = @VIN, 
     BookingDate = @BookingDate, 
     BookingTime = @BookingTime, 
     ReturnDate = @ReturnDate, 
     ReturnTime = @ReturnTime, 
     OdoReadingInbound = @OdoReadingInbound, 
     Discount = @Discount, 
     ObservationsIn = @ObservationsIn, 
     EmpID = @EmpID, 
     Cost = @Cost 
    WHERE BookingID = @BookingID 
END 

我絕不用SQL所以任何幫助,將不勝感激感謝過於經歷,我已經試圖尋找這個問題我自己在過去的30分鐘左右無濟於事:)

編輯:現在我試圖使用OUT PUT變量,但是我仍然從第二個SP中獲得0作爲我的最終值。有人能指出我在這裏做錯了嗎?

ALTER PROCEDURE [dbo].[calculateCost] 
    -- Add the parameters for the stored procedure here 
    (@VIN int 
    , @BookingDate date 
    , @BookingTime time(7) 
    , @ReturnDate date 
    , @ReturnTime time(7) 
    , @Cost money OUTPUT 
    ) 
AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 

    -- Insert statements for procedure here 
    SELECT (CASE WHEN 
      (DATEDIFF(hour, @BookingTime, @ReturnTime) 
      * Vehicle.HourlyRate) <= 0 THEN 0 ELSE 
      (DATEDIFF(hour, @BookingTime, @ReturnTime) 
      * Vehicle.HourlyRate) END) 
      + DATEDIFF(day, @BookingDate, @ReturnDate) 
      * Vehicle.DailyRate AS Cost 
    FROM dbo.Vehicle 
    WHERE VIN = @VIN 
    RETURN 
END 

ALTER PROCEDURE [dbo].[updateBooking] 
-- Add the parameters for the stored procedure here 
    (@BookingID int 
    , @CustomerID int 
    , @VIN int 
    , @BookingDate date 
    , @BookingTime time(7) 
    , @ReturnDate date 
    , @ReturnTime time(7) 
    , @OdoReadingInbound int 
    , @Discount float 
    , @ObservationsIn nvarchar(50) 
    , @EmpID int 
    ) 
AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 

    -- Insert statements for procedure here 
    DECLARE @Cost money 
    EXEC @Cost = dbo.calculateCost @BookingID, @BookingDate, @BookingTime, @ReturnDate, @ReturnTime, @Cost OUTPUT 
    UPDATE dbo.Booking 
    SET CustomerID = @CustomerID, 
     VIN = @VIN, 
     BookingDate = @BookingDate, 
     BookingTime = @BookingTime, 
     ReturnDate = @ReturnDate, 
     ReturnTime = @ReturnTime, 
     OdoReadingInbound = @OdoReadingInbound, 
     Discount = @Discount, 
     ObservationsIn = @ObservationsIn, 
     EmpID = @EmpID, 
     Cost = @Cost 
    WHERE BookingID = @BookingID 
END 
+0

看到這個問題https://stackoverflow.com/questions/1605242/how-to-set-the-result-of-exec-存儲過程到變量 –

+0

您需要在第一個存儲過程中使用返回值,以便它返回一個標量值,該值可以分配給變量 –

+1

,您可以使用INSERT INTO table EXEC ...; OR RETURN; OR OUTPUT參數 –

回答

0
SELECT @COST = CASE WHEN 
     (DATEDIFF(hour, @BookingTime, @ReturnTime) 
     * Vehicle.HourlyRate) <= 0 THEN 0 ELSE 
     (DATEDIFF(hour, @BookingTime, @ReturnTime) 
     * Vehicle.HourlyRate) END 
     + DATEDIFF(day, @BookingDate, @ReturnDate) 
     * Vehicle.DailyRate AS Cost 
FROM dbo.Vehicle 
WHERE VIN = @VIN 
RETURN 

你可以嘗試這樣的calculateCost?

+0

感謝您的回覆,我試過這個,但得到一個錯誤「多部分標識符」Vehicle.CoulmnName「無法綁定」。 –

0

好吧,在擺弄了一會兒之後,我讓我的calculateCost SP在我的updateBooking SP之外正常工作。我遇到了一個錯誤"The multi-part identifier "Vehicle.CoulmnName" could not be bound.",我通過從查詢中刪除AS Cost來解決這個問題。我用下面的查詢來調試:

declare @Cost money 
exec dbo.calculateCost 7 
, '20170101' 
, '18:00:00' 
, '20170101' 
, '20:30:00' 
, @Cost output 
print @Cost 

哪打印出正確的值。但是,updateBooking SP仍然無法正常工作。再次閱讀後,我意識到我正在將@BookingID轉換爲calculateCost而不是@VIN,這就是爲什麼它沒有返回任何東西。這裏是兩個存儲過程的最終代碼,以防將來幫助任何人。

calculateCost:

ALTER PROCEDURE [dbo].[calculateCost] 
    -- Add the parameters for the stored procedure here 
    (@VIN int 
    , @BookingDate date 
    , @BookingTime time(7) 
    , @ReturnDate date 
    , @ReturnTime time(7) 
    , @Cost money OUTPUT 
    ) 
AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 

    -- Insert statements for procedure here 
    SELECT @Cost = (CASE WHEN 
      (DATEDIFF(hour, @BookingTime, @ReturnTime) 
      * Vehicle.HourlyRate) <= 0 THEN 0 ELSE 
      (DATEDIFF(hour, @BookingTime, @ReturnTime) 
      * Vehicle.HourlyRate) END) 
      + DATEDIFF(day, @BookingDate, @ReturnDate) 
      * Vehicle.DailyRate 
    FROM dbo.Vehicle 
    WHERE VIN = @VIN 
    RETURN 
END 

updateBooking:

ALTER PROCEDURE [dbo].[updateBooking] 
    -- Add the parameters for the stored procedure here 
    (@BookingID int 
    , @CustomerID int 
    , @VIN int 
    , @BookingDate date 
    , @BookingTime time(7) 
    , @ReturnDate date 
    , @ReturnTime time(7) 
    , @OdoReadingInbound int 
    , @Discount float 
    , @ObservationsIn nvarchar(50) 
    , @EmpID int 
    ) 
AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 

    -- Insert statements for procedure here 
    DECLARE @Cost money 
    EXEC dbo.calculateCost @VIN, @BookingDate, @BookingTime, @ReturnDate, @ReturnTime, @Cost OUTPUT 
    UPDATE dbo.Booking 
    SET CustomerID = @CustomerID, 
     VIN = @VIN, 
     BookingDate = @BookingDate, 
     BookingTime = @BookingTime, 
     ReturnDate = @ReturnDate, 
     ReturnTime = @ReturnTime, 
     OdoReadingInbound = @OdoReadingInbound, 
     Discount = @Discount, 
     ObservationsIn = @ObservationsIn, 
     EmpID = @EmpID, 
     Cost = @Cost 
    WHERE BookingID = @BookingID 
END 
+0

看起來不錯:-)剛剛完成了一個類似的解決方案。但是,在大多數情況下,我更喜歡功能而不是SP。 :-D – Tyron78

+0

@ Tyron78謝謝你的提示!我一定會考慮它以備將來使用。我被要求爲此做一個存儲過程,所以我決定堅持這些要求:)。 –