我一直在試圖使用存儲過程來計算服務的總成本,以在單獨的存儲過程中設置「成本」參數的值。使用存儲過程設置參數值
計算存儲過程按預期工作(當我運行它時返回正確的值),並且第二個存儲過程正在執行而沒有錯誤,但第一個存儲過程的值沒有被結轉(返回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
看到這個問題https://stackoverflow.com/questions/1605242/how-to-set-the-result-of-exec-存儲過程到變量 –
您需要在第一個存儲過程中使用返回值,以便它返回一個標量值,該值可以分配給變量 –
,您可以使用INSERT INTO table EXEC ...; OR RETURN; OR OUTPUT參數 –