SlideShare una empresa de Scribd logo
1 de 15
--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

Más contenido relacionado

La actualidad más candente

06.01 sql select distinct
06.01 sql select distinct06.01 sql select distinct
06.01 sql select distinct
Bishal Ghimire
 

La actualidad más candente (20)

Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
Working with Databases and MySQL
Working with Databases and MySQLWorking with Databases and MySQL
Working with Databases and MySQL
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
 
06.01 sql select distinct
06.01 sql select distinct06.01 sql select distinct
06.01 sql select distinct
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
 
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
 
8. sql
8. sql8. sql
8. sql
 
Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
Cross Join Example and Applications
Cross Join Example and ApplicationsCross Join Example and Applications
Cross Join Example and Applications
 
Up to Speed on HTML 5 and CSS 3
Up to Speed on HTML 5 and CSS 3Up to Speed on HTML 5 and CSS 3
Up to Speed on HTML 5 and CSS 3
 
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
Views, Triggers, Functions, Stored Procedures,  Indexing and JoinsViews, Triggers, Functions, Stored Procedures,  Indexing and Joins
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
 
Expressjs
ExpressjsExpressjs
Expressjs
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
Presentation LIBRARY MANAGEMENT SYSTEM
Presentation LIBRARY MANAGEMENT SYSTEM Presentation LIBRARY MANAGEMENT SYSTEM
Presentation LIBRARY MANAGEMENT SYSTEM
 
Xml parsing
Xml parsingXml parsing
Xml parsing
 
Sql fundamentals
Sql fundamentalsSql fundamentals
Sql fundamentals
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
Triggers and Stored Procedures
Triggers and Stored ProceduresTriggers and Stored Procedures
Triggers and Stored Procedures
 

Similar a SQL Stored Procedures For My Library Project

Library Project Stored Procs
Library Project Stored ProcsLibrary Project Stored Procs
Library Project Stored Procs
nwbgh
 
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Alex Sharp
 
Frank Rodenbaugh Portfolio
Frank Rodenbaugh PortfolioFrank Rodenbaugh Portfolio
Frank Rodenbaugh Portfolio
FrankRodenbaugh
 
Sql Portfolio(March 31)
Sql Portfolio(March 31)Sql Portfolio(March 31)
Sql Portfolio(March 31)
iceolated
 
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docxAssg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
festockton
 
On SQL Managment studioThis lab is all about database normalizatio.pdf
On SQL Managment studioThis lab is all about database normalizatio.pdfOn SQL Managment studioThis lab is all about database normalizatio.pdf
On SQL Managment studioThis lab is all about database normalizatio.pdf
infomalad
 
Oracle SQL - Write the code for a stored procedure that accepts a prop.docx
Oracle SQL - Write the code for a stored procedure that accepts a prop.docxOracle SQL - Write the code for a stored procedure that accepts a prop.docx
Oracle SQL - Write the code for a stored procedure that accepts a prop.docx
lmark1
 

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

Library Project Stored Procs
Library Project Stored ProcsLibrary Project Stored Procs
Library Project Stored Procs
 
My Portfolio
My PortfolioMy Portfolio
My Portfolio
 
My Portfolio
My PortfolioMy Portfolio
My Portfolio
 
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
 
Greg Lewis SQL Portfolio
Greg Lewis SQL PortfolioGreg Lewis SQL Portfolio
Greg Lewis SQL Portfolio
 
Frank Rodenbaugh Portfolio
Frank Rodenbaugh PortfolioFrank Rodenbaugh Portfolio
Frank Rodenbaugh Portfolio
 
Sql Portfolio(March 31)
Sql Portfolio(March 31)Sql Portfolio(March 31)
Sql Portfolio(March 31)
 
PostgreSQL table partitioning
PostgreSQL table partitioningPostgreSQL table partitioning
PostgreSQL table partitioning
 
Using web2py's DAL in other projects or frameworks
Using web2py's DAL in other projects or frameworksUsing web2py's DAL in other projects or frameworks
Using web2py's DAL in other projects or frameworks
 
Drupal7 dbtng
Drupal7  dbtngDrupal7  dbtng
Drupal7 dbtng
 
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docxAssg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
Assg 12 Binary Search Trees COSC 2336assg-12.cppAssg 12 Binary .docx
 
On SQL Managment studioThis lab is all about database normalizatio.pdf
On SQL Managment studioThis lab is all about database normalizatio.pdfOn SQL Managment studioThis lab is all about database normalizatio.pdf
On SQL Managment studioThis lab is all about database normalizatio.pdf
 
Chris Mc Glothen Sql Portfolio
Chris Mc Glothen Sql PortfolioChris Mc Glothen Sql Portfolio
Chris Mc Glothen Sql Portfolio
 
Shangz R Brown Presentation
Shangz R Brown PresentationShangz R Brown Presentation
Shangz R Brown Presentation
 
Everything About PowerShell
Everything About PowerShellEverything About PowerShell
Everything About PowerShell
 
Oracle SQL - Write the code for a stored procedure that accepts a prop.docx
Oracle SQL - Write the code for a stored procedure that accepts a prop.docxOracle SQL - Write the code for a stored procedure that accepts a prop.docx
Oracle SQL - Write the code for a stored procedure that accepts a prop.docx
 
supporting t-sql scripts for Heap vs clustered table
supporting t-sql scripts for Heap vs clustered tablesupporting t-sql scripts for Heap vs clustered table
supporting t-sql scripts for Heap vs clustered table
 
Write a script to insert at least 10-000 records in NY- NJ- and CT int.docx
Write a script to insert at least 10-000 records in NY- NJ- and CT int.docxWrite a script to insert at least 10-000 records in NY- NJ- and CT int.docx
Write a script to insert at least 10-000 records in NY- NJ- and CT int.docx
 
How te bring common UI patterns to ADF
How te bring common UI patterns to ADFHow te bring common UI patterns to ADF
How te bring common UI patterns to ADF
 
The Art of Transduction
The Art of TransductionThe Art of Transduction
The Art of Transduction
 

Último

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Último (20)

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
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