SlideShare a Scribd company logo
--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
--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
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
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)
if @@ERROR <> 0
            BEGIN
                  RAISERROR('Error, member not added.',11,1)
                  ROLLBACK TRANSACTION
                  RETURN
            END

COMMIT TRANSACTION
--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
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
--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())
--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
--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
--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
--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
--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,
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
--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

More Related Content

PPTX
SQL Join Basic
PPTX
DATABASE CONSTRAINTS
PPT
Sql dml & tcl 2
PPT
Sql ppt
PPT
Unix ch03-03(2)
ODP
Introduction to triggers
PDF
Introduction to oracle functions
SQL Join Basic
DATABASE CONSTRAINTS
Sql dml & tcl 2
Sql ppt
Unix ch03-03(2)
Introduction to triggers
Introduction to oracle functions

What's hot (20)

PPTX
Procedure and Functions in pl/sql
PPTX
SQL Basics
PPTX
Sql operator
PPTX
introdution to SQL and SQL functions
PPTX
SQL(DDL & DML)
PPTX
PL/SQL - CURSORS
PPT
Joins in SQL
PPTX
sql function(ppt)
PPT
Music management system
PDF
UNIT 1- Data Warehouse.pdf
PPT
SQL Views
PPTX
SQL - DML and DDL Commands
PPTX
Date and time functions in mysql
PPTX
SQL Queries Information
PPT
Advanced sql
PPT
Introduction to structured query language (sql)
PPTX
Multi-Tier Architecture or N Tier Architecture
PPTX
PPTX
Unit 4 plsql
PPTX
Unit I Database concepts - RDBMS & ORACLE
Procedure and Functions in pl/sql
SQL Basics
Sql operator
introdution to SQL and SQL functions
SQL(DDL & DML)
PL/SQL - CURSORS
Joins in SQL
sql function(ppt)
Music management system
UNIT 1- Data Warehouse.pdf
SQL Views
SQL - DML and DDL Commands
Date and time functions in mysql
SQL Queries Information
Advanced sql
Introduction to structured query language (sql)
Multi-Tier Architecture or N Tier Architecture
Unit 4 plsql
Unit I Database concepts - RDBMS & ORACLE
Ad

Similar to SQL Stored Procedures For My Library Project (20)

DOCX
Library Project Stored Procs
PPTX
Frank Rodenbaugh Portfolio
PPTX
Tim Kunze\'s SQL Server and VB.NET portfolio
DOCX
My Portfolio
DOC
Library Management System - V1.0
PPT
My Portfolio
PPT
My Portfolio
PPT
Chris Mc Glothen Sql Portfolio
PPT
JDBeyler
PPTX
G Hslideshare2
DOCX
Tony Vitabile .Net Portfolio
DOCX
SetFocus Portfolio
DOC
Marcus Portfolio
PDF
Mark Jackson\'s Portfoilo
PPT
.NET Code Examples
PPTX
Geek Sync | Rewriting Bad SQL Code 101
PPT
Set Focus SQL Portfolio
PPT
Sql Portfolio(March 31)
PPT
Lewis Chiu Portfolio
PPT
Greg Lewis SQL Portfolio
Library Project Stored Procs
Frank Rodenbaugh Portfolio
Tim Kunze\'s SQL Server and VB.NET portfolio
My Portfolio
Library Management System - V1.0
My Portfolio
My Portfolio
Chris Mc Glothen Sql Portfolio
JDBeyler
G Hslideshare2
Tony Vitabile .Net Portfolio
SetFocus Portfolio
Marcus Portfolio
Mark Jackson\'s Portfoilo
.NET Code Examples
Geek Sync | Rewriting Bad SQL Code 101
Set Focus SQL Portfolio
Sql Portfolio(March 31)
Lewis Chiu Portfolio
Greg Lewis SQL Portfolio
Ad

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
A Presentation on Artificial Intelligence
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Encapsulation theory and applications.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Machine learning based COVID-19 study performance prediction
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
MYSQL Presentation for SQL database connectivity
Unlocking AI with Model Context Protocol (MCP)
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Modernizing your data center with Dell and AMD
Reach Out and Touch Someone: Haptics and Empathic Computing
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
A Presentation on Artificial Intelligence
Advanced methodologies resolving dimensionality complications for autism neur...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Encapsulation theory and applications.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Per capita expenditure prediction using model stacking based on satellite ima...
Machine learning based COVID-19 study performance prediction
The Rise and Fall of 3GPP – Time for a Sabbatical?
MYSQL Presentation for SQL database connectivity

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