Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using SQL Server. Myguitarshop database. 1.Write a script that creates and calls

ID: 3865021 • Letter: U

Question

using SQL Server. Myguitarshop database.

1.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/arguments. One parameter/argument for each of these columns: CategoryID (int), ProductCode (varchar), ProductName (varchar), ListPrice (smallmoney), and DiscountPercent (decimal). In the procedure, validate the following: If the value for the ListPrice column is a negative number, the stored procedure should throw an error (using THROW) that indicates that this column doesn’t accept negative numbers. If the value for the DiscountPercent column is a negative number, the stored procedure should also throw a similar error (using THROW). When inserting data into the Products table, set the Description column to an empty string and set the DateAdded column to the current date.

2. Code at least two EXEC statements that test this procedure (one successful, one failure)

Explanation / Answer

USE Myguitarshop;
GO
CREATE PROCEDURE spInsertProduct
@CategoryID int,   
@ProductCode varchar(50),
   @ProductName nvarchar(50),
   @ListPrice smallmoney,
   @DiscountPercent decimal
AS   

SET NOCOUNT ON;
   if   (@ListPrice <0)
   begin
   THROW 60000, 'ListPrice cannot be negative'
   end
   else if   (@DiscountPercent <0)
   begin
   THROW 60000, 'DiscountPercent cannot be negative'
   end  
   else
   begin
   INSERT INTO Products (CategoryID,ProductCode,ProductName,ListPrice,DiscountPercent,Decription,DateAdded)
   VALUES (@CategoryID,@ProductCode,@ProductName,@ListPrice,@DiscountPercent,'',getdate());
   end  
GO

--sucessful
EXEC spInsertProduct ('c001','p001','facial cream',90,1.5);
--failure
EXEC spInsertProduct ('c001','p001','facial cream',-90,1.5)