All parameters for a procedure are Input parameters. If you declare a parameter as an OUTPUT
parameter, it is still an input one, and if it doesn't have a default value must be supplied.
If you want the OUTPUT
parameters to be option, which I personally find can be quite often, then give them a default value. I also add some additional logic to your procedure, as you should be using an TRY...CATCH
and an ORDER BY
in your query with a TOP
.
CREATE PROC dbo.spBookReturn @loanID uniqueidentifier,
@bookID uniqueidentifier = NULL OUTPUT,
@custID uniqueidentifier = NULL OUTPUT
AS
BEGIN
BEGIN TRY --If you are using tranasctions, make sure you have a ROLLBACK and THROW for errors
BEGIN TRANSACTION tBookReturn
UPDATE BorrowedMaterial
SET returned = 1,
returnedDate = GETDATE()
WHERE loanID = @loanID;
/*
UPDATE BorrowedMaterial
SET returnedDate = GETDATE()
WHERE loanID = @loanID;
*/
SET @bookID = (SELECT TOP 1 bookID
FROM BorrowedMaterial
WHERE loanID = @loanID
ORDER BY ???); --A TOP should have an ORDER BY
UPDATE Books
SET nHome = nHome + 1
WHERE ID = @bookID;
COMMIT TRANSACTION tBookReturn;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION tBookReturn;
THROW;
END CATCH;
END;
Then you can execute the procedure as you have, without @bookID
and @custID
being passed. Of course, if you don't, their values will be "lost" in the calling statement. If you need them, then pass their values too in the EXEC
:
DECLARE @bookID uniqueidentifier, @CustID uniqueidentifier;
EXEC dbo.spBookReturn @loanID, @bookID OUTPUT, @CustID OUTPUT;
--SELECT @bookID, @CustID;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…