2014-03-26 131 views
0

我有一个数据库,包含一个客户的租金。如果客户进行10次租赁,那么他们是高端客户。我如何制作触发器,以便客户在有10次租赁后才成为高级客户?SQL创建触发器

我真的很新的SQL。任何帮助将不胜感激。

我一直收到这个错误,当我尝试这个查询 错误:只有一个表达式可以在子查询没有引入EXISTS时在选择列表中指定。

CREATE TRIGGER tIsPremium 
ON UserAccount 
FOR UPDATE 
AS 
IF EXISTS 
(
    SELECT 'TRUE' 
    FROM UserAccount u JOIN Rental r 
     ON u.userAccount_ID = r.userAccount_ID 
    WHERE (SELECT DISTINCT r.rental_ID, COUNT(*) AS Rentals 
      FROM Rental r INNER JOIN UserAccount u 
       ON r.userAccount_ID = u.userAccount_ID 
      GROUP BY r.rental_ID 
      ) >= 10 
) 
BEGIN 
    RAISERROR('Cannot make user premiums if they do not have at least 10 rentals.',16,1) 
    ROLLBACK TRAN 
END 

回答

0

这是怎么回事?

-- ********************** 
-- Very simple datamodel: 
-- ********************** 
CREATE TABLE Customer(
    IdCustomer int not null, -- PK 
    IsLoyalCustomer bit not null default 0) 
GO 

CREATE TABLE Rental(
    IdRental int not null, -- PK for Rental 
    IdCustomer int not null) -- FK to Customer 
GO 

-- ******************** 
-- Here is the trigger: 
-- ******************** 
CREATE Trigger RentalTrigger ON Rental 
AFTER INSERT, UPDATE, DELETE 
AS 
BEGIN 

-- Set based operation, handling insert, update and delete commands: 
-- Find all customers involved in this set based operation (=> RentalCustomers) 
-- Inner Join With Rental => Gives all rentals where these RentalCustomers are involved 
-- Recalculate IsLoyalCustomer for each RentalCustomer 
-- Update those Customer in the Customer table where IsLoyalCustomer is changed. 
UPDATE Customer SET IsLoyalCustomer = RentalCustomerStats.IsLoyalCustomer 
FROM 
(SELECT 
    Rental.IdCustomer, 
    IsLoyalCustomer = CASE WHEN COUNT(*) < 10 THEN 0 ELSE 1 END 
FROM 
    Rental 
    INNER JOIN (
     SELECT DISTINCT IdCustomer FROM inserted 
     UNION 
     SELECT DISTINCT IdCustomer FROM deleted) AS RentalCustomers 
      ON Rental.IdCustomer = RentalCustomers.IdCustomer 
    GROUP BY Rental.IdCustomer) 
AS RentalCustomerStats 
WHERE 
    Customer.IdCustomer = RentalCustomerStats.IdCustomer 
    AND Customer.IsLoyalCustomer <> RentalCustomerStats.IsLoyalCustomer; 
END; 
GO 

-- ******** 
-- Testing: 
-- ******** 
-- Add Customer: 
INSERT INTO Customer(IdCustomer) VALUES(1); 
-- Add Purchases for this customer: 
INSERT INTO Rental(IdCustomer, IdRental) VALUES(1,1); 
INSERT INTO Rental(IdCustomer, IdRental) VALUES(1,2); 
INSERT INTO Rental(IdCustomer, IdRental) VALUES(1,3); 
INSERT INTO Rental(IdCustomer, IdRental) VALUES(1,4); 
INSERT INTO Rental(IdCustomer, IdRental) VALUES(1,5); 
INSERT INTO Rental(IdCustomer, IdRental) VALUES(1,6); 
INSERT INTO Rental(IdCustomer, IdRental) VALUES(1,7); 
INSERT INTO Rental(IdCustomer, IdRental) VALUES(1,8); 
INSERT INTO Rental(IdCustomer, IdRental) VALUES(1,9); 
-- Not yet loyal: 
SELECT * FROM Customer; 
-- Add more rentals for this customer: 
INSERT INTO Rental(IdCustomer, IdRental) VALUES(1,10); 
-- Now the customer is loyal: 
SELECT * FROM Customer; 

-- Add more rentals for this customer: 
INSERT INTO Rental(IdCustomer, IdRental) VALUES(1,11); 
INSERT INTO Rental(IdCustomer, IdRental) VALUES(1,12); 
-- Still loyal: 
SELECT * FROM Customer; 

-- Remove some rentals: 
DELETE FROM Rental WHERE IdRental > 9; 
-- Not loyal customer anymore: 
SELECT * FROM Customer; 

-- The remaining rentals: 
SELECT * FROM Rental;