我有一个数据库,用来存放客户的租金。如果客户租房10次,那么他们就是高级客户。我如何使一个触发,使客户只是一个优质客户后,他们有10个租金?
我对sql非常陌生。任何帮助都将不胜感激。
当我尝试这个查询错误时,我一直会得到这个错误:当子查询没有被引入时,只能在select列表中指定一个表达式。
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
发布于 2014-03-26 01:10:03
那这个呢?
-- **********************
-- 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;
https://stackoverflow.com/questions/22648919
复制相似问题