在sql server中插入触发器之后

时间:2016-04-13 05:17:35

标签: sql-server

我们可以在sql server中的索引视图上插入之后创建

我在索引视图的sql server中创建了一个after insert插件。

CREATE TRIGGER t_STUD_FUZZY_CERTIFICATIONS_mv 
ON STUD_FUZZY_CERTIFICATIONS_mv
AFTER INSERT AS

BEGIN

DECLARE

@count_rec int,

@new_poor INT,
@new_average INT,
@new_excellent INT,
@new_stud_num VARCHAR(10), 
@new_mem_set INT, 
@new_lname VARCHAR(25), 
@new_fname VARCHAR(25), 
@new_report_date date, 
@new_total_certs INT


select @new_stud_num=stud_num, @new_mem_set=mem_set, @new_lname=lname, @new_fname=fname, @new_report_date=report_date, @new_total_certs=total_certs from inserted;


select @count_rec = count (*) from fuzzy_param_trapezoid where MEM_SET = (select mem_set from inserted);
--dbms_output.put_line(count_rec);
if(@count_rec = 0)

begin --if

--update fuzzy attributes of stud_fuzzy_orders_mv
SET @new_poor =  dbo.FIT.update_certs_poor(@new_stud_num, @new_total_certs, @new_mem_set);
SET @new_average = dbo.FIT.update_certs_average(@new_stud_num, @new_total_certs, @new_mem_set);
SET @new_excellent = dbo.FIT.update_certs_excellent(@new_stud_num, @new_total_certs, @new_mem_set);

INSERT INTO STUD_FUZZY_CERTIFICATIONS values(@new_stud_num, 
@new_mem_set,
@new_lname, 
@new_fname,
@new_report_date, 
@new_total_certs, dbo.FIT.update_certs_poor(@new_stud_num, @new_total_certs, @new_mem_set),
dbo.FIT.update_certs_average(@new_stud_num, @new_total_certs, @new_mem_set), 
dbo.FIT.update_certs_excellent(@new_stud_num, @new_total_certs, @new_mem_set));

UPDATE STUD_FUZZY_CERTIFICATIONS_mv set POOR=@new_poor, AVERAGE=@new_average, EXCELLENT=@new_excellent where stud_num=@new_stud_num and mem_set=@new_mem_set;

end;--end if

END;

GO

我收到以下错误

Msg 8197, Level 16, State 6, Procedure t_STUD_FUZZY_CERTIFICATIONS_mv, Line 1
The object 'STUD_FUZZY_CERTIFICATIONS_mv' does not exist or is invalid for this operation.

我只在正确的数据库上运行,对象STUD_FUZZY_CERTIFICATIONS_mv存在于数据库中。

1 个答案:

答案 0 :(得分:0)

我认为您正在针对View而不是table创建触发器。

您可能希望将数据插入视图中不可见的列。为此,请在视图上创建INSTEAD OF触发器以处理插入。

CREATE TRIGGER t_STUD_FUZZY_CERTIFICATIONS_mv ON STUD_FUZZY_CERTIFICATIONS_mv
INSTEAD OF INSERT 
AS
BEGIN
    DECLARE @count_rec int,@new_poor INT,@new_average INT,@new_excellent INT,@new_stud_num VARCHAR(10),@new_mem_set INT, 
            @new_lname VARCHAR(25),@new_fname VARCHAR(25),@new_report_date date,@new_total_certs INT

    SELECT @new_stud_num=stud_num, @new_mem_set=mem_set, @new_lname=lname, @new_fname=fname, @new_report_date=report_date, @new_total_certs=total_certs 
    FROM inserted;

    SELECT @count_rec = COUNT (*) FROM fuzzy_param_trapezoid WHERE MEM_SET = (SELECT mem_set FROM inserted);
    --dbms_output.put_line(count_rec);

    IF(@count_rec = 0)
    BEGIN --if
        --update fuzzy attributes of stud_fuzzy_orders_mv
        SET @new_poor =  dbo.FIT.update_certs_poor(@new_stud_num, @new_total_certs, @new_mem_set);
        SET @new_average = dbo.FIT.update_certs_average(@new_stud_num, @new_total_certs, @new_mem_set);
        SET @new_excellent = dbo.FIT.update_certs_excellent(@new_stud_num, @new_total_certs, @new_mem_set);

        INSERT INTO STUD_FUZZY_CERTIFICATIONS values(@new_stud_num,@new_mem_set,@new_lname,@new_fname,@new_report_date, 
                @new_total_certs, dbo.FIT.update_certs_poor(@new_stud_num, @new_total_certs, @new_mem_set),
                dbo.FIT.update_certs_average(@new_stud_num, @new_total_certs, @new_mem_set), 
                dbo.FIT.update_certs_excellent(@new_stud_num, @new_total_certs, @new_mem_set));

        UPDATE STUD_FUZZY_CERTIFICATIONS_mv SET POOR=@new_poor, AVERAGE=@new_average, EXCELLENT=@new_excellent 
        WHERE stud_num=@new_stud_num and mem_set=@new_mem_set;

    END;
END;
GO