1. Write a script that creates and calls a stored procedure named spInsertCatego
ID: 3680898 • Letter: 1
Question
1. Write a script that creates and calls a stored procedure named spInsertCategory. First, code a statement that creates a procedure that adds a new row to the Categories table. To do that, this procedure should have one parameter for the category name. Code at least two EXEC statements that test this procedure. (Note that this table doesn’t allow duplicate category names.)
2. Write a script that creates a function named fnDiscountPrice that calculates the discount price of an item in the OrderItems table (discount amount subtracted from item price). To do that, this function should accept one parameter for the item ID, and it should return the value of the discount price for that item. Write a query using the fnDiscountPrice function that shows the ItemID, Item price, discount amount, and the calculated value returned from the function – detail columns come from the OrderItems table.
3. Write a script that creates a function named fnItemTotal that calculates the total amount of an item in the OrderItems table (fnDiscountPrice result multiplied by the quantity). To do that, this function should accept one parameter for the item ID, it should use the fnDiscountPrice function that you created in exercise 2, and it should return the value of the total for the item. Write a query using the fnItemTotal function and the fnDiscountPrice function to display – ItemID, item price, discount amount, discount price, quantity, and item total.
4. Write a script that creates and calls a stored procedure named spInsertProduct that inserts a row into the Products table. This stored procedure should accept five parameters. One parameter for each of these columns: CategoryID, ProductCode, ProductName, ListPrice, and DiscountPercent. This stored procedure should set the Description column to an empty string, and it should set the DateAdded column to the current date. If the value for the ListPrice column is a negative number, the stored procedure should raise an error that indicates that this column doesn’t accept negative numbers. Similarly, the procedure should raise an error if the value for the DiscountPercent column is a negative number. Code at least two EXEC statements that test this procedure.
5. Write a script that creates and calls a stored procedure named spUpdateProductDiscount that updates the DiscountPercent column in the Products table. This procedure should have one parameter for the product ID and another for the discount percent. If the value for the DiscountPercent column is a negative number, the stored procedure should raise an error that indicates that the value for this column must be a positive number. Code at least two EXEC statements that test this procedure.
6. Create a trigger named Products_UPDATE that checks the new value for the DiscountPercent column of the Products table. This trigger should raise an appropriate error if the discount percent is greater than 100 or less than 0. If the new discount percent is between 0 and 1, this trigger should modify the new discount percent by multiplying it by 100. That way, a discount percent of .2 becomes 20. Test this trigger with an appropriate UPDATE statement.
7. Create a trigger named Products_INSERT that inserts the current date for the DateAdded column of the Products table if the value for that column is null. Test this trigger with an appropriate INSERT statement.
8. Create a table named ProductsAudit. This table should have all columns of the Products table, except the Description column. Also, it should have an AuditID column for its primary key, and the DateAdded column should be changed to DateUpdated. Create a trigger named Products_UPDATE. This trigger should insert the old data about the product into the ProductsAudit table after the row is updated. Then, test this trigger with an appropriate UPDATE statement
Explanation / Answer
Q1.
CREATE PROCEDURE dbo.spInsertCategory
@CategoryName NVARCHAR(50) = NULL
AS
BEGIN
SET NOCOUNT ON
INSERT INTO dbo.spInsertCategory
(
CategoryName
)
VALUES
(
@CategoryName
)
END
GO
exec dbo.terms
@CategoryName = 'Category2'
exec dbo.terms
@CategoryName = 'Category1'
Q2.
CREATE FUNCTION fnDiscountPrice
(@ItemID int)
RETURNS cost
BEGIN
RETURN (SELECT SUM(ItemPrice - Discount) AS Price_Disc
FROM OrderItems
WHERE ItemID = @ItemID);
END;
//Now print the discounted price using dbo.fnDiscountPrice
Q3.
CREATE FUNCTION fnItemTotal(@ItemID int)
RETURNS cost
BEGIN
RETURN (
SELECT
dbo.fnDiscountPrice(@ItemId) * (
SELECT Qty
FROM OrderItems
WHERE ItemID = @ItemID
)
)
END;
// Now print the final price using dbo.fnItemTotal
Q4.
CREATE TRIGGER Products_INSERT
ON Products
AFTER INSERT
AS
UPDATE Products
SET DateAdded = GETDATE()
WHERE DateAdded IS NULL OR
DateAdded IN (SELECT DateAdded FROM my_inserted_tbl);
INSERT INTO Products (CategoryID, ProductCode, ProductName, ListPrice,DiscountPercent,Description, DateAdded)
VALUES (1234, 'My_Code_1', 'My Prod 1',
10, 0.5, 'My first test procedure',NULL)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.