Tim Kunze<br />SQL Developer/DBA<br />Portfolio<br />
Teaser Rate Offer Calculator<br />I got one of those teaser offers from Bank of America so I thought, “wouldn’t it be nice...
Library Project<br />Utilizes the Microsoft Library Database<br />Created stored procedures for the following functions:<b...
Add Adult<br />/* AddAdult Stored Procedure (dbo.AddAdult_SP)<br />This procedure is executed when a new adult member is a...
Testing:<br />BAD DATA!<br />INPUT:<br />-- NO DATA!<br />DECLARE @ret int<br />EXEC @ret =  dbo.AddAdult_SP<br />SELECT @...
Add Juvenile<br />----------------------------------------------------------------------------------------<br />/* Add Juv...
Add Juvenile (Cont.)<br />BEGIN CATCH<br />--  ERROR!<br />     SET @output_msg = CAST('AddJuvenile_SP - ERROR:  ' AS char...
Testing:<br />BAD DATA!<br />INPUT:<br />-- NO DATA!<br />DECLARE @ret int<br />EXEC @ret = dbo.AddJuvenile_SP<br />SELECT...
Check In A Book<br />-------------------------------------------------------------------------<br />/*Book Check In Stored...
Check In A Book (Cont.)<br />-- Verify if this member number actually has this book checked out!<br />IF NOT EXISTS(SELECT...
Check In A Book (Cont.)<br />-- UPDATE the on_loan column of the dbo.copy row!<br />BEGIN TRY<br />    UPDATE dbo.copy<br ...
Testing:<br />BAD DATA!<br />INPUT:<br />-- No Data!<br />DECLARE @ret int<br />EXEC @ret = dbo.BookCheckIn_SP<br />SELECT...
Check Out A Book<br />--------------------------------------------------------------------------------<br />/*Book Check O...
Check Out A Book (Cont.)<br />-- Verify that book is loanable<br />SET @on_loan = (SELECT loanable FROM dbo.itemi<br />   ...
Add A Book<br />-------------------------------------------------------------------------------<br />/*Add a Book (dbo.Add...
Add A Book (Cont.)<br />-- GOT AUTHOR?<br />IF @Author IS NULL<br />BEGIN<br />     SET @output_msg = CAST('AddBook_SP - E...
Renew Membership<br />----------------------------------------------------------------------<br />/*Renew Membership (dbo....
Change Juvenile To Adult<br />/*Move over 18 year old Juveniles to adult table (dbo.ChangeJuvenile_SP)<br />This procedure...
Change Juvenile To Adult (Cont.)<br />-- Move didn't work!<br />     SET @output_msg = CAST('ChangeJuvenile_SP - ERROR:  '...
Update Adult<br />/* Update adult stored procedure (dbo.UpdateAdult_SP)<br />This procedure allows the librarian to input ...
Update Adult (Cont.)<br />-- Figure out what information was provided and change it in adult<br />BEGIN TRY<br />     IF @...
MORE TO COME!!!<br />
Upcoming SlideShare
Loading in...5
×

Tim Kunze\'s SQL Server and VB.NET portfolio

479

Published on

This is a portfolio of SQL Server and VB.net projects on which I\'ve worked.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
479
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Tim Kunze\'s SQL Server and VB.NET portfolio

  1. 1. Tim Kunze<br />SQL Developer/DBA<br />Portfolio<br />
  2. 2. Teaser Rate Offer Calculator<br />I got one of those teaser offers from Bank of America so I thought, “wouldn’t it be nice to have an application that would tell you whether using the offer to pay off another credit card would be worthwhile?” Rather than go search the internet to find one, I decided it would be more fun to write it myself. So, here it is:<br />So, here’s how it works: You enter the line of credit available on the offer, the fee for the advance and the teaser interest rate in the “Available Calculations”. Then, you enter the payoff amount, interest rate and the number of months the offer runs (ie. Payoff in May and offer runs through December = 8 months). Once you have all this data entered in the boxes, click the “Calc Savings” button (or press Ctrl + s). If the payoff is greater than the line of credit, the line of credit is used in the calculations. If the payoff is less than the line of credit, the payoff is used.<br />
  3. 3. Library Project<br />Utilizes the Microsoft Library Database<br />Created stored procedures for the following functions:<br />Add an adult member<br />Add a juvenile member<br />Check in a book<br />Check out a book<br />Add a book to the database<br />Renew Membership<br />Change a juvenile member to adult<br />Update an adult member’s information<br />
  4. 4. Add Adult<br />/* AddAdult Stored Procedure (dbo.AddAdult_SP)<br />This procedure is executed when a new adult member is added<br />to the Library database.<br /> <br />INPUT (and format): Position #<br />Name<br />member.lastnamevarchr(15) 1<br />member.firstnamevarchar(15) 2<br />member.middleinitialchar(1) 7 OPTIONAL (NULL)<br /> Mailing Address<br />adult.streetvarchar(15) 3<br />adult.cityvarchar(15) 4<br />adult.statechar(2) 5<br /> adult.zip char(10) 6<br /> Phone Number<br />phone_nochar(13) 8 OPTIONAL (NULL)<br /> <br />RETURN VALUES:<br /> -1 Error on insertion of member row<br /> -2 Error on insertion of adult row<br /> New Member Number if successful<br />*/<br />CREATE PROC dbo.AddAdult_SP<br /> @lastnamevarchar(15), @firstnamevarchar(15), @street varchar(15),<br /> @city varchar(15), @state char(2), @zip char(10),<br /> @middleinitial char(1) = NULL, @phone_no char(13) = NULL,<br /> @NewMember_nosmallint OUTPUT<br />AS<br />BEGIN TRANSACTION<br />-- variable for returning error/success messages (troubleshooting)<br /> DECLARE @output_msgvarchar(150)<br />-- Insert the new member row<br /> BEGIN TRY<br /> INSERT INTO dbo.member<br />(lastname, firstname, middleinitial)<br />VALUES (@lastname, @firstname, @middleinitial)<br /> SET @NewMember_no = @@IDENTITY<br /> END TRY<br />BEGIN CATCH<br />-- ERROR!<br /> SET @output_msg = CAST('AddAdult_SP - ERROR: ' AS char(23)) +<br /> CAST(ERROR_NUMBER() AS varchar(5)) + <br /> CAST(' LINE: ' AS char(8)) +<br /> CAST(ERROR_LINE() AS varchar(4)) +<br /> CAST(' ' AS char(1)) + ERROR_MESSAGE()<br /> SET @NewMember_no = -1<br /> SELECT @NewMember_noNewMember#, @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @NewMember_no<br /> END CATCH<br /> BEGIN TRY<br />-- Insert the new adult row.<br />INSERT INTO dbo.adult<br /> (member_no, street, city, state, zip, phone_no, expr_date)<br /> VALUES (@NewMember_no, @street, @city, @state, @zip, @phone_no,<br /> DATEADD (Year, 1, getdate()))<br /> END TRY<br /> BEGIN CATCH<br />-- ERROR!<br /> SET @output_msg = CAST('AddAdult_SP - ERROR: ' AS char(23)) +<br />CAST(ERROR_NUMBER() AS varchar(5)) + <br />CAST(' LINE: ' AS char(8)) +<br />CAST(ERROR_LINE() AS varchar(4)) + <br />CAST(' ' AS char(1)) + ERROR_MESSAGE()<br /> SELECT @NewMember_noNewMember#, @output_msgErrorMessage<br /> SET @NewMember_no = -2<br /> ROLLBACK TRANSACTION<br /> RETURN @NewMember_no<br /> END CATCH<br />-- Send success message!<br />SET @output_msg = CAST('AddAdult_SP - New member # ' AS char(27)) +<br /> CAST(@NewMember_no AS char(8)) +<br /> CAST (' successfully entered!' AS char(22))<br />SELECT @output_msg [Success Message]<br />COMMIT TRANSACTION<br />RETURN @NewMember_no<br />;<br />go<br />
  5. 5. Testing:<br />BAD DATA!<br />INPUT:<br />-- NO DATA!<br />DECLARE @ret int<br />EXEC @ret = dbo.AddAdult_SP<br />SELECT @ret [Return Value]<br />RESULTS:<br />Msg 201, Level 16, State 4, Procedure AddAdult_SP, Line 0<br />Procedure or function 'AddAdult_SP' expects parameter '@lastname', which was not supplied.<br />INPUT:<br />-- BAD DATA (missing parameter)<br />DECLARE @ret int<br />EXEC @ret = dbo.AddAdult_SP Jones, Fred, [Main Place], Enid, OK<br />SELECT @ret [Return Value]<br />RESULTS:<br />Msg 201, Level 16, State 4, Procedure AddAdult_SP, Line 0<br />Procedure or function 'AddAdult_SP' expects parameter '@zip', which was not supplied.<br />GOOD DATA!<br />INPUT:<br />DECLARE @ret int<br />EXEC @ret = dbo.AddAdult_SP Kunze, Arnold, [E 3rd Place], Tulsa, OK, 74112,<br /> DEFAULT, DEFAULT, @ret<br />SELECT @ret [Returned Value]<br />SELECT firstname, lastname, street, city, state, zip, phone_no<br />FROM dbo.adult a<br /> JOIN dbo.member m<br />ON m.member_no = a.member_no<br /> WHERE lastname = 'Kunze' AND firstname = 'Arnold'<br />Go<br />RESULTS:<br />Success Message<br />AddAdult_SP - New member # 10001 successfully entered!<br />Returned Value<br />10001<br />Firstnamelastname street city state zipphone_no<br />Arnold Kunze E 3rd Place Tulsa OK 74112 NULL<br />
  6. 6. Add Juvenile<br />----------------------------------------------------------------------------------------<br />/* Add Juvenile Stored Procedure (dbo.AddJuvenile_SP)<br />This procedure is executed when a new Juvenile member is added to the Library<br />Database.<br /> <br />INPUT (and format): Position #<br /> Adult Memberr# 1 MUST EXIST<br /> NAME<br />member.lastnamevarchar(15) 2<br />member.firstnamevarchar(15) 3<br />member.middleinitial char(1) 5 OPTIONAL (NULL)<br /> Birth Date<br />juvenile.birth_date(datetime) 4 Check for < 18<br /> <br />RETURN VALUES<br /> -1 Error on juvenile's age<br /> -2 Adult sponsor doesn't have a record in the database (erroneous adult member #)<br /> -3 Error on insertion of member row<br /> -4 Error on insertion of juvenile row<br /> -5 Adult Member's card has expired<br /> New Member Number if successful<br />*/<br /> <br />CREATE PROC dbo.AddJuvenile_SP<br /> @Adult_Member_nosmallint, @lastnamevarchar(15), @firstnamevarchar(15),<br />@birth_datedatetime, @middleinitial char(1) = NULL, <br /> @NewMember_nosmallint OUTPUT<br />AS<br />BEGIN TRANSACTION<br />-- variable for returning error/success messages (troubleshooting)<br /> DECLARE @output_msgvarchar(150), @expr_datedatetime<br />-- Verify juvenile's age!<br /> IF DATEADD (Year, 18, @birth_date) <= getdate()<br /> BEGIN<br /> SET @output_msg =<br />'AddJuvenile_SP- ERROR: New member is older than 18 - must be input as adult.'<br />SELECT @output_msgErrorMessage<br />SET @NewMember_no = -1<br />ROLLBACK TRANSACTION<br />RETURN @NewMember_no<br /> END<br /> IF @birth_date > getdate()<br /> BEGIN<br /> SET @output_msg =<br /> 'AddJuvenile_SP - ERROR: New member has not been born yet!'<br /> SELECT @output_msgErrorMessage<br /> SET @NewMember_no = -1<br /> ROLLBACK TRANSACTION<br /> RETURN @NewMember_no<br /> END<br />-- Check to see if adult member exists!<br /> IF NOT EXISTS (SELECT member_no FROM dbo.adult<br /> WHERE dbo.adult.member_no = @Adult_Member_no)<br /> BEGIN<br /> SET @output_msg =<br /> 'AddJuvenile_SP - ERROR: Adult sponsor for this juvenile does not exist!'<br /> SELECT @output_msgErrorMessage<br /> SET @NewMember_no = -2<br /> ROLLBACK TRANSACTION<br /> RETURN @NewMember_no<br /> END<br />-- Verify adult's card is current!<br /> IF (SELECT expr_date FROM dbo.adult<br /> WHERE adult.member_no = @Adult_Member_no) < getdate()<br /> BEGIN<br /> SET @output_msg = 'AddJuvenile_SP - ERROR: Adult member card has expired!'<br /> SELECT @output_msgErrorMessage<br /> SET @NewMember_no = -5<br /> ROLLBACK TRANSACTION<br /> RETURN @NewMember_no<br /> END<br />-- Insert the new member row<br /> BEGIN TRY<br />INSERT INTO dbo.member<br />(lastname, firstname, middleinitial)<br />VALUES (@lastname, @firstname, @middleinitial)<br /> SET @NewMember_no = @@IDENTITY<br /> END TRY<br />
  7. 7. Add Juvenile (Cont.)<br />BEGIN CATCH<br />-- ERROR!<br /> SET @output_msg = CAST('AddJuvenile_SP - ERROR: ' AS char(25)) +<br />CAST(ERROR_NUMBER() AS varchar(5)) +<br />CAST(' LINE: ' AS char(8)) +<br />CAST(ERROR_LINE() AS varchar(4)) + <br />CAST(' ' AS char(1)) + ERROR_MESSAGE()<br /> SELECT @NewMember_noNewMember#, @output_msgErrorMessage<br />SET @NewMember_no = -3<br />ROLLBACK TRANSACTION<br />RETURN @NewMember_no<br /> END CATCH<br /> BEGIN TRY<br />-- Insert the new juvenile row.<br /> INSERT INTO dbo.juvenile<br />(member_no, adult_member_no, birth_date)<br />VALUES (@NewMember_no, @Adult_Member_no, @birth_date)<br /> END TRY<br /> BEGIN CATCH<br />-- ERROR!<br /> SET @output_msg = CAST('AddJuvenile_SP - ERROR: ' AS char(25)) +<br />CAST(ERROR_NUMBER() AS varchar(5)) +<br />CAST(' LINE: ' AS char(8)) +<br />CAST(ERROR_LINE() AS varchar(4)) + <br />CAST(' ' AS char(1)) + ERROR_MESSAGE()<br /> SELECT @NewMember_noNewMember#, @output_msgErrorMessage<br />SET @NewMember_no = -4<br />ROLLBACK TRANSACTION<br />RETURN @NewMember_no<br /> END CATCH<br />-- Send success message!<br /> SET @output_msg = CAST('AddJuvenile_SP - Member # ' AS char(26)) +<br />CAST(@NewMember_no AS char(8)) +<br />CAST(' successfully entered!' AS char(22))<br /> SELECT @output_msg [Success Message]<br /> COMMIT TRANSACTION<br /> RETURN @NewMember_no<br />;<br />go<br />
  8. 8. Testing:<br />BAD DATA!<br />INPUT:<br />-- NO DATA!<br />DECLARE @ret int<br />EXEC @ret = dbo.AddJuvenile_SP<br />SELECT @ret [Return Value]<br />RESULTS:<br />Msg 201, Level 16, State 4, Procedure AddJuvenile_SP, Line 0<br />Procedure or function 'AddJuvenile_SP' expects parameter '@Adult_Member_no', which was not supplied.<br />INPUT:<br />-- BAD DATA (missing parameter - birth date)<br />DECLARE @ret int<br />EXEC @ret = dbo.AddJuvenile_SP 16102, Jones, Fred<br />SELECT @ret [Return Value]<br />RESULTS:<br />Msg 201, Level 16, State 4, Procedure AddJuvenile_SP, Line 0<br />Procedure or function 'AddJuvenile_SP' expects parameter '@birth_date', which was not supplied.<br />INPUT:<br />-- BAD DATA (Adult member not in DB)<br />DECLARE @ret int<br />EXEC @ret = dbo.AddJuvenile_SP 20000, Jones, Fred, [1/1/2000], DEFAULT, @ret<br />SELECT @ret [Return Value]<br />RESULTS:<br />ErrorMessage<br />AddJuvenile_SP - ERROR: Adult sponsor for this juvenile does not exist!<br />Return Value<br />-2<br />GOOD DATA!<br />INPUT:<br />DECLARE @ret int, @Member_nosmallint<br />SET @Member_no = (SELECT TOP(1) a.member_no FROM dbo.adult a<br /> JOIN dbo.member m ON m.member_no = a.member_no<br /> WHERE m.lastname = 'Kunze' AND m.firstname = 'Arnold')<br />EXEC @ret = dbo.AddJuvenile_SP @Member_no, Kunze, Dane, [1/28/1999], DEFAULT, @ret<br />SELECT @ret [Returned Value]<br />SELECT firstname, lastname, j.member_no, birth_date<br /> FROM dbo.member m<br /> JOIN dbo.juvenile j<br /> ON j.member_no = m.member_no<br /> WHERE firstname = 'Dane' AND lastname = 'Kunze'<br />RESULTS:<br />Success Message<br />AddJuvenile_SP - Member # 10002 successfully entered!<br />Returned Value<br />10002<br />firstnamelastnamemember_nobirth_date<br />Dane Kunze 10002 1999-01-28 00:00:00.000<br />
  9. 9. Check In A Book<br />-------------------------------------------------------------------------<br />/*Book Check In Stored Procedure (dbo.BookCheckIn_SP)<br />This procedure is executed when a book is being checked in.<br />INPUT (and format): Position #<br />Member_no 1MUST EXIST<br /> Checking In Books<br />ISBN 2<br />COPY 3<br />RETURN VALUES<br /> -1 Invalid member number<br /> -2 Member owes fines<br /> -3 Member's card has expired<br /> -4 Book & Copy combination not in DB<br /> -5 Book was not on loan<br /> -6 Error inserting into Loan History<br /> -7 Error updating copy<br /> -8 Error deleting loan<br /> 0 if successful<br />*/<br />CREATE PROC dbo.BookCheckIn_SP<br /> @member_nosmallint, @ISBN int, @copy smallint, @output smallint<br />AS<br />BEGIN TRANSACTION<br />-- variable for returning error/success messages (troubleshooting)<br />DECLARE @output_msgvarchar(150)<br />--variable for checking if member owes fines & Count of books checked out<br />DECLARE @Fine_Valuesmallmoney, @BooksCheckedOutint, @title varchar(63),<br /> @title_noint, @on_loan char(1), @out_datedatetime, @due_datedatetime,<br /> @in_datedatetime<br />-- Verify member number<br />IF NOT EXISTS (SELECT member_no FROM dbo.member<br /> WHERE member.member_no = @member_no)<br /> BEGIN<br /> SET @output = -1<br /> SET @output_msg = CAST('BookCheckIn_SP - ERROR: ' AS char(25)) +<br /> CAST('Member number ' AS char(14)) +<br /> CAST(@member_no AS varchar(8)) +<br /> CAST(' does not exist!' AS char(16))<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br /> END<br />-- Check for outstanding fines<br />SET @Fine_Value = (SELECT SUM(ISNULL(fine_assessed, 0)) -<br /> (SUM(ISNULL(fine_paid, 0)) + SUM(ISNULL(fine_waived, 0)))<br /> FROM dbo.loanhist<br /> WHERE member_no = @member_no)<br />IF @Fine_Value > 0<br /> BEGIN<br /> SET @output = -2<br /> SET @output_msg = CAST('BookCheckIn_SP - ERROR: ' AS char(25)) +<br /> CAST('Member owes fines in total of $' AS char(31)) + <br /> CAST(@Fine_Value AS varchar(8))<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br /> END<br />-- Verify adult's card is current!<br />IF (SELECT expr_date FROM dbo.adult<br /> WHERE adult.member_no = @Member_no) < getdate()<br /> BEGIN<br /> SET @output_msg = 'BookCheckIn_SP - ERROR: Member card has expired!'<br /> SELECT @output_msgErrorMessage<br /> SET @output = -3<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br /> END<br />
  10. 10. Check In A Book (Cont.)<br />-- Verify if this member number actually has this book checked out!<br />IF NOT EXISTS(SELECT * FROM dbo.loan<br /> WHERE isbn = @ISBN AND<br />copy_no = @Copy AND<br />member_no = @Member_no)<br /> BEGIN<br /> SET @output_msg = CAST('BookCheckIn_SP - ERROR: Member ' AS char(32)) +<br /> CAST(@Member_no AS varchar(8)) + CAST(' does not have ISBN 'AS char(20)) +<br /> CAST(@ISBN AS varchar(8)) + CAST(', copy # ' AS char(9)) +<br /> CAST(@Copy AS varchar(8)) + CAST(' checked out!' AS char(13))<br /> SET @output = -5<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br />END<br />-- Check to see if ISBN/COPY exist in DB<br />IF NOT EXISTS (SELECT isbn, copy_no FROM dbo.copy<br /> WHERE isbn = @ISBN AND copy_no = @Copy)<br /> BEGIN<br /> SET @output = -4<br /> SET @output_msg = 'BookCheckIn_SP - ERROR: ISBN and Copy do not exist in database!'<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br /> END<br />-- Verify that book really was on loan<br />SET @on_loan = (SELECT on_loan FROM dbo.copy<br /> WHERE isbn = @ISBN AND<br />copy_no = @Copy)<br />IF @on_loan IN ('n', 'N')<br /> BEGIN<br /> SET @output = -5<br /> SET @title = (SELECT TOP(1) title<br /> FROM dbo.copy JOIN dbo.title<br /> ON title.title_no = copy.title_no<br /> WHERE copy.isbn = @ISBN AND copy.copy_no = @Copy)<br /> SET @output_msg = CAST('BookCheckIn_SP - ERROR: ' AS char(25)) +<br /> CAST(@ISBN AS varchar(10)) +<br /> CAST(', COPY: ' AS char(9)) +<br /> CAST(@COPY AS varchar(3)) + CAST(' - "' AS char(4)) +<br /> @title + CAST('" was NOT checked out!' AS char(22))<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br /> END<br />-- Passed all the tests, check it in!<br />IF (SELECT out_date FROM loan<br /> WHERE isbn = @ISBN AND copy_no = @Copy AND member_no = @Member_no)<br /> IS NOT NULL<br /> SET @out_date = (SELECT out_date FROM loan<br /> WHERE isbn = @ISBN AND copy_no = @Copy AND member_no = @Member_no)<br />ELSE<br /> SET @out_date = DATEADD(DAY, 14, (SELECT due_date FROM loan<br /> WHERE isbn = @ISBN AND copy_no = @Copy AND member_no = @Member_no))<br /> SET @due_date = (SELECT due_date FROM loan<br /> WHERE isbn = @ISBN AND copy_no = @Copy AND member_no = @Member_no)<br /> SET @title_no = (SELECT title_no FROM loan<br /> WHERE isbn = @ISBN AND copy_no = @Copy AND member_no = @Member_no)<br /> SET @in_date = getdate()<br />-- INSERT row into dbo.loanhist<br />BEGIN TRY<br /> INSERT INTO dbo.loanhist<br /> (isbn, copy_no, out_date, title_no, member_no, due_date, in_date)<br /> VALUES (@ISBN, @Copy, @out_date, @title_no, @Member_no, @due_date,<br /> @in_date)<br />-- NOTE: Specs didn't say anything about levying fines!<br />END TRY<br />BEGIN CATCH<br />-- INSERT failed!<br /> SET @output = -6<br /> SET @output_msg = CAST('BookCheckIn_SP - ERROR: ' AS char(25)) +<br /> CAST(ERROR_NUMBER() AS varchar(5)) +<br /> CAST(' LINE: ' AS char(8)) + CAST(ERROR_LINE() AS varchar(4)) +<br /> CAST(' inserting loanhist ISBN: ' AS char(26)) +<br /> CAST(@ISBN AS varchar(8)) + <br /> CAST(' ' AS char(1)) + ERROR_MESSAGE()<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br />END CATCH<br />
  11. 11. Check In A Book (Cont.)<br />-- UPDATE the on_loan column of the dbo.copy row!<br />BEGIN TRY<br /> UPDATE dbo.copy<br /> SET on_loan = 'N'<br /> WHERE isbn = @ISBN AND<br />copy_no = @Copy<br />END TRY<br />BEGIN CATCH<br />-- UPDATE failed!<br /> SET @output = -7<br /> SET @output_msg = CAST('BookCheckIn_SP - ERROR: ' AS char(25)) +<br /> CAST(ERROR_NUMBER() AS varchar(5)) +<br /> CAST(' LINE: ' AS char(8)) +<br /> CAST(ERROR_LINE() AS varchar(4)) +<br /> CAST(' updating copy ISBN: ' AS char(22)) +<br /> CAST(@ISBN AS varchar(8)) + CAST(' ' AS char(1)) + <br /> ERROR_MESSAGE()<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br />END CATCH<br />-- DELETE row from dbo.loan<br />BEGIN TRY<br /> DELETE dbo.loan<br /> WHERE isbn = @ISBN AND<br />copy_no = @Copy AND<br />member_no = @Member_no<br />END TRY<br />BEGIN CATCH<br />-- DELETE failed!<br /> SET @output = -8<br /> SET @output_msg = CAST('BookCheckIn_SP - ERROR: ' AS char(25)) +<br /> CAST(ERROR_NUMBER() AS varchar(5)) +<br /> CAST(' LINE: ' AS char(8)) + CAST(ERROR_LINE() AS varchar(4)) +<br /> CAST(' deleting loan ISBN: ' AS char(22)) +<br /> CAST(@ISBN AS varchar(8)) + CAST(' ' AS char(1)) +<br /> ERROR_MESSAGE()<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br />END CATCH<br />-- Book successfully checked in!<br />SET @output = 0<br />SET @output_msg = 'BookCheckIn_SP - Book successfully checked in! Have a great day!'<br />SELECT @output_msg [Success Message]<br />COMMIT TRANSACTION<br />RETURN @output<br />;<br />go<br />
  12. 12. Testing:<br />BAD DATA!<br />INPUT:<br />-- No Data!<br />DECLARE @ret int<br />EXEC @ret = dbo.BookCheckIn_SP<br />SELECT @ret [Return Value]<br />RESULTS:<br />Msg 201, Level 16, State 4, Procedure BookCheckIn_SP, Line 0<br />Procedure or function 'BookCheckIn_SP' expects parameter '@member_no', which was not supplied.<br />INPUT:<br />-- Member not in DB<br />DECLARE @ret int<br />EXEC @ret = dbo.BookCheckIn_SP 9999, 2, 1, @ret<br />SELECT @ret [Return Value]<br />RESULTS:<br />ErrorMessage<br />BookCheckIn_SP - ERROR: Member 9999 does not have ISBN 2, copy # 1 checked out!<br />Return Value<br />-5<br />INPUT:<br />-- Book not checked out!<br />DECLARE @ret int<br />EXEC @ret = dbo.BookCheckIn_SP 4, 2, 3, @ret<br />SELECT @ret [Return Value]<br />RESULTS:<br />ErrorMessage<br />BookCheckIn_SP - ERROR: Member 4 does not have ISBN 2, copy # 3 checked out!<br />Return Value<br />-5<br />GOOD DATA!<br />INPUT:<br />
  13. 13. Check Out A Book<br />--------------------------------------------------------------------------------<br />/*Book Check Out Stored Procedure (dbo.BookCheckOut_SP)<br />This procedure is executed when a book is being checked out.<br />INPUT (and format): Position #<br />member.member_no 1 MUST EXIST<br /> Checked Out Books<br />ISBN 2<br />COPY 3<br />RETURN VALUES<br /> -1 Invalid member number<br /> -2 Member owes fines<br /> -4 Book already checked out/not loanable<br /> -5 Book & Copy combination not in DB<br /> -6 Too many books<br /> -7 Error inserting into Loan<br /> -8 Member's card has expired<br /> -9 Book is not loanable<br /> 0 if successful<br />*/<br />CREATE PROC dbo.BookCheckOut_SP<br /> @member_nosmallint, @ISBN int, @copy smallint, @output smallint<br />AS<br />BEGIN TRANSACTION<br />-- variable for returning error/success messages (troubleshooting)<br />DECLARE @output_msgvarchar(150)<br />--variable for checking if member owes fines & Count of books checked out<br />DECLARE @Fine_Valuesmallmoney, @BooksCheckedOutint, @title varchar(63),<br /> @title_noint, @on_loan char(1), @out_datedatetime, @in_datedatetime,<br /> @due_datedatetime<br />--, @Book_Table TABLE (@BT_ISBN int, @BT_Copysmallint) --table to put input ISBN/copy#s into<br />-- Verify member number<br />IF NOT EXISTS (SELECT member_no FROM dbo.member<br /> WHERE member.member_no = @member_no)<br />BEGIN<br /> SET @output = -1<br /> SET @output_msg = CAST('BookCheckOut_SP - ERROR: ' AS char(26)) +<br />CAST('Member number ' AS char(14)) +<br />CAST(@member_no AS varchar(8)) +<br />CAST(' does not exist!' AS char(16))<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br />END<br />-- Check for outstanding fines<br />SET @Fine_Value = (SELECT SUM(ISNULL(fine_assessed, 0)) -<br /> (SUM(ISNULL(fine_paid, 0)) + SUM(ISNULL(fine_waived, 0)))<br /> FROM dbo.loanhist<br /> WHERE member_no = @member_no)<br />IF @Fine_Value > 0<br />BEGIN<br /> SET @output = -2<br /> SET @output_msg = CAST('BookCheckOut_SP - ERROR: ' AS char(26)) +<br /> CAST('Member owes fines in total of $' AS char(31)) + <br /> CAST(@Fine_Value AS varchar(8))<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br />END<br />-- Check for book already checked out<br />SET @on_loan = (SELECT on_loan FROM dbo.copy<br /> WHERE isbn = @ISBN AND<br />copy_no = @Copy)<br />IF @on_loan IN ('y', 'Y')<br />BEGIN<br /> SET @output = -4<br /> SET @title = (SELECT TOP(1) title<br /> FROM dbo.copy JOIN dbo.title<br /> ON title.title_no = copy.title_no<br /> WHERE copy.isbn = @ISBN AND copy.copy_no = @Copy)<br /> SET @output_msg = CAST('BookCheckOut_SP - ERROR: ' AS char(26)) +<br /> CAST(@ISBN AS varchar(10)) +<br /> CAST(', COPY: ' AS char(9)) +<br /> CAST(@COPY AS varchar(3)) + CAST(' - "' AS char(4)) +<br /> @title + CAST ('" is already checked out!' AS char(25))<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br />END<br />
  14. 14. Check Out A Book (Cont.)<br />-- Verify that book is loanable<br />SET @on_loan = (SELECT loanable FROM dbo.itemi<br /> WHERE i.isbn = @ISBN)<br />IF @on_loan NOT IN ('y', 'Y')<br />BEGIN<br /> SET @output = -4<br /> SET @title = (SELECT TOP(1) title<br /> FROM dbo.copy JOIN dbo.title<br /> ON title.title_no = copy.title_no<br /> WHERE copy.isbn = @ISBN AND copy.copy_no = @Copy)<br /> SET @output_msg = CAST('BookCheckOut_SP - ERROR: ' AS char(26)) +<br /> CAST(@ISBN AS varchar(10)) +<br /> CAST(', COPY: ' AS char(9)) +<br /> CAST(@COPY AS varchar(3)) + CAST(' - "' AS char(4)) +<br /> @title + CAST ('" is not loanable!' AS char(18))<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br />END<br />-- Check to see if ISBN/COPY exist in DB<br />IF NOT EXISTS (SELECT isbn, copy_no FROM dbo.copy<br /> WHERE isbn = @ISBN AND copy_no = @Copy)<br />BEGIN<br /> SET @output = -5<br /> SET @output_msg = 'BookCheckOut_SP - ERROR: ISBN and Copy do not exist in database!‘<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br />END<br />-- Check to see how many books member has checked out (can't have more than 4)<br />SET @BooksCheckedOut = (SELECT COUNT(member_no) FROM dbo.loan<br /> WHERE loan.member_no = @member_no)<br />IF @BooksCheckedOut > 3<br />BEGIN<br /> SET @output = -6<br /> SET @output_msg = 'BookCheckOut_SP - ERROR: Member already has four books checked out!‘<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br />END<br />-- Verify adult's card is current!<br />IF (SELECT expr_date FROM dbo.adult<br /> WHERE adult.member_no = @Member_no) < getdate()<br />BEGIN<br /> SET @output_msg = 'BookCheckOut_SP - ERROR: Adult member card has expired!‘<br /> SELECT @output_msgErrorMessage<br /> SET @output = -8<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br />END<br />-- Verify that book is loanable<br />IF (SELECT loanable FROM item WHERE isbn = @ISBN) IN ('n', 'N')<br />BEGIN<br /> SET @output_msg = 'BookCheckOut_SP - ERROR: Book is not loanable!‘<br /> SELECT @output_msgErrorMessage<br /> SET @output = -9<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br />END<br />-- Everything checks out so let's check this member out!<br />BEGIN TRY<br /> SET @title_no = (SELECT title_no FROM dbo.copy<br /> WHERE isbn = @ISBN AND<br />copy_no = @Copy)<br /> UPDATE dbo.copy<br /> SET on_loan = 'Y‘<br /> WHERE isbn = @ISBN AND<br />copy_no = @Copy<br />END TRY<br />BEGIN CATCH<br /> SET @output = -8<br /> SET @output_msg = CAST('BookCheckOut_SP - ERROR: ' AS char(26)) +<br /> CAST(ERROR_NUMBER() AS varchar(5)) +<br /> CAST(' LINE: ' AS char(8)) + CAST(ERROR_LINE() AS varchar(4)) +<br /> CAST(' updating copy for ISBN: ' AS char(25)) +<br /> CAST(@ISBN AS varchar(8)) +<br /> CAST(' ' AS char(1)) + ERROR_MESSAGE()<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br />END CATCH<br />BEGIN TRY<br /> SET @out_date = getdate()<br /> SET @due_date = (DATEADD(day, 14, getdate()))<br /> INSERT INTO dbo.loan<br /> VALUES (@ISBN, @Copy, @title_no, @Member_no,<br /> @out_date, @due_date)<br />END TRY<br />BEGIN CATCH<br /> SET @output = -8<br /> SET @output_msg = CAST('BookCheckOut_SP - ERROR: ' AS char(26)) +<br /> CAST(ERROR_NUMBER() AS varchar(5)) +<br /> CAST(' LINE: ' AS char(8)) + CAST(ERROR_LINE() AS varchar(4)) +<br /> CAST(' inserting loan for ISBN: ' AS char(27)) +<br /> CAST(@ISBN AS varchar(8)) +<br /> CAST(' ' AS char(1)) + ERROR_MESSAGE()<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br />END CATCH<br />SET @output = 0<br />SET @output_msg = 'BookCheckOut_SP - Book successfully checked out! Have a great day!'<br />SELECT @output_msg [Success Message]<br />COMMIT<br />RETURN @output<br />;<br />go<br />
  15. 15. Add A Book<br />-------------------------------------------------------------------------------<br />/*Add a Book (dbo.AddBook_SP)<br />This procedure is executed when a book is being added.<br />INPUT (and format): Position #<br /> New Book<br />ISBN 1<br />Title 2 OPTIONAL (If ISBN exists)<br />Author 3 OPTIONAL (If ISBN exists)<br />Translation 4 OPTIONAL (If ISBN exists)<br />Cover 5 OPTIONAL (If ISBN exists)<br />Synopsis 6 OPTIONAL<br />RETURN VALUES<br /> -1 Error updating copy<br /> -2 Missing Title<br /> -3 Missing Author<br /> -4 Missing Translation<br /> -5 Missing Cover<br /> -6 Error inserting into Title<br /> -7 Error inserting into Item<br /> -8 Error inserting into Copy<br /> ISBN if successful<br />*/<br />CREATE PROC dbo.AddBook_SP<br />@ISBN int, @Title varchar(63) = NULL, @Author varchar(31) = NULL,<br /> @Translation char(8) = NULL, @Cover char(8) = NULL,<br /> @Synopsis text = NULL, @ISBN_outint OUTPUT<br />AS<br />BEGIN TRANSACTION<br />-- Variables for use throughout this proc<br />DECLARE @output_msgvarchar(150), @next_copy_nosmallint, @title_noint,<br /> @output int<br />-- Does the ISBN already exist?<br />IF EXISTS (SELECT isbn FROM dbo.item<br /> WHERE isbn = @ISBN)<br /> BEGIN<br /> BEGIN TRY<br /> SET @next_copy_no = (SELECT MAX(copy_no) FROM dbo.copy<br /> WHERE isbn = @ISBN) + 1<br /> SET @title_no = (SELECT TOP(1) title_no FROM dbo.copy<br /> WHERE isbn = @ISBN)<br /> INSERT INTO dbo.copy<br /> VALUES (@ISBN, @next_copy_no, @title_no, 'N')<br /> END TRY<br /> BEGIN CATCH<br /> SET @output = -1<br /> SET @output_msg = CAST('AddBook_SP - ERROR: ' AS char(21)) +<br /> CAST(ERROR_NUMBER() AS varchar(5)) + CAST(' ' AS char(1)) +<br /> CAST(' LINE: ' AS char(8)) +<br /> CAST(ERROR_LINE() AS varchar(4)) +<br /> CAST(' updating copy for ISBN: ' AS char(26)) +<br /> CAST(@ISBN AS varchar(8)) + CAST(' ' AS char(1)) +<br /> ERROR_MESSAGE()<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br /> END CATCH<br /> SET @output_msg = CAST('AddBook_SP - ISBN ' AS char(18)) +<br /> CAST(@ISBN AS varchar(8)) + CAST(' copy # ' AS char(8)) +<br /> CAST(@next_copy_no AS varchar(8)) +<br /> CAST(' successfully added!' AS char(20))<br /> SELECT @output_msgErrorMessage<br /> COMMIT TRANSACTION<br /> RETURN @ISBN<br />END<br />-- Book doesn't already exist so must insert it into item, copy and title!<br />-- First, verify that all the needed parameters were passed!<br />-- GOT TITLE?<br />IF @Title IS NULL<br />BEGIN<br /> SET @output_msg =<br /> CAST('AddBook_SP - ERROR: Missing Title for new ISBN ' AS char(48)) +<br /> CAST(@ISBN AS varchar(8))<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN -2<br />END<br />
  16. 16. Add A Book (Cont.)<br />-- GOT AUTHOR?<br />IF @Author IS NULL<br />BEGIN<br /> SET @output_msg = CAST('AddBook_SP - ERROR: Missing Author for new ISBN ' AS char(49)) +<br /> CAST(@ISBN AS varchar(8))<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN -3<br />END<br />-- GOT TRANSLATION?<br />IF @Translation IS NULL<br />BEGIN<br /> SET @output_msg = CAST('AddBook_SP - ERROR: Missing Translation for new ISBN ' AS char(54)) +<br /> CAST(@ISBN AS varchar(8))<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN -4<br />END<br />-- GOT COVER?<br />IF @Cover IS NULL<br />BEGIN<br /> SET @output_msg = CAST('AddBook_SP - ERROR: Missing Cover for new ISBN ' AS char(48)) +<br /> CAST(@ISBN AS varchar(8))<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN -5<br />END<br />-- First must insert into title to get title_no<br />BEGIN TRY<br /> INSERT INTO dbo.title<br /> VALUES (@Title, @Author, @Synopsis)<br /> SET @title_no = @@IDENTITY<br />END TRY<br />BEGIN CATCH<br /> SET @output = -6<br /> SET @output_msg = CAST('AddBook_SP - ERROR: ' AS char(21)) +<br /> CAST(ERROR_NUMBER() AS varchar(5)) + ' ' +<br /> CAST(' LINE: ' AS char(8)) + CAST(ERROR_LINE() AS varchar(4)) +<br /> CAST(' inserting title for ISBN: ' AS char(28)) +<br /> CAST(@ISBN AS varchar(8)) + CAST(' ' AS char(1)) + ERROR_MESSAGE()<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br />END CATCH<br />-- Next, insert into item<br />BEGIN TRY<br /> INSERT INTO dbo.item<br /> VALUES (@ISBN, @title_no, @Translation, @Cover, 'Y')<br />END TRY<br />BEGIN CATCH<br /> SET @output = -7<br /> SET @output_msg = CAST('AddBook_SP - ERROR: ' AS char(21)) +<br /> CAST(ERROR_NUMBER() AS varchar(5)) + ' ' +<br /> CAST(' LINE: ' AS char(8)) + CAST(ERROR_LINE() AS varchar(4)) +<br /> CAST(' inserting item for ISBN: ' AS char(27)) +<br /> CAST(@ISBN AS varchar(8)) + CAST(' ' AS char(1)) + ERROR_MESSAGE()<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br />END CATCH<br />-- Finally, insert into copy<br />BEGIN TRY<br /> INSERT INTO dbo.copy<br /> VALUES (@ISBN, 1, @title_no, 'N')<br />END TRY<br />BEGIN CATCH<br /> SET @output = -8<br /> SET @output_msg = CAST('AddBook_SP - ERROR: ' AS char(21)) +<br /> CAST(ERROR_NUMBER() AS varchar(5)) + ' ' +<br /> CAST(' LINE: ' AS char(8)) + CAST(ERROR_LINE() AS varchar(4)) +<br /> CAST(' inserting copy for ISBN: ' AS char(27)) +<br /> CAST(@ISBN AS varchar(8)) + CAST(' ' AS char(1)) + ERROR_MESSAGE()<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> RETURN @output<br />END CATCH<br />SET @output_msg = CAST('AddBook_SP - ISBN: ' AS char(20)) +<br /> CAST(@ISBN AS varchar(8)) + CAST(' copy # 1' AS char(9)) +<br /> CAST(' successfully added!' AS char(20))<br />SELECT @output_msg Message<br />COMMIT TRANSACTION<br />RETURN @ISBN<br />;<br />go<br />
  17. 17. Renew Membership<br />----------------------------------------------------------------------<br />/*Renew Membership (dbo.RenewMembership_SP)<br />This procedure is executed when a membership is being renewed.<br />INPUT (and format): Position #<br /> Member Number 1<br />RETURN VALUES<br /> -1 Member not an adult<br /> -2 Member owes fines<br /> -3 Error updating adult<br /> Member Number if successful<br />*/<br />CREATE PROC dbo.RenewMembership_SP<br /> @Member_noint, @Member_no_outint OUTPUT<br />AS<br />BEGIN TRANSACTION<br />-- Variables for use throughout this proc<br />DECLARE @output_msgvarchar(150), @Fine_Valuesmallmoney<br />SET @Member_no_out = @Member_no<br />-- Check to make sure this is an adult membership<br />IF NOT EXISTS (SELECT member_no FROM dbo.adult<br />WHERE member_no = @Member_no)<br />BEGIN<br /> SET @output_msg = CAST('RenewMembership_SP - Member number ' AS char(35)) +<br /> CAST(@Member_no AS varchar(8)) +<br /> CAST(' is not an adult. Can not renew juveniles!' AS char(43))<br /> SELECT @output_msgErrorMessage<br /> SET @Member_no_out = -1<br /> ROLLBACK TRANSACTION<br /> RETURN @Member_no_out<br />END<br />-- Check to see if member owes any fines<br />SET @Fine_Value = (SELECT SUM(ISNULL(fine_assessed, 0)) –<br /> (SUM(ISNULL(fine_paid, 0)) + SUM(ISNULL(fine_waived, 0)))<br /> FROM dbo.loanhist<br /> WHERE member_no = @Member_no)<br />IF @Fine_Value > 0<br /> BEGIN<br /> SET @output_msg = CAST('RenewMembership_SP - ' AS char(21)) +<br /> CAST('Member owes fines in total of $' AS char(31)) + <br /> CAST(@Fine_Value AS varchar(8))<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> SET @Member_no_out = -2<br /> RETURN @Member_no_out<br /> END<br />-- Update expr_date to one year from now<br />BEGIN TRY<br /> UPDATE adult<br /> SET expr_date = DATEADD(Year, 1, getdate())<br />END TRY<br />BEGIN CATCH<br /> SET @output_msg = CAST('RenewMembership_SP - ERROR: ' AS char(29)) + <br /> CAST(ERROR_NUMBER() AS varchar(5)) + ' ' +<br /> CAST(' LINE: ' AS char(8)) + CAST(ERROR_LINE() AS varchar(4)) +<br /> CAST(' updating adult for Member #: ' AS char(31)) +<br /> CAST(@Member_no AS varchar(8)) + CAST(' ' AS char(1)) +<br /> ERROR_MESSAGE()<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> SET @Member_no_out = -3<br /> RETURN @Member_no_out<br />END CATCH<br />COMMIT TRANSACTION<br />SET @output_msg = CAST('Member: ' AS char(9)) +<br /> CAST(@Member_no AS varchar(8)) + CAST(' renewed!'AS char(9))<br />SELECT @output_msg [Message]<br />RETURN @Member_no<br />;<br />go<br />
  18. 18. Change Juvenile To Adult<br />/*Move over 18 year old Juveniles to adult table (dbo.ChangeJuvenile_SP)<br />This procedure is scheduled to run periodically to move juvenile members<br />who are now over 18 to the adult table.<br />INPUT (and format): Position #<br />None<br />RETURN VALUES<br /> -1 Error updating adult<br /> -2 Error deleting Juvenile<br /> -3 No Juveniles over 18<br /> 0 if successful (Note: Can't return single member<br /> number because might process <br />multiples. Am outputing each<br />processed member number on messages.<br />*/<br />CREATE PROC dbo.ChangeJuvenile_SP<br /> @Member_noint OUTPUT<br />AS<br />BEGIN TRANSACTION<br />-- Variables for use throughout this proc<br />DECLARE @output_msgvarchar(150), @row_countint, @j_expr_datedatetime<br />-- First, check to see if any of the juveniles are over 18!<br />BEGIN TRY<br /> IF NOT EXISTS (SELECT * FROM dbo.juvenile<br /> WHERE birth_date < DATEADD(Year, -18, getdate()))<br /> BEGIN<br /> SET @output_msg = 'ChangeJuvenile_SP - No over 18 year-old juveniles to move!‘<br /> SELECT @output_msg [Status Message]<br /> ROLLBACK TRANSACTION<br /> RETURN -3<br /> END<br />END TRY<br />BEGIN CATCH<br />-- Had a problem with EXISTS????<br /> SET @output_msg = CAST('ChangeJuvenile_SP - ERROR: ' AS char(28)) +<br /> CAST(ERROR_NUMBER() AS varchar(5)) + ' ' +<br /> CAST(' LINE: ' AS char(8)) + CAST(ERROR_LINE() AS varchar(4)) +<br /> CAST(' selecting (EXISTS) juveniles into jv_table.' AS char(44)) +<br /> CAST(' ' AS char(1)) + ERROR_MESSAGE()<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> SET @Member_no = -1<br /> PRINT @Member_no<br /> RETURN @Member_no<br />END CATCH<br />-- Move over 18 year-old Juveniles to Adult table<br />BEGIN TRY<br /> SET @j_expr_date = DATEADD(Year, 1, getdate())<br /> INSERT INTO dbo.adult (member_no, street, city, state, zip,<br />phone_no, expr_date)<br /> SELECT j.member_no, a.street, a.city, a.state, a.zip, a.phone_no,<br /> @j_expr_date<br /> FROM dbo.juvenile j<br /> JOIN dbo.adult a<br /> ON j.adult_member_no = a.member_no<br /> WHERE j.birth_date < DATEADD(Year, -18, getdate())<br /> SET @row_count = @@ROWCOUNT<br /> SET @output_msg = CAST('ChangeJuvenile_SP - ' AS char(20)) +<br /> CAST(@row_count AS varchar(8)) +<br /> CAST(' juveniles moved to adult table.' AS char(32))<br /> SELECT @output_msg [Juveniles moved]<br />END TRY<br />BEGIN CATCH<br />
  19. 19. Change Juvenile To Adult (Cont.)<br />-- Move didn't work!<br /> SET @output_msg = CAST('ChangeJuvenile_SP - ERROR: ' AS char(28)) +<br /> CAST(ERROR_NUMBER() AS varchar(5)) +<br /> CAST(' LINE: ' AS char(8)) + CAST(ERROR_LINE() AS varchar(4)) +<br /> CAST(' selecting juveniles into jv_table.' AS char(35)) +<br /> CAST(' ' AS char(1)) + ERROR_MESSAGE()<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> SET @Member_no = -1<br /> PRINT @Member_no<br /> RETURN @Member_no<br />END CATCH<br />-- Report Juveniles who got moved!<br />SELECT j.member_no [Member #], a.street [Street], a.city [City],<br />a.state [State], a.zip [Zip Code], a.phone_no [Phone #],<br /> @j_expr_date [Expires], 'Moved to adult' [-- Moved --]<br /> FROM dbo.juvenile j<br /> JOIN dbo.adult a<br /> ON j.adult_member_no = a.member_no<br /> WHERE j.birth_date < DATEADD(Year, -18, getdate())<br />-- Now, delete them from the juvenile table<br />BEGIN TRY<br /> DELETE FROM dbo.juvenile<br /> WHERE birth_date < DATEADD(Year, -18, getdate())<br /> SELECT 'ChangeJuvenile_SP - Over 18 year-old juvenile(s) deleted!' [-- Deleted --]<br />END TRY<br />BEGIN CATCH<br />-- That didn't work!<br /> SET @output_msg = CAST('ChangeJuvenile_SP - ERROR: ' AS char(29)) +<br /> CAST(ERROR_NUMBER() AS varchar(5)) + CAST(' LINE: ' AS char(8)) +<br /> CAST(ERROR_LINE() AS varchar(10)) +<br /> CAST(' deleting juvenile row(s)' AS char(25)) +<br /> CAST(' ' AS char(1)) + ERROR_MESSAGE()<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> SET @Member_no = -2<br /> PRINT @Member_no<br /> RETURN @Member_no<br />END CATCH<br />-- Success!!!!!<br />COMMIT TRANSACTION<br />RETURN 0<br />;<br />go<br />
  20. 20. Update Adult<br />/* Update adult stored procedure (dbo.UpdateAdult_SP)<br />This procedure allows the librarian to input changes to be made to the<br />adult's information.<br />INPUT (and format): Position #<br /> Member number 1<br />NOTE: Except for member number, all other fields are OPTIONAL<br /> --BUT-- it's pointless to call this proc if you're not<br /> going to intput some change!<br /> Member name<br />Last name 2<br />First name 3<br />Middle Initial 4<br />Street5<br />City6<br />State7<br />Zip Code 8<br /> Phone Number 9<br />RETURN VALUES<br /> -1 Member does not exist<br /> -2 Update requested without submitting any updated information!<br /> -3 Error updating adult<br /> -4 Error updating member<br /> Member Number if successful<br />*/<br />CREATE PROC dbo.UpdateAdult_SP<br /> @Member_no int, @Last_Name varchar(15) = NULL,<br /> @First_Namevarchar(15) = NULL, @Middle_Initial char(1) = NULL,<br /> @Street varchar(15) = NULL, @City varchar(15) = NULL,<br /> @State char(2) = NULL, @Zip char(10) = NULL, @Phone_no char(10) = NULL,<br /> @Member_no_outint OUTPUT<br />AS<br />BEGIN TRANSACTION<br />-- Variables for use throughout this proc<br />DECLARE @output_msgvarchar(150)<br />SET @Member_no_out = @Member_no<br />-- Does the member number actually exist?<br />IF NOT EXISTS (SELECT member_no FROM dbo.adult<br /> WHERE member_no = @Member_no)<br />BEGIN<br /> SET @output_msg = CAST('UpdateAdult_SP - ERROR: ' AS char(25)) +<br /> CAST('Member number ' AS char(14)) +<br /> CAST(@Member_no AS varchar(8)) +<br /> CAST(' does not exist!' AS char(16))<br /> SET @Member_no_out = -1<br /> SELECT @output_msg [Error Message]<br /> ROLLBACK TRANSACTION<br /> RETURN @Member_no_out<br />END<br />-- Did you give me any information to update?<br />IF @Last_Name IS NULL AND @First_Name IS NULL AND @Middle_Initial IS NULL<br /> AND<br /> @Street IS NULL AND @City IS NULL AND @State IS NULL AND<br /> @Zip IS NULL AND @Phone_no IS NULL<br />BEGIN<br /> SET @output_msg = CAST('UpdateAdult_SP - ERROR: ' AS char(25)) +<br /> CAST('You did not give me any data to change for member number ' AS char(57)) +<br /> CAST(@Member_no AS varchar(8)) + CAST('!' AS char(1))<br /> SELECT @output_msg [Error Message]<br /> SET @Member_no_out = -2<br /> ROLLBACK TRANSACTION<br /> RETURN @Member_no_out<br />END<br />
  21. 21. Update Adult (Cont.)<br />-- Figure out what information was provided and change it in adult<br />BEGIN TRY<br /> IF @Street IS NOT NULL<br /> UPDATE dbo.adult<br /> SET street = @Street<br /> WHERE member_no = @Member_no<br /> IF @City IS NOT NULL<br /> UPDATE dbo.adult<br /> SET city = @City<br /> WHERE member_no = @Member_no<br /> IF @State IS NOT NULL<br /> UPDATE dbo.adult<br /> SET state = @State<br /> WHERE member_no = @Member_no<br /> IF @Zip IS NOT NULL<br /> UPDATE dbo.adult<br /> SET zip = @Zip<br /> WHERE member_no = @Member_no<br /> IF @Phone_no IS NOT NULL<br /> UPDATE dbo.adult<br /> SET phone_no = @Phone_no<br /> WHERE member_no = @Member_no<br />END TRY<br />BEGIN CATCH<br />-- Error updating adult table!<br /> SET @output_msg = CAST('UpdateAdult_SP - ERROR: ' AS char(25)) +<br /> CAST(ERROR_NUMBER() AS varchar(5)) + CAST(' LINE: ' AS char(8)) +<br /> CAST(ERROR_LINE() AS varchar(4)) +<br /> CAST(' deleting juvenile row(s)' AS char(25)) +<br /> CAST(' ' AS char(1)) + ERROR_MESSAGE()<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> SET @Member_no_out = -3<br /> PRINT @Member_no_out<br /> RETURN @Member_no_out<br />END CATCH<br />-- Okay, now for the member table!<br />BEGIN TRY<br /> IF @Last_Name IS NOT NULL<br /> UPDATE dbo.member<br /> SET lastname = @Last_Name<br /> WHERE member_no = @Member_no<br /> IF @First_Name IS NOT NULL<br /> UPDATE dbo.member<br /> SET firstname = @First_Name<br /> WHERE member_no = @Member_no<br /> IF @Middle_Initial IS NOT NULL<br /> UPDATE dbo.member<br /> SET middleinitial = @Middle_Initial<br /> WHERE member_no = @Member_no<br />END TRY<br />BEGIN CATCH<br />-- Problem upading member table!<br /> SET @output_msg = CAST('UpdateAdult_SP - ERROR: ' AS char(25)) +<br /> CAST(ERROR_NUMBER() AS varchar(5)) +<br /> CAST(' LINE: ' AS char(8)) + CAST(ERROR_LINE() AS varchar(4)) +<br /> CAST(' updating Adult' AS char(25)) +<br /> CAST(' ' AS char(1)) + ERROR_MESSAGE()<br /> SELECT @output_msgErrorMessage<br /> ROLLBACK TRANSACTION<br /> SET @Member_no_out = -4<br /> PRINT @Member_no_out<br /> RETURN @Member_no_out<br />END CATCH<br />-- Success! Let them know it!<br />SET @output_msg = CAST('UpdateAdult_SP - Member number ' AS char(31)) +<br /> CAST(@Member_no AS varchar(8)) +<br /> CAST(' successfully updated!' AS char(22))<br />COMMIT TRANSACTION<br />SELECT @output_msg<br />PRINT @Member_no_out<br />RETURN @Member_no_out<br />;<br />go<br />
  22. 22. MORE TO COME!!!<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×