This document contains the code for several stored procedures used in a library database:
1) It defines procedures for adding adult members, adding items/copies to the library catalog, checking items in and out, and retrieving member and item loan information.
2) The procedures perform validation checks and make transactions atomic by committing or rolling back based on the success of data modifications.
3) Stored procedures streamline common database tasks, promote consistency, and help enforce data integrity constraints for the library application.
Scaling API-first – The story of a global engineering organization
SQL Stored Procedures For My Library Project
1. --This procedure adds an adult member to
--the dbo.member and dbo.adult tables.
CREATE PROCEDURE [dbo].[AddAdultMember]
@FirstName nvarchar (15),
@MiddleInitial nvarchar (1) = NULL,
@LastName nvarchar (15),
@Street nvarchar (15),
@City nvarchar (15),
@State nchar (2),
@ZipCode nvarchar (10),
@PhoneNumber nvarchar (15) = NULL,
@MemberID int OUTPUT
AS
DECLARE @ExprDate datetime
SET @ExprDate = DATEADD(year,1,GETDATE())
--Test for null values.
IF @FirstName is NULL OR @LastName is NULL
OR @Street is NULL OR @City is NULL
OR @State is NULL OR @ZipCode is NULL
BEGIN
RAISERROR('Invalid data entry.',11,1)
RETURN
END
--Add the new member to the dbo.member table.
--Then add that member as a child
--record to the adult table.
BEGIN TRANSACTION
INSERT dbo.member
(lastname,firstname,middleinitial)
VALUES (@LastName,@FirstName,@MiddleInitial)
--Test for successful insertion.
if @@ERROR <> 0
BEGIN
RAISERROR('Error, member not added.',11,1)
ROLLBACK TRANSACTION
RETURN
END
SET @MemberID = Scope_Identity();
INSERT dbo.adult
(member_no,street,city,[state],zip,phone_no,expr_date)
VALUES (@MemberID,@Street,@City,@State,@ZipCode,@PhoneNumber,@ExprDate)
if @@ERROR <> 0
BEGIN
RAISERROR('Error, member not added.',11,1)
ROLLBACK TRANSACTION
RETURN
END
COMMIT TRANSACTION
2. --This procedure adds a new item to
--the library. ISBN, copy #, Title,
--Title #, translation, cover, author,
--loanable, and synopsis fields are
--required. If an ISBN already exists
--in the library a new copy # is taken out.
CREATE PROCEDURE [dbo].[AddItem]
@ISBN int,
@Translation nvarchar (8) = NULL,
@Cover nvarchar (8) = NULL,
@Title nvarchar (63),
@Author nvarchar (31),
@Synopsis text = NULL,
@Loanable nchar (1),
@CopyNo smallint OUTPUT
AS
--Test for null values.
IF @ISBN is NULL OR @Loanable is NULL
OR @Title is NULL OR @Author is NULL
BEGIN
RAISERROR('Invalid data entry.',11,1)
RETURN
END
--Test to see if the item exists in library.
DECLARE @ItemCount int
SELECT @ItemCount = count(*) FROM dbo.item as it
WHERE it.isbn = @ISBN
IF @ItemCount < 1 --The item doesn't exist so add it.
BEGIN
BEGIN TRANSACTION
DECLARE @TitleNo int
--Add record to the title table first.
INSERT dbo.title
(title,author,synopsis)
VALUES (@Title,@Author,@Synopsis)
IF @@Error <> 0
BEGIN
RAISERROR('Cannot add item.',11,1)
ROLLBACK TRANSACTION
RETURN
END
SET @TitleNo = @@IDENTITY
--Add record to the item table next.
INSERT dbo.item
(isbn,title_no,translation,cover,loanable)
VALUES (@ISBN,@TitleNo,@Translation,@Cover,@Loanable)
IF @@ERROR <> 0
BEGIN
RAISERROR('Cannot add item.',11,1)
ROLLBACK TRANSACTION
RETURN
END
--Add record to the copy table next.
SET @CopyNo = 1
INSERT dbo.copy
(isbn,copy_no,title_no,on_loan)
VALUES (@ISBN,@CopyNo,@TitleNo,'N')
IF @@ERROR <> 0
BEGIN
3. RAISERROR('Cannot add item.',11,1)
ROLLBACK TRANSACTION
RETURN
END
COMMIT TRANSACTION
END
ELSE
BEGIN --The item already exists. Add another copy.
--First determine what the title number is.
SELECT @TitleNo = title_no FROM dbo.item
WHERE isbn = @ISBN
--Then determine what the copy number should be
--by counting how many copies already exist.
DECLARE @CopyCount int
SELECT @CopyCount = count(*) FROM dbo.copy as co
WHERE co.isbn = @isbn
--The latest copy # should be the count + 1.
SET @CopyNo = @CopyCount + 1
BEGIN TRANSACTION
--Add record to the copy table.
INSERT dbo.copy
(isbn,copy_no,title_no,on_loan)
VALUES (@ISBN,@CopyNo,@TitleNo,'N')
IF @@ERROR <> 0
BEGIN
RAISERROR('Cannot add item.',11,1)
ROLLBACK TRANSACTION
RETURN
END
COMMIT TRANSACTION
END
4. CREATE PROCEDURE [dbo].[AddJuvenileMember]
@FirstName nvarchar (15),
@MiddleInitial nvarchar (1) = NULL,
@LastName nvarchar (15),
@AdultMemberNo int,
@BirthDate datetime,
@MemberID int OUTPUT
AS
--Test for null values.
IF @FirstName is NULL OR @LastName is NULL
OR @AdultMemberNo is NULL OR @BirthDate is NULL
BEGIN
RAISERROR('Invalid data entry.',11,1)
RETURN
END
--Test for valid adult member ID.
DECLARE @Exists int
SELECT @Exists = COUNT(*) FROM adult
WHERE member_no = @AdultMemberNo
IF @Exists < 1 --A valid adult does not exist.
BEGIN
RAISERROR('Valid adult member does not exist.',11,1)
RETURN
END
--Test for valid birthday.
IF (@BirthDate > GETDATE()) or (@BirthDate < DATEADD(year,-18,GETDATE()))
BEGIN
RAISERROR('Invalid birth date',11,1)
RETURN
END
DECLARE @ExprDate datetime
SET @ExprDate = DATEADD(year,1,GETDATE())
--Add the new member to the dbo.member table.
--Then add that member as a child
--record to the juvenile table.
BEGIN TRANSACTION
INSERT dbo.member
(lastname,firstname,middleinitial)
VALUES (@LastName,@FirstName,@MiddleInitial)
--Test for successful insertion.
if @@ERROR <> 0
BEGIN
RAISERROR('Error, member not added.',11,1)
ROLLBACK TRANSACTION
RETURN
END
SET @MemberID = Scope_Identity();
INSERT dbo.juvenile
(member_no,adult_member_no,birth_date)
VALUES (@MemberID,@AdultMemberNo,@BirthDate)
5. if @@ERROR <> 0
BEGIN
RAISERROR('Error, member not added.',11,1)
ROLLBACK TRANSACTION
RETURN
END
COMMIT TRANSACTION
6. --This procedure checks in an item by
--removing a record from the loan table
--and adding a record to the loanhist table.
--It also updates the on_loan field
--in the copy table to 'N'.
CREATE PROCEDURE [dbo].[CheckInItem]
@isbn int,
@CopyNo smallint
AS
--Test for null values.
IF @isbn is NULL OR @CopyNo is NULL
BEGIN
RAISERROR('Check in not successful. Invalid data entry.',11,1)
RETURN
END
--Test to see if the item exists in library.
DECLARE @ItemCount int
SELECT @ItemCount = count(*) FROM [dbo].[item] as it
JOIN [dbo].[copy] as co ON
it.isbn = co.isbn
WHERE it.isbn = @isbn AND co.copy_no = @CopyNo
IF @ItemCount < 1
BEGIN
RAISERROR('Check in not successful. Item does not exist in this
library.',12,1)
RETURN
END
--Test to see if the item is not on loan.
DECLARE @OnLoan nvarchar (1)
SELECT @OnLoan = on_loan FROM [dbo].[copy]
WHERE isbn = @isbn AND copy_no = @CopyNo
IF @OnLoan = 'N'
BEGIN
RAISERROR('Check in not successful. Item is not currently checked out.',13,1)
RETURN
END
--Retrieve the title number.
DECLARE @TitleNo int
SELECT @TitleNo = title_no FROM [dbo].[copy]
WHERE isbn = @isbn AND copy_no = @CopyNo
--Retrieve the member ID.
--Retrieve the check out date.
--Retrieve the due date.
DECLARE @MemberID int, @OutDate datetime, @DueDate datetime
SELECT @MemberID = member_no, @OutDate = out_date, @DueDate = due_date FROM [dbo].[loan]
WHERE isbn = @isbn AND copy_no = @CopyNo
--Set the check in date as today.
DECLARE @InDate datetime
SET @InDate = GETDATE()
--Delete the record in the loan table.
--Change the on_loan field to 'N' in the copy table.
--Add a record in the loanhist table.
BEGIN TRANSACTION
7. UPDATE [dbo].[copy]
SET on_loan = 'N'
WHERE isbn = @isbn AND copy_no = @CopyNo
if @@ERROR <> 0
BEGIN
RAISERROR('Error, item not checked in.',11,1)
ROLLBACK TRANSACTION
RETURN
END
DELETE [dbo].[loan]
WHERE isbn = @isbn AND copy_no = @CopyNo
if @@ERROR <> 0
BEGIN
RAISERROR('Error, item not checked in.',11,1)
ROLLBACK TRANSACTION
RETURN
END
INSERT loanhist
(isbn,copy_no,out_date,title_no,member_no,due_date,in_date)
VALUES (@isbn,@CopyNo,@OutDate,@TitleNo,@MemberID,@DueDate,@InDate)
if @@ERROR <> 0
BEGIN
RAISERROR('Error, item not checked in.',11,1)
ROLLBACK TRANSACTION
RETURN
END
COMMIT TRANSACTION
8. --This procedure checks out an item by
--adding a record to the loan table.
--It also updates the on_loan field
--in the copy table to 'Y'.
CREATE PROCEDURE [dbo].[CheckOutItem]
@MemberID int,
@isbn int,
@CopyNo smallint
AS
--Test for null values.
IF @MemberID is NULL OR @isbn is NULL OR @CopyNo is NULL
BEGIN
RAISERROR('Invalid data entry.',11,1)
RETURN
END
--Test to see if the item exists in library.
DECLARE @ItemCount int
SELECT @ItemCount = count(*) FROM [dbo].[item] as it
JOIN [dbo].[copy] as co ON
it.isbn = co.isbn
WHERE it.isbn = @isbn AND co.copy_no = @CopyNo
IF @ItemCount < 1
BEGIN
RAISERROR('Item does not exist in this library.',11,1)
RETURN
END
--Test to see if the item is loanable.
DECLARE @Loanable nvarchar (1)
SELECT @Loanable = loanable FROM [dbo].[item]
WHERE isbn = @isbn
IF @Loanable = 'N'
BEGIN
RAISERROR('Check out not successful. Item is not loanable.',11,1)
RETURN
END
--Test to see if the item is already on loan.
DECLARE @OnLoan nvarchar (1)
SELECT @OnLoan = on_loan FROM [dbo].[copy]
WHERE isbn = @isbn AND copy_no = @CopyNo
IF @OnLoan = 'Y'
BEGIN
RAISERROR('Check out not successful. Item is already checked out.',12,1)
RETURN
END
--Retrieve the title number.
DECLARE @TitleNo int
SELECT @TitleNo = title_no FROM [dbo].[copy]
WHERE isbn = @isbn AND copy_no = @CopyNo
--Set the check out date as today and the
--due date as 14 days from today.
--Assume that the library is open
--on weekends.
DECLARE @OutDate datetime
DECLARE @DueDate datetime
SET @OutDate = GETDATE()
SET @DueDate = DATEADD(day,14,GETDATE())
9. --Add the new record to the loan table.
--Then change the on_loan field to 'Y' in the copy table.
BEGIN TRANSACTION
UPDATE [dbo].[copy]
SET on_loan = 'Y'
WHERE isbn = @isbn AND copy_no = @CopyNo
if @@ERROR <> 0
BEGIN
RAISERROR('Error, item not checked out.',11,1)
ROLLBACK TRANSACTION
RETURN
END
INSERT [dbo].[loan]
(isbn,copy_no,title_no,member_no,out_date,due_date)
VALUES (@isbn,@CopyNo,@TitleNo,@MemberID,@OutDate,@DueDate)
--Test for successful insertion.
if @@ERROR <> 0
BEGIN
RAISERROR('Error, item not checked out.',11,1)
ROLLBACK TRANSACTION
RETURN
END
COMMIT TRANSACTION
10. --This procedure deletes a member from
--either the adult or juvenile table.
CREATE PROCEDURE [dbo].[DeleteMember]
@MemberID int
AS
--Test for null values.
IF @MemberID is NULL
BEGIN
RAISERROR('Invalid data entry.',11,1)
RETURN
END
--Test for adult member.
DECLARE @AdultCount int
SELECT @AdultCount = count(*) FROM adult
WHERE member_no = @MemberID
IF @AdultCount > 0
BEGIN
BEGIN TRANSACTION
DELETE dbo.adult
WHERE member_no = @MemberID
--Test for successful deletion.
if @@ERROR <> 0
BEGIN
RAISERROR('Error, member not deleted.',11,1)
ROLLBACK TRANSACTION
RETURN
END
COMMIT TRANSACTION
RETURN
END
--Test for juvenile member.
DECLARE @JuvenileCount int
SELECT @JuvenileCount = count(*) FROM juvenile
WHERE member_no = @MemberID
IF @JuvenileCount > 0
BEGIN
BEGIN TRANSACTION
DELETE dbo.juvenile
WHERE member_no = @MemberID
--Test for successful deletion.
if @@ERROR <> 0
BEGIN
RAISERROR('Error, member not deleted.',11,1)
ROLLBACK TRANSACTION
RETURN
END
COMMIT TRANSACTION
RETURN
END
11. --This procedure returns a result set
--with values from the following fields:
--Author, CheckOutDate, CopyNumber,
--DueDate, ISBN, MemberID, and Title.
CREATE PROCEDURE [dbo].[GetItem]
@isbn int,
@CopyNo smallint
AS
--Test for null values.
IF @isbn is NULL OR @CopyNo is NULL
BEGIN
RAISERROR('Item not found. Invalid data entry.',11,1)
RETURN
END
--Test to see if the item exists in library.
DECLARE @ItemCount int
SELECT @ItemCount = count(*) FROM [dbo].[item] as it
JOIN [dbo].[copy] as co ON
it.isbn = co.isbn
WHERE it.isbn = @isbn AND co.copy_no = @CopyNo
IF @ItemCount < 1
BEGIN
RAISERROR('Item does not exist in this library.',11,1)
RETURN
END
SELECT co.isbn as ISBN,
co.copy_no as CopyNumber,
ti.title as Title,
ti.author as Author,
lo.out_date as CheckOutDate,
lo.due_date as DueDate,
lo.member_no as MemberID
FROM [dbo].[copy] as co
JOIN [dbo].[title] as ti ON co.title_no = ti.title_no
LEFT JOIN [dbo].[loan] as lo ON lo.isbn = @isbn AND lo.copy_no = @CopyNo
WHERE co.isbn = @isbn AND co.copy_no = @CopyNo
12. --This procedure returns a result set
--containing items on loan for a
--particular member ID.
CREATE PROCEDURE [dbo].[GetItems]
@MemberID int
AS
--Test for null values.
IF @MemberID is NULL
BEGIN
RAISERROR('Member not found. Invalid data entry.',11,1)
RETURN
END
--Test to see if the member exists in membership.
DECLARE @MemberCount int
SELECT @MemberCount = count(*) FROM [dbo].[member]
WHERE member_no = @MemberID
IF @MemberCount < 1
BEGIN
RAISERROR('Member not found.',11,1)
RETURN
END
SELECT lo.isbn,
lo.copy_no,
ti.title,
ti.author,
lo.out_date,
lo.due_date,
it.translation,
it.cover,
it.loanable,
lo.title_no,
ti.synopsis,
co.on_loan,
lo.member_no
FROM [dbo].[loan] as lo
JOIN [dbo].[item] as it ON lo.isbn = it.isbn AND lo.title_no = it.title_no
JOIN [dbo].[copy] as co ON lo.isbn = co.isbn AND lo.title_no = co.title_no AND
lo.copy_no = co.copy_no
JOIN [dbo].[title] as ti ON lo.title_no = ti.title_no
WHERE lo.member_no = @MemberID
13. --This procedure returns an adult member
--or a juvenile member
CREATE PROCEDURE [dbo].[GetMember]
@MemberID int,
@MemberType nvarchar (8) OUTPUT
AS
--Test for null values.
IF @MemberID is NULL
BEGIN
RAISERROR('Invalid data entry.',11,1)
SET @MemberType = 'NONE'
RETURN
END
--Test for valid juvenile member.
DECLARE @AdultMemberNo int
SELECT @AdultMemberNo = adult_member_no FROM [dbo].[juvenile]
WHERE member_no = @MemberID
IF @AdultMemberNo IS null
BEGIN
--Test for a valid adult member.
DECLARE @AdultExists int
SELECT @AdultExists = count(*) FROM [dbo].[adult]
WHERE member_no = @MemberID
IF @AdultExists < 1
BEGIN
RAISERROR('No member exists by that number.',11,1)
SET @MemberType = 'NONE'
RETURN
END
ELSE
BEGIN
SELECT me.lastname as LastName,
me.firstname as FirstName,
me.middleinitial as MiddleInitial,
ad.street as Street,
ad.city as City,
ad.state as State,
ad.zip as ZipCode,
ad.phone_no as PhoneNumber,
ad.expr_date as ExprDate,
ad.member_no as MemberID
FROM [dbo].[member] as me LEFT JOIN
[dbo].[adult] as ad ON
ad.member_no = @MemberID
WHERE
me.member_no = @MemberID
SET @MemberType = 'ADULT'
END
END
ELSE
BEGIN
SELECT me.lastname as LastName,
me.firstname as FirstName,
me.middleinitial as MiddleInitial,
ju.adult_member_no as AdultMemberNumber,
ju.birth_date as BirthDate,
ad.street as Street,
ad.city as City,
ad.state as State,
ad.zip as ZipCode,
ad.phone_no as PhoneNumber,
ad.expr_date as ExprDate,
14. ju.member_no as MemberID
FROM [dbo].[member] as me LEFT JOIN
[dbo].[juvenile] as ju ON
ju.member_no = @MemberID LEFT JOIN
[dbo].[adult] as ad ON
ad.member_no = @AdultMemberNo
WHERE
me.member_no = @MemberID
SET @MemberType = 'JUVENILE'
END
15. --This procedure renews a membership
--by adding one year to the expiration date.
CREATE PROCEDURE [dbo].[RenewMembership]
@MemberID int
AS
--Test for null values.
IF @MemberID is NULL
BEGIN
RAISERROR('Invalid data entry.',11,1)
RETURN
END
--Test to see if the member exists in library.
DECLARE @Count int
SELECT @Count = count(*) FROM [dbo].[member] as me
WHERE me.member_no = @MemberID
IF @Count < 1
BEGIN
RAISERROR('Member does not exist in this library.',11,1)
RETURN
END
--Test to see if member is a juvenile.
--If so then update the adult expiration date
--based on the adult member ID otherwise
--update based on the member ID.
DECLARE @AdultMemberID int
SELECT @AdultMemberID = adult_member_no FROM [dbo].[juvenile] as ju
WHERE ju.member_no = @MemberID
IF @AdultMemberID > 0
BEGIN
BEGIN TRANSACTION
UPDATE [dbo].[adult]
SET expr_date = DATEADD(year,1,GETDATE())
WHERE member_no = @AdultMemberID
if @@ERROR <> 0
BEGIN
RAISERROR('Error, membership not renewed.',11,1)
ROLLBACK TRANSACTION
RETURN
END
COMMIT TRANSACTION
END
ELSE
BEGIN
BEGIN TRANSACTION
UPDATE [dbo].[adult]
SET expr_date = DATEADD(year,1,GETDATE())
WHERE member_no = @MemberID
if @@ERROR <> 0
BEGIN
RAISERROR('Error, membership not renewed.',11,1)
ROLLBACK TRANSACTION
RETURN
END
COMMIT TRANSACTION
END