• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Library Project Stored Procs
 

Library Project Stored Procs

on

  • 670 views

 

Statistics

Views

Total Views
670
Views on SlideShare
662
Embed Views
8

Actions

Likes
2
Downloads
25
Comments
1

3 Embeds 8

http://www.lmodules.com 4
http://www.linkedin.com 3
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • helpful
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Library Project Stored Procs Library Project Stored Procs Document Transcript

    • Library Project Sql Stored Procedures<br />CREATE PROCEDURE AdultMember<br />(@FirstName varchar(15),<br /> @LastName varchar(15),<br /> @MiddleInitial char(1),<br /> @MemberID smallint OUTPUT,<br /> @PhoneNumber varchar(10),<br /> @ExpirationDate smallint,<br /> @ZipCode varchar(5),<br /> @Street smallint,<br /> @State smallint,<br /> @City smallint<br />)<br />AS<br />BEGIN<br /> <br /> IF @FirstName IS NULL<br /> BEGIN<br /> --RAISERROR('Adult FirstName is not null',15,1);<br /> return-15<br /> END<br /> <br /> IF @LastName IS NULL<br /> BEGIN<br /> --RAISERROR('Adult LastName is not null',15,1);<br /> return-15<br /> END<br /> <br /> IF @MiddleInitial IS NULL<br /> BEGIN<br /> --RAISERROR('Adult FirstName is not null',1);<br /> return-1<br /> END<br /> <br /> IF @MemberID IS NULL OR @MemberID<0<br /> BEGIN<br /> --RAISERROR('Adult MemberID is not null',1);<br /> return-1<br /> END<br /> <br /> IF @PhoneNumber IS NULL<br /> BEGIN<br /> --RAISERROR('Adult PhoneNumber is not null',10);<br /> return-10<br /> END<br /> <br /> IF @ExpirationDate IS NULL<br /> BEGIN<br /> --RAISERROR('Adult ExpirationDate is not null',1);<br /> return-1<br /> END<br /> <br /> IF @ZipCode IS NULL<br /> BEGIN<br /> --RAISERROR('Adult ZipCode is not null',5);<br /> return-5<br /> END<br /> <br /> IF @Street IS NULL<br /> BEGIN<br /> --RAISERROR('Adult Street is not null',1);<br /> return-1<br /> END<br /> <br /> IF @State IS NULL<br /> BEGIN<br /> --RAISERROR('Adult State is not null',1);<br /> return-1<br /> END<br /> <br /> IF @City IS NULL<br /> BEGIN<br /> --RAISERROR('Adult City is not null',1);<br /> return-1<br /> END<br /> <br /> --Need to wrap the INSERTs, UPDATEs, and DELETEs in a transaction DJL<br /> INSERT INTO member(firstName, middleinitial, lastname)<br /> VALUES(@firstName, @MiddleInitial, @LastName);<br /> <br /> select @memberid = SCOPE_IDENTITY();<br /> <br /> INSERT INTO adult(member_no, phone_no, city, [state], street, zip, expr_date)<br /> VALUES(@MemberID, @PhoneNumber, @City, @State, @Street, @ZipCode, @ExpirationDate);<br /> <br /> END<br /> <br /> BEGIN TRY<br /> EXEC @MemberID =@MemberID'Nana', 'W', 'Boama', '3107 petre st', 'chesapekae', 'VA', '23325', '', '03/05/2010', 0;<br /> END TRY <br /> BEGIN CATCH<br /> PRINT 'Test memberid: FAIL -- CAUGHT Exception';<br /> END CATCH<br /> IF ( SELECT COUNT(*)<br /> FROM member AS m INNER JOIN adult AS a ON m.member_no = a.member_no<br /> WHERE m.member_no = m.member_no) = 1<br /> PRINT 'Test MemberID: PASS -- memberid = ' + CAST(@memberID AS varchar);<br /> ELSE<br /> PRINT 'Test MemberID: FAIL -- memberid = ' + CAST(@memberID AS varchar);<br />CREATE PROCEDURE JuvenileMember<br />(@FirstName varchar(15),<br /> @LastName varchar(15),<br /> @MiddleInitial char(1),<br /> @MemberID smallint OUTPUT,<br /> @BirthDate datetime,<br /> @AdultSponsorID smallint<br />)<br />AS<br />BEGIN<br /> <br /> IF @FirstName IS NULL<br /> BEGIN<br /> --RAISERROR('Juvenile FirstName is null',15,1);<br /> return-15<br /> END<br /> <br /> IF @LastName IS NULL<br /> BEGIN<br /> --RAISERROR('Juvenile LastName is null',15,1);<br /> return-16<br /> END<br /> <br /> IF @MiddleInitial IS NULL<br /> BEGIN<br /> --RAISERROR('Juvenile FirstName is null',1);<br /> return-17<br /> END<br /> IF @MemberID IS NULL OR @MemberID<0<br /> BEGIN<br /> --RAISERROR('Juvenile MemberID is null',1);<br /> return-18<br /> END<br /> <br /> IF @AdultSponsorID IS NULL OR @AdultSponsorID<0<br /> BEGIN<br /> --RAISERROR('Adult Sponsor ID is null',1);<br /> return-19<br /> END<br /> IF @BirthDate IS NULL<br /> BEGIN<br /> --RAISERROR('Birthdate is null',1);<br /> return-20<br /> END<br /> --Need to wrap the INSERTs, UPDATEs, and DELETEs in a transaction DJL<br /> INSERT INTO member(firstName, middleinitial, lastname)<br /> VALUES(@firstName, @MiddleInitial, @LastName);<br /> <br /> select @memberid = SCOPE_IDENTITY();<br /> <br /> INSERT INTO juvenile(member_no, adult_member_no, birth_date)<br /> VALUES(@MemberID, @AdultSponsorID, @BirthDate<br /> END<br /> CREATE PROCEDURE AddBook <br /> @ISBN int, <br /> @Translation nvarchar (8) = NULL, <br /> @Cover nvarchar (8) = NULL, <br /> @Title nvarchar (63), <br /> @Author nvarchar (31), <br /> @Synopsis text = NULL, <br /> @Loanable nchar (1), <br /> @CopyNo smallint OUTPUT <br />AS <br /> <br />--Test for null values. <br />IF @ISBN is NULL OR @Loanable is NULL <br /> OR @Title is NULL OR @Author is NULL <br /> BEGIN <br /> RAISERROR('Invalid data entry.',11,1) <br /> RETURN <br /> END <br /> <br />--Test to see if the item exists in library. <br />DECLARE @ItemCount int <br />SELECT @ItemCount = count(*) FROM dbo.item as it <br /> WHERE it.isbn = @ISBN <br /> <br />IF @ItemCount < 1 --The item doesn't exist so add it. <br /> BEGIN <br /> BEGIN TRANSACTION <br /> DECLARE @TitleNo int <br /> --Add record to the title table first. <br /> INSERT dbo.title <br /> (title,author,synopsis) <br /> VALUES (@Title,@Author,@Synopsis) <br /> IF @@Error <> 0 <br /> BEGIN <br /> RAISERROR('Cannot add item.',11,1) <br /> ROLLBACK TRANSACTION <br /> RETURN <br /> END <br /> SET @TitleNo = @@IDENTITY <br /> <br /> --Add record to the item table next. <br /> INSERT dbo.item <br /> (isbn,title_no,translation,cover,loanable) <br /> VALUES (@ISBN,@TitleNo,@Translation,@Cover,@Loanable) <br /> IF @@ERROR <> 0 <br /> BEGIN <br /> RAISERROR('Cannot add item.',11,1) <br /> ROLLBACK TRANSACTION <br /> RETURN <br /> END <br />--Add record to the copy table next. <br /> SET @CopyNo = 1 <br /> INSERT dbo.copy <br /> (isbn,copy_no,title_no,on_loan) <br /> VALUES (@ISBN,@CopyNo,@TitleNo,'N') <br /> IF @@ERROR <> 0 <br /> BEGIN <br />RAISERROR('Cannot add item.',11,1) <br /> ROLLBACK TRANSACTION <br /> RETURN <br /> END <br /> COMMIT TRANSACTION <br /> END <br />ELSE <br /> BEGIN --The item already exists. Add another copy. <br /> --First determine what the title number is. <br /> SELECT @TitleNo = title_no FROM dbo.item <br /> WHERE isbn = @ISBN <br /> --Then determine what the copy number should be <br /> --by counting how many copies already exist. <br /> DECLARE @CopyCount int <br /> SELECT @CopyCount = count(*) FROM dbo.copy as co <br /> WHERE co.isbn = @isbn <br /> --The latest copy # should be the count + 1. <br /> SET @CopyNo = @CopyCount + 1 <br /> BEGIN TRANSACTION <br /> --Add record to the copy table. <br /> INSERT dbo.copy <br /> (isbn,copy_no,title_no,on_loan) <br /> VALUES (@ISBN,@CopyNo,@TitleNo,'N') <br /> IF @@ERROR <> 0 <br /> BEGIN <br /> RAISERROR('Cannot add item.',11,1) <br /> ROLLBACK TRANSACTION <br /> RETURN <br /> END <br /> COMMIT TRANSACTION <br />END<br /> <br />CREATE PROC AddCopy<br />(@isbn int,<br />@copynum smallint<br />)<br />AS <br />BEGIN<br />IF @isbn IS NULL<br />BEGIN<br />--RAISERROR<br />return -1;<br />END<br />IF @copynum IS NULL<br />BEGIN<br />--RAISERROR<br />return -1;<br />END<br />SELECT title, out_date, due_date, translation, author, loanable, member_no, cover<br />FROM copy INNER JOIN title ON copy.title_no = copy.title_no<br /> INNER JOIN item ON item.isbn = copy.isbn<br /> LEFT OUTER JOIN loan ON loan.isbn = copy.isbn AND<br /> loan.copy_no = copy.copy_no<br />WHERE copy.isbn = @isbn AND copy.copy_no = @copynum;<br />END<br />CREATE PROCEDURE CheckInItem<br />(@isbn int,<br /> @copyNumber smallint<br /> )<br /> <br /> AS<br /> BEGIN<br /> <br /> IF @isbn IS NULL<br /> BEGIN<br /> --RAISERROR<br /> return -1;<br /> END<br /> <br /> IF @copyNumber is NULL<br /> BEGIN<br /> --RAISERROR<br /> return -1;<br /> END<br /> <br /> --Need to wrap the INSERTs, UPDATEs, and DELETEs in a transaction DJL<br />DECLARE @loanID smallint;<br /> INSERT INTO loanhist(isbn, copy_no) --Need to populate other columns in loanhist DJL<br /> VALUES(@isbn, @copyNumber)<br /> <br /> UPDATE copy<br /> SET on_loan ='N'<br /> WHERE isbn = @isbn AND copy_no = @copyNumber <br /> <br /> DELETE FROM loan<br /> WHERE isbn = @isbn AND copy_no = @copyNumber <br />END<br />CREATE PROC CheckOutItem<br />(@isbn int,<br /> @copyNumber smallint,<br /> @memberNumber smallint<br /> )<br /> AS<br /> BEGIN<br /> <br /> IF @isbn IS NULL<br /> BEGIN<br /> --RAISERROR<br /> return -1;<br /> END<br /> <br /> IF @copyNumber is NULL<br /> BEGIN<br /> --RAISERROR<br /> return -1;<br /> END<br /> <br /> IF @memberNumber IS NULL<br /> BEGIN<br /> --RAISERROR<br /> return -1;<br /> END<br /> <br /> DECLARE @ItemCount int<br /> SELECT @ItemCount = COUNT(*) FROM [dbo].[item] as it<br />join [dbo].[copy] as co on<br />it.isbn = co.isbn<br /> WHERE it.isbn = @isbn AND co.copy_no = @copyNumber<br /> IF @ItemCount < 1<br /> BEGIN<br />RAISERROR('item does not exist in this library.', 11,1)<br />Return<br /> END<br /> <br /> DECLARE @Loanable nvarchar (1)<br /> SELECT @Loanable = loanable FROM [dbo].[item]<br /> WHERE isbn = @isbn<br /> IF @Loanable ='N'<br /> BEGIN<br /> RAISERROR('Check out not successful, Item not loanable,', 11,1)<br /> RETURN<br /> END<br /> <br /> DECLARE @OnLoan nvarchar (1)<br /> SELECT @OnLoan = on_loan FROM [dbo].[copy]<br /> WHERE isbn = @isbn AND copy_no = @copyNumber<br /> IF @Loanable = 'Y'<br /> BEGIN<br /> RAISERROR('Check out not successful,',12,1)<br /> END<br /> <br /> DECLARE @TitleNo int<br /> SELECT @TitleNo = title_no FROM [dbo].[copy]<br /> WHERE isbn = @isbn AND copy_no = @copyNumber<br /> <br /> <br /> DECLARE @OutDate datetime<br /> DECLARE @DueDate datetime<br /> SET @OutDate = GETDATE()<br /> SET @DueDate = DATEADD(DAY, 14, GETDATE())<br /> <br />BEGIN TRANSACTION<br /> <br /> UPDATE [dbo].[copy]<br /> SET on_loan = 'Y'<br /> WHERE isbn = @isbn AND copy_no = @copyNumber<br /> <br /> if @@ERROR <> 0<br /> BEGIN<br /> RAISERROR('item not checked out,',11,1)<br /> ROLLBACK TRANSACTION<br /> RETURN<br /> END<br /> <br /> INSERT [dbo].[loan]<br />(isbn,copy_no, title_no, member_no, out_date, due_date)<br /> VALUEs (@isbn, @copyNumber, @TitleNo, @memberNumber, @OutDate, @DueDate)<br /> <br /> if @@ERROR <> 0<br /> BEGIN<br /> RAISERROR('Error, item not checked out.',11,1)<br /> ROLLBACK TRANSACTION<br /> RETURN<br /> END<br /> <br />END <br /> <br />CREATE PROC ConvertJuvToAdult<br />(@memberID smallint<br />)<br />AS<br />BEGIN<br />IF @memberid IS NULL<br />BEGIN<br />--RAISERROR<br />return -1;<br />END<br />SELECT juvenile.member_no, juvenile.birth_date, juvenile.adult_member_no, adult.street, adult.city, adult.state, adult.zip, adult.phone_no, adult.expr_date<br />FROM dbo.adult Adult, dbo.juvenile<br />WHERE adult.member_no = juvenile.member_no<br />AND juvenile.member_no = adult.member_no<br />SELECT juvenile.member_no, juvenile.adult_member_no, juvenile.birth_date, adult.street, adult.city, adult.state, adult.zip, adult.phone_no, adult.expr_date<br />FROM dbo.adult Adult, dbo.juvenile Juvenile, dbo.member Member<br />WHERE Adult.member_no = Juvenile.adult_member_no<br />AND Juvenile.member_no = Member.member_no<br />AND Member.member_no = Juvenile.member_no<br />END<br />CREATE PROC DeleteMember<br /> @MemberID int <br />AS <br /> <br />--Test for null values. <br />IF @MemberID is NULL <br /> BEGIN <br /> RAISERROR('Invalid data entry.',11,1) <br /> RETURN <br /> END <br /> <br />--Test for adult member. <br />DECLARE @AdultCount int <br />SELECT @AdultCount = count(*) FROM adult <br /> WHERE member_no = @MemberID <br /> <br />IF @AdultCount > 0 <br /> BEGIN <br /> BEGIN TRANSACTION <br /> DELETE dbo.adult <br /> WHERE member_no = @MemberID <br /> <br /> --Test for successful deletion. <br /> if @@ERROR <> 0 <br /> BEGIN <br /> RAISERROR('Error, member not deleted.',11,1) <br /> ROLLBACK TRANSACTION <br /> RETURN <br /> END <br /> COMMIT TRANSACTION <br /> RETURN <br /> END <br /> <br />--Test for juvenile member. <br />DECLARE @JuvenileCount int <br />SELECT @JuvenileCount = count(*) FROM juvenile <br /> WHERE member_no = @MemberID <br /> <br />IF @JuvenileCount > 0 <br /> BEGIN <br /> BEGIN TRANSACTION <br /> DELETE dbo.juvenile <br /> WHERE member_no = @MemberID <br /> <br /> --Test for successful deletion. <br /> if @@ERROR <> 0 <br /> BEGIN <br /> RAISERROR('Error, member not deleted.',11,1) <br /> ROLLBACK TRANSACTION<br /> RETURN<br />END<br />COMMIT TRANSACTION<br />RETURN<br />END<br />CREATE PROC GetItem<br />( @isbn int,<br /> @copyno smallint<br />)<br />AS<br />BEGIN<br /> IF @isbn IS NULL<br /> BEGIN<br /> --RAISERROR<br /> return -1;<br /> END<br /> IF @copyno IS NULL<br /> BEGIN<br /> --RAISERROR<br /> return -1;<br /> END<br /> <br /> --Test to see if the item exists in library. <br />DECLARE @ItemCount int <br />SELECT @ItemCount = count(*) FROM [dbo].[item] as it <br />JOIN [dbo].[copy] as co ON <br /> it.isbn = co.isbn <br /> WHERE it.isbn = @isbn AND co.copy_no = @CopyNo <br /> IF @ItemCount < 1 <br /> BEGIN <br /> RAISERROR('Item does not exist in this library.',11,1) <br /> RETURN <br /> END <br /> SELECT title, author, translation, cover, loanable, member_no, out_date, due_date<br /> FROM copy INNER JOIN title ON copy.title_no = title.title_no<br /> INNER JOIN item ON item.isbn = copy.isbn<br /> LEFT OUTER JOIN loan ON loan.isbn = copy.isbn AND loan.copy_no = copy.copy_no<br /> WHERE copy.isbn = @isbn AND copy.copy_no = @copyno;<br />END<br />CREATE PROCEDURE GetItems<br />( @memberno smallint<br />)<br />AS<br />BEGIN<br /> IF @memberno IS NULL<br /> BEGIN<br /> --RAISERROR<br /> return -1;<br /> END<br /> SELECT loan.isbn, loan.copy_no, title, author, out_date, due_date<br /> FROM loan INNER JOIN title ON loan.title_no = title.title_no<br /> INNER JOIN item ON item.isbn = loan.isbn<br /> WHERE member_no = @memberno;<br />END<br />CREATE PROCEDURE GetMemberByItem<br />(@isbn int,<br /> @copyno smallint<br /> )<br /> AS<br /> BEGIN<br /> IF @isbn IS NULL<br /> BEGIN<br /> --RAISERROR<br /> return -1;<br /> END<br /> <br /> IF @copyno IS NULL<br /> BEGIN<br /> --RAISERROR<br /> return -1;<br /> END<br /> <br /> SELECT title, author, translation, cover, loanable, member_no, out_date, due_date<br /> FROM copy INNER JOIN title ON copy.title_no = title.title_no<br /> INNER JOIN item ON item.isbn = copy.isbn<br /> LEFT OUTER JOIN loan ON loan.isbn = copy.isbn AND loan.copy_no = copy.copy_no<br /> WHERE copy.isbn = @isbn AND copy.copy_no = @copyno;<br /> END<br />CREATE PROC SetExpirationDate<br />(@memberID smallint<br />)<br />AS<br />BEGIN<br />IF @memberid IS NULL<br />BEGIN<br />--RAISERROR<br />return -1;<br />END<br />DECLARE @OutDate datetime<br />DECLARE @DueDate datetime<br />SET @OutDate = GETDATE()<br />SET @DueDate = DATEADD(DAY,14,GETDATE())<br />end<br />