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 userdefined
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 : Create a Stored Procedure to Insert a New Book Record
Write a stored procedure named 'InsertBook' that takes three parameters: @Title
NVARCHAR @AuthorID INT and @Price DECIMAL The procedure should
insert a new book record into the 'Books' table. If there is an issue eg 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 : Create a Function to Calculate Total Sales for a Book
Write a userdefined 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
CODE FOR UDF:
Task : Create a Stored Procedure to Update Book Information
Write a stored procedure named 'UpdateBookInfo' that takes three parameters: @BookID
INT @NewTitle NVARCHAR and @NewPrice DECIMAL 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
