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

The purpose of this assignment is to add parameters around data entry through tr

ID: 3745239 • Letter: T

Question

The purpose of this assignment is to add parameters around data entry through triggers that are launched when specific conditions are met.

Due to warehousing limitations, inventory over 800 units needs to be sent to an external storage site and tracked separately. You have been asked to monitor when an update will exceed this boundary so it can be addressed in production meetings.

Write a trigger titled "tgrExcessInventory" for the Production.ProductInventory table to ensure the quantity can never exceed 800 units.

Modify the trigger created in step 1 to execute its check code only if the Quantity column is updated.

USE Adventure Works 2012

Explanation / Answer

USE Adventure Works 2012;

create trigger tgrExcessInventory

on Production.ProductInventory

INSTEAD OF update

as

declare @qty int,

@ProductID int,

@LocationID smallint,

@Shelf nvarchar(10),

@Bin tinyint,

@rowguid uniqueidentifier,

@ModifiedDate datetime;

select @qty =i.Quantity from inserted i;

select @ProductID=i.ProductID from inserted i;

select @LocationID=i.LocationID from inserted i;

select @Shelf=i.Shelf from inserted i;

select @Bin=i.Bin from inserted i;

select @rowguid=i.rowguid from inserted i;

select @ModifiedDate=i.ModifiedDate from inserted i;

Begin

Begin tran

if(@qty>800)

begin

RAISERROR('Cannot Insert Quantity >800',16,1); ROLLBACK; end

Else Begin

insert into Production.ProductInventory values(@ProductID,@LocationID,@Shelf,@Bin,@qty,@rowguid,@ModifiedDate);

COMMIT;   

End

End