I have a assignment due so any help would help a lot thank you!
Not sure if I did it right any help would be great thank you!
Create a trigger so that no sales transaction can be initially entered or modified that is larger than the credit limit. If the sales transaction is over the credit limit, roll back the transaction.
My Code
Create Trigger salesTransaction on Customers
For insert, update
as
--Get Credit limit
Declare @@Creditlimit money
select @@Creditlimit = CreditLimt from inserted i
Declare @@amount money
select @@amount = (p.Price * s.qtyOrdered) from inserted i
inner join Orders o
on i.CustomerNo = o.CustomerNo
inner join SalesDetail s
on s.OrderNo = o.OrderNo
inner join Products p
on (p.ManufactureID + p.ProductID) = (s.ManufactureID + s.ProductID)
where (i.CustomerNo = o.CustomerNo and o.CustomerNo = s.OrderNo and s.ManufactureID = p.ManufactureID and s.ProductID = p.ProductID)
if(@@amount > @@Creditlimit)
Begin
rollback transaction
End
My tables
Create Table Customers
(
CustomerNo char(4)
Constraint ck_CustomerNoHas4positionsWithNumbers
Check(CustomerNo like'[0-9],[0-9],[0-9],[0-9]'),
Company varchar(50) not null,
CustomerRep char(3),
CreditLimt money default(20000.00),
PRIMARY KEY(CustomerNo)
)
Alter Table Customers
Add constraint fk_customerrep
FOREIGN KEY (CustomerRep)
REFERENCES Salesreps(EmployeeNo)
Create Table Orders
(
OrderNo int,
OrderDate Date not null,
CustomerNo char(4) not null,
SalesRep char(3) not null
PRIMARY KEY(OrderNo)
)
Alter Table Orders
Add constraint fk_customerno
FOREIGN KEY (CustomerNo)
REFERENCES Customers(CustomerNo),
constraint fk_salesrep
FOREIGN KEY (SalesRep)
REFERENCES Salesreps(EmployeeNo)
Create Table SalesDetail
(
SaleDetailID int,
ManufactureID char(3) Constraint ck_ManufactureIDFromSaleDetails check(ManufactureID like'[a-z],[a-z],[a-z]') not null,
ProductID char(5) Constraint ck_ProductIDSalesDetail check(ProductID like'[0-9],[0-9],[a-z],[a-z],[a-z]') not null,
OrderNo int,
qtyOrdered int
PRIMARY KEY(SaleDetailID)
)
Alter Table SalesDetail
add FOREIGN KEY (OrderNo)
REFERENCES Orders(OrderNo)
Alter Table SalesDetail
Add constraint fk_SalesDetails_Mid_Pid
FOREIGN KEY (ManufactureID, ProductID)
REFERENCES Products(ManufactureID, ProductID)
Create Table Products
(
ManufactureID char(3)
Constraint ck_ManufactureIDifItHasLettersOnly check(ManufactureID like'[a-z],[a-z],[a-z]'),
ProductID char(5)
Constraint ck_ProductIDhasTwoLettersAndThreeNumbers check(ProductID like'[a-z],[a-z],[0-9],[0-9],[0-9]'),
Description varchar(50) not null,
Price money not null,
QtyOnHand int not null,
PRIMARY KEY(ManufactureID, ProductID)
)
Several things:
WHY is the trigger on Customer
?? You don't want to check these conditions when a new customer is inserted (or updated) - but rather when a new Order is inserted - no??
Your code doesn't take into account the fact that the trigger will be called once per statement and that this one statement can very well insert (or update) multiple orders at once - therefore, the Inserted
pseudo table can (and will!) contain multiple entries, and your code doesn't work with multiple entries....
So my take would be:
Orders
table Inserted
CreditLimit
- if that's greater than zero (at least one order exceeds that limit), I roll back the transaction. Here's the trigger code:
CREATE TRIGGER salesTransaction ON dbo.Orders
FOR INSERT, UPDATE
AS
--Get Credit limit
DECLARE @Count INT
SELECT @COUNT = COUNT(*)
FROM Inserted i
INNER JOIN dbo.Orders o ON i.CustomerNo = o.CustomerNo
INNER JOIN SalesDetail s ON s.OrderNo = o.OrderNo
INNER JOIN Products p ON p.ManufactureID = s.ManufactureID AND m.Product = s.ProductID
INNER JOIN dbo.Customer c ON o.CustomerNo = c.CustomerNo
WHERE (p.Price * s.qtyOrdered) > c.CreditLimit
IF (@Count > 0)
ROLLBACK TRANSACTION
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.