Question: This homework assignment focuses on working with stored procedures and user - defined functions using the 'Bookstore' database. Complete the tasks below by writing and

This homework assignment focuses on working with stored procedures and user-defined
functions using the 'Bookstore' database. Complete the tasks below by writing and
executing SQL code based on the provided instructions. Make sure to use proper syntax and
error handling as needed.
Task 1: Create a Stored Procedure to Insert a New Book Record
Write a stored procedure named 'InsertBook' that takes three parameters: @Title
(NVARCHAR(100)), @AuthorID (INT), and @Price (DECIMAL(10,2)). The procedure should
insert a new book record into the 'Books' table. If there is an issue (e.g., duplicate entry),
handle it using a TRY...CATCH block.
CODE FOR STORED PROCEDURE:
EXECUTION CODE: (EXEC COMMAND)
Verify the result by selecting the newly inserted book:
Task 2: Create a Function to Calculate Total Sales for a Book
Write a user-defined function named 'CalculateTotalSales' that takes one parameter:
@BookID (INT). The function should calculate and return the total sales amount for the
given book by multiplying the quantity sold by the price. If there are no sales, return 0.
CODE FOR UDF:
Task 3: Create a Stored Procedure to Update Book Information
Write a stored procedure named 'UpdateBookInfo' that takes three parameters: @BookID
(INT), @NewTitle (NVARCHAR(100)), and @NewPrice (DECIMAL(10,2)). The procedure
should update the title and price of the specified book. Use a TRY...CATCH block to handle
any potential errors.
CODE FOR STORED PROCEDURE:
EXECUTION CODE: (EXEC COMMAND)
Verify the result by selecting the newly inserted book:
VERIFICATION CODE: (SELECT STATEMENT)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!