我很惊讶这还没有得到解决但是我在stackoverflow中搜寻并且无法找到它。
基本上,我尝试在系统中启用学生帐户的大量删除,但希望将这些记录存档,以便记录仍然可用,而不再是系统的一部分。
我已经写了一个删除触发器来自动将已删除的记录存档到存档表中。
但是,我不知道如何使触发器接受处理多个记录,至少对于insert into语句。现在,我的insert into语句只会将内存中已删除表的第一条记录输入到我的归档表中。我使用的是SQL Server。
drop trigger trgDelete;
create TRIGGER trgDelete ON KPMG_URecruiting_CIS484_Group21.dbo.Student AFTER DELETE
as begin
Declare @Email varchar (50);
Declare @Password varchar(50);
Declare @FirstName varchar(50);
Declare @MiddleName varchar(50);
Declare @LastName varchar(50);
Declare @PreferredName varchar(50);
Declare @CellPhone char(20);
Declare @Notes varchar(max);
Declare @Institution varchar(100);
Declare @EducationLevel char(20);
Declare @Major varchar(100);
Declare @Minor varchar(100);
Declare @GPA float;
Declare @GradDate date;
Declare @CPA bit;
Declare @Service varchar(100);
Declare @Industry varchar(100);
Declare @Location varchar(100);
Select @Email=d.Email from deleted d;
Select @Password=d.Password from deleted d;
Select @FirstName=d.FirstName from deleted d;
Select @MiddleName=d.MiddleName from deleted d;
Select @LastName=d.LastName from deleted d;
Select @PreferredName=d.PreferredName from deleted d;
Select @CellPhone=d.CellPhone from deleted d;
Select @Notes=d.Notes from deleted d;
Select @Institution=d.Institution from deleted d;
Select @EducationLevel=d.EducationLevel from deleted d;
Select @Major=d.Major from deleted d;
Select @Minor=d.Minor from deleted d;
Select @GPA=d.GPA from deleted d;
Select @CPA=d.CPA from deleted d;
Select @GradDate=d.GradDate from deleted d;
Select @Service=d.Service from deleted d;
Select @Industry=d.Industry from deleted d;
Select @Location=d.Location from deleted d;
Insert into Student_Archive
(
Email, Password, FirstName, MiddleName, LastName, PreferredName, CellPhone, Notes, Institution, EducationLevel, Major, Minor, GPA, GradDate, CPA, Service, Industry, Location, Audit_TimeStamp
)
Values
(
@Email, @Password, @FirstName, @MiddleName, @LastName, @PreferredName, @CellPhone, @Notes, @Institution,@EducationLevel, @Major, @Minor, @GPA, @GradDate, @CPA, @Service, @Industry, @Location, getdate());
end

你能指点我如何强制触发器在内存中删除的表中执行插入到EACH记录而不仅仅是第一个吗?任何建议都非常感激。
答案 0 :(得分:0)
好吧,直接执行INSERT
:
create TRIGGER trgDelete ON KPMG_URecruiting_CIS484_Group21.dbo.Student AFTER DELETE
as
begin
Insert into Student_Archive
(
Email, Password, FirstName, MiddleName, LastName, PreferredName, CellPhone, Notes, Institution, EducationLevel, Major, Minor, GPA, GradDate, CPA, Service, Industry, Location, Audit_TimeStamp
)
SELECT Email, Password, FirstName, MiddleName, LastName, PreferredName, CellPhone, Notes, Institution, EducationLevel, Major, Minor, GPA, GradDate, CPA, Service, Industry, Location, Audit_TimeStamp, getdate()
FROM DELETED;
end
答案 1 :(得分:0)
试试这个 - 只是一个简单的,简单的基于集合的方法:
CREATE TRIGGER trgDelete
ON KPMG_URecruiting_CIS484_Group21.dbo.Student
AFTER DELETE
AS BEGIN
INSERT INTO dbo.Student_Archive (Email, Password, FirstName, MiddleName, LastName, PreferredName, CellPhone, Notes, Institution, EducationLevel, Major, Minor, GPA, GradDate, CPA, Service, Industry, Location, Audit_TimeStamp)
SELECT
Email, Password, FirstName, MiddleName, LastName, PreferredName, CellPhone, Notes, Institution, EducationLevel, Major, Minor, GPA, GradDate, CPA, Service, Industry, Location, SYSDATETIME()
FROM
Deleted
END