首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SQL创建触发器

SQL创建触发器
EN

Stack Overflow用户
提问于 2014-03-26 00:07:49
回答 1查看 83关注 0票数 0

我有一个数据库,用来存放客户的租金。如果客户租房10次,那么他们就是高级客户。我如何使一个触发,使客户只是一个优质客户后,他们有10个租金?

我对sql非常陌生。任何帮助都将不胜感激。

当我尝试这个查询错误时,我一直会得到这个错误:当子查询没有被引入时,只能在select列表中指定一个表达式。

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-26 01:10:03

那这个呢?

代码语言:javascript
运行
复制
-- **********************
-- 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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22648919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档