如何在更新值不是空的情况下,在Materialized View表中用触发器设置父表的更新值。

问题描述 投票:0回答:1

我使用的是 C#'s EF Core 它的一个很好的好处是,我可以动态地更新在 Users 模型的一种方法。我想做一个触发器来感知当我的 token 表的属性 Users 是更新的。这很容易,但是,让我们说 Users 有两个 '子类物质化视图' DonatorsStaffs. 我希望下面这些对称的属性在通过触发器更新时能够匹配起来。


CREATE TABLE [Users](
    email VARCHAR(320) NOT NULL,
    password_salt varbinary(max) NOT NULL,
    password_hash varbinary(max) NOT NULL,
    token VARCHAR(320),
    /*d=Donator | s=Staff*/
    roles VARCHAR (2) NOT NULL,
    /*For: Donator */
    amount_donated MONEY,
    /*For: Staff */
    title VARCHAR(128),
    PRIMARY KEY (email),
);
/***Role Based Sub-Class Tables***/
CREATE TABLE Donators(
    email VARCHAR(320) NOT NULL,
    password_salt varbinary(max) NOT NULL,
    password_hash varbinary(max) NOT NULL,
    token VARCHAR(320),
    roles VARCHAR (2) NOT NULL,
    amount_donated MONEY NOT NULL,
    PRIMARY KEY (email),
    FOREIGN KEY (email) REFERENCES [Users](email)
        ON DELETE CASCADE
        ON UPDATE CASCADE
);
CREATE TABLE Staffs(
    email VARCHAR(320) NOT NULL,
    password_salt varbinary(max) NOT NULL,
    password_hash varbinary(max) NOT NULL,
    token VARCHAR(320),
    roles VARCHAR (2) NOT NULL,
    title VARCHAR(128) NOT NULL,
    type VARCHAR(256),
    created DATETIME,
    PRIMARY KEY (email),
    FOREIGN KEY (email) REFERENCES [Users](email)
        ON DELETE CASCADE
        ON UPDATE CASCADE
);

为了不使这个问题部分混乱,我将不把我的触发器放在这里,因为它目前不能按照我想要的方式工作,因为 Donators 属性 token 当我更新 Users 属性 token.

我的代码与这个问题有关,可以找到。此处 248-366行)。Heads up Users还有几个子类。但是,我只需要理解并找到允许的代码。tokenStaffsDonators.

sql-update sql-server-express database-trigger materialized-views
1个回答
0
投票

好的,所以一个 Ravenous Baboon的文章 是我在这里找到了问题的答案。下面是我如何保持我的物化视图的触发器。Donators 最新的。跟着再有什么传承,我想是不言而喻的。(有不明白的地方可以随时问)

CREATE TRIGGER User_Updated_Check
ON [Users]
AFTER UPDATE
AS
BEGIN
    SET NOCOUNT ON

    DECLARE @email VARCHAR(320);
    DECLARE @old_roles VARCHAR (2);
    DECLARE @new_password_hash VARBINARY(max);
    DECLARE @new_password_salt VARBINARY(max);
    DECLARE @new_token VARCHAR(320);
    DECLARE @new_f_name VARCHAR(64);
    DECLARE @new_l_name VARCHAR(64);
    DECLARE @new_roles VARCHAR (2);
    DECLARE @new_amount_donated MONEY;
    DECLARE @new_title VARCHAR(128);
    DECLARE @new_type VARCHAR(256);
    DECLARE @new_created DATETIME;


    SET @email = (SELECT email FROM Inserted);
    SET @old_roles = (SELECT roles FROM Deleted);
    SET @new_password_hash = (SELECT  password_hash FROM Inserted);
    SET @new_password_salt = (SELECT  password_salt FROM Inserted);
    SET @new_token = (SELECT token FROM Inserted);
    SET @new_f_name = (SELECT f_name FROM Inserted);
    SET @new_l_name = (SELECT l_name FROM Inserted);
    SET @new_roles = (SELECT roles FROM Inserted);
    SET @new_amount_donated = (SELECT amount_donated FROM Inserted);
    SET @new_title = (SELECT title FROM Inserted);
    SET @new_type = (SELECT type FROM Inserted);
    SET @new_created = (SELECT created FROM Inserted);



    IF @old_roles != @new_roles
        BEGIN
            THROW 51000, 'The Roles need to match and cannot change', 1;
            ROLLBACK TRANSACTION
        END

    IF @new_roles != 'd' AND
       @new_roles != 's' AND
       @new_roles != 'e' AND
       @new_roles != 'a' AND
       @new_roles != 'sd' AND
       @new_roles != 'ed' AND
       @new_roles != 'ad'
        BEGIN
            THROW 51000, 'The Role entered does not exist', 1;
            ROLLBACK TRANSACTION
        END

    -- Donators
    IF @new_roles = 'd'  OR
       @new_roles = 'sd' OR
       @new_roles = 'ed' OR
       @new_roles = 'ad'
        IF UPDATE(token)
            UPDATE Donators
            SET token = @new_token
            WHERE email = @email;
        IF Update(password_salt)
            UPDATE Donators
            SET password_salt = @new_password_salt
            WHERE email = @email;
        IF Update(password_hash)
            UPDATE Donators
            SET password_hash = @new_password_hash
            WHERE email = @email;
        IF UPDATE(f_name)
            UPDATE Donators
            SET f_name = @new_f_name
            WHERE email = @email;
        IF Update(l_name)
            UPDATE Donators
            SET l_name = @new_l_name
            WHERE email = @email;
        IF Update(amount_donated)
            UPDATE Donators
            SET amount_donated = @new_amount_donated
            WHERE email = @email;

END

© www.soinside.com 2019 - 2024. All rights reserved.