Creating Stored Procedures and User Defined Functions
This HW uses the books database from the book "SQL, Visual Quickstart Guide, 2nd edition" by Fehily. The following is the ERD for the database. See page 39 in the book for more information about the tables.
- Write
a stored proceudre called "createBackupTables" that uses
"select into" to create the following tables. Each
"Backup" table should have the same structure as the original
table. If the backup table already exists, it should be dropped first.
- title_authors_Backup
- publishers_Backup
- titles_backup
- royalties_backup
- Write
a stored procedure named "removePublisher" that removes a
publisher record and all associated records from the title_authors, titles
and royalties tables.
The stored procedure should place copies of the deleted records in the
Backup tables (created in the last assignment). If any backup table does
not exist, the procedure should call "createBackupTables" (from
the previous question).
The procedure should take a single input parameter for the id of the
publisher. The procedure should have 3 OUTPUT parameters that will be
assigned the number of records removed from each of the 3 tables,
title_authors, titles and royalties.
Make sure that all relevant SQL statements are coded inside a transaction.
- Write
a stored procdure named unremovePublisher that undoes the effects of the
previous procedure. Make sure that all relevant SQL statements are coded
inside a transaction.
- Write
a user defined function that returns the total number of pages that a
specific author has written in all his books.
The function should take a single parameter for the author id (au_id) and
return an integer.