Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Stored procedure to create loan
- CREATE PROCEDURE uspCreateLoan (@userID INT, @itemID INT, @employeeID INT)
- AS
- BEGIN
- DECLARE @currentLoans INT = dbo.fnGetUserLoans(@userID) --Declaring variable currentLoans as the current amount of active loans from the user
- IF (SELECT maxItems FROM LibraryUser WHERE userID = @userID) > @currentLoans --Check if user has not surpassed max items they may loan at once
- BEGIN
- IF (SELECT categoryName FROM Category --Do not let the user loan reference literature
- JOIN Item ON Item.CategoryID = Category.categoryID
- WHERE Item.itemID = @itemID) NOT LIKE 'Reference Literature'
- BEGIN
- IF (SELECT avaliable FROM Item WHERE itemID = @itemID) = 1 --If the item the user wants is avaliable then create a loan for that item
- BEGIN
- DECLARE @dateAdd INT --Declaring dateadd and dueDate variables
- DECLARE @dueDate DATE
- SET @dateAdd = (SELECT maxLoanAllowance FROM Category --Setting dateAdd to the max amount of time you can loan that item
- JOIN Item ON Item.CategoryID = Category.categoryID
- WHERE Item.itemID = @itemID)
- SET @dueDate = GETDATE()+DATEADD(week,@dateAdd,@itemID) --calculating the due date by adding the current date with dateAdd
- INSERT INTO Loan (userID,employeeID,dueDate,itemID,returned) --Creating a new row in Loan with arguments supplied by the caller
- VALUES (@userID,@employeeID,@dueDate,@itemID,0)
- PRINT 'Thank you!'
- UPDATE Item SET avaliable = 0 WHERE itemId = @itemID --Setting item as unavaliable so it may not be loaned.
- RETURN
- END
- PRINT 'Unsuccessful, Item Unavaliable.'
- RETURN
- END
- PRINT 'You may not loan reference literature'
- RETURN
- END
- PRINT 'You have reached your loan limit. Return past loans.'
- END
- GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement