我有一个包含多列的表(dbo.membersdatatable)。我想这个触发器更新一个名为" memberstatus"的列。什么时候"会员指定"更新为" 10"来自" 9"。
的值-------下面的SQL语句---------------
IF UPDATE([MemberDesignation])
--need to make it so it will recognize when memberdesignation = 10
UPDATE dbo.MembersDataTable
SET MemStatus = '0'
FROM dbo.MembersDataTable AS mdtbl
INNER JOIN inserted AS i ON i.StudentID = mdtbl.StudentID
答案 0 :(得分:0)
使用SPARINGLY触发器。例如,我的系统有138个表,550个存储过程,只有28个触发器。我希望自己少一点。
但如果您决定需要触发器,它将如下所示:
CREATE TRIGGER [t_MemberDesignationSideEffect] ON [dbo].MembersDataTable
FOR UPDATE
AS
SET NOCOUNT ON
IF NOT ( UPDATE([MemberDesignation]) ) RETURN
-- REMINDER: All triggers can and will fire for a set of rows...not just one
UPDATE mdtbl
SET MemStatus = '0'
FROM inserted i
JOIN dbo.MembersDataTable mdtbl on i.StudentID = mdtbl.StudentID
JOIN deleted d on d.StudentID = mdtbl.StudentID
WHERE i.memberdesignation = '10'
and d.memberdesignation = '9'
GO
答案 1 :(得分:0)
您正在桌面上编写更新触发器。在这里,您可以使用已删除和插入的魔术表。如果是更新触发器:
已删除表包含UPDATE语句之前 的行。
插入的表包含行,因为它在
create trigger update_member_status
on membersdatatable
for update
as
begin
declare @old_designation int
declare @new_designation int
set @old_designation = (select memberdesignation from deleted)
set @new_designation = (select memberdesignation from inserted)
if (@old_designation = 9 and @new_designation = 10)
begin
update membersdatatable
set memstatus = '0'
from membersdatatable as mdtbl
inner join inserted as i on i.StudentID = mdtbl.StudentID
end
end
答案 2 :(得分:0)
您将使用a更新所有行的成员状态 memberdesignation = 10
您可以为此查询添加条件,以便仅使用日期字段或其他字段更新所需的条件
更新表格后,只需调用此程序
CREATE PROCEDURE USP_MemberStatusRules()
AS
UPDATE dbo.MembersDataTable
SET MemStatus = '0'
WHERE memberdesignation = 10
AND .....
AND .....
GO