SQL Server触发将数据更新到表中,但仅当客户插入或更新一个客户机时才应更新数据

SQL Server触发将数据更新到表中,但仅当客户插入或更新一个客户机时才应更新数据,sql,triggers,sql-server-2008-r2,Sql,Triggers,Sql Server 2008 R2,现在触发器看起来是这样的,它部分工作 我的扳机是这样的 ALTER trigger [dbo].[tr_EligebilityCheck] on [dbo].[Clients] for INSERT,update as BEGIN UPDATE Clients SET StatusID = 5 WHERE ClientID IN (Select ClientID from Clients c join inserted --A

现在触发器看起来是这样的,它部分工作

我的扳机是这样的

   ALTER  trigger [dbo].[tr_EligebilityCheck]
on [dbo].[Clients]
 for INSERT,update
as 

BEGIN
UPDATE Clients 
SET 
StatusID = 5
WHERE 
ClientID IN (Select ClientID
            from Clients c 
            join inserted --ADD THIS JOIN
            on inserted.ClientID = c.ClientID
            join IncomeEligibility i 
            on c.HshldSize = i.HshldSize
            where StatusID in (1,2) 
            and  WIC = 0
            and (c.CategCode = 'SR' 
                and ((MonthlyYearly = 'month' and c.AnnualHshldIncome >= i.SeniorMo) 
                or (c.AnnualHshldIncome >= i.SeniorYr and MonthlyYearly ='year'))
                and DATEDIFF(YEAR,DOB,GETDATE()) < 60)
            or
                (c.CategCode = 'CH' 
                and ((MonthlyYearly = 'month' and c.AnnualHshldIncome >= i.WomanChildMo) 
    or (c.AnnualHshldIncome >= i.WomanChildYr and MonthlyYearly ='year'))
                and DATEDIFF(YEAR,DOB,GETDATE()) > 6))

update Clients
set StatusID = 4 
where WIC =1
from Clients --ADD THIS FROM STATEMENT
join inserted --ADD THIS JOIN
on inserted.ClientID = Clients.ClientID

END

如果我用CategCode='CH'插入客户机,它检查收入,但不检查DOB。

只要您对主键有正确的引用,我不明白为什么您需要
而不是
触发器。似乎没有任何情况会阻止插入或更新,对吗?您只需要确保StatusID的值。没有理由必须在更新之前完成

我认为更新太多行的原因是没有将触发器限制为仅插入
表中的那些行。尝试向触发器添加联接,如下所示:

ALTER  trigger [dbo].[tr_EligebilityCheck]
on [dbo].[Clients]
 for INSERT,update
as 

BEGIN
UPDATE Clients 
SET 
StatusID = 5
WHERE 
ClientID IN (Select ClientID
            from Clients c 
            join inserted --ADD THIS JOIN
            on inserted.ClientID = c.ClientID
            join IncomeEligibility i 
            on c.HshldSize = i.HshldSize
            where StatusID in (1,2) 
            and  WIC = 0
            and (c.CategCode = 'SR' 
                and ((MonthlyYearly = 'month' and c.AnnualHshldIncome >= i.SeniorMo) 
                or (c.AnnualHshldIncome >= i.SeniorYr and MonthlyYearly ='year'))
                and DATEDIFF(YEAR,DOB,GETDATE()) < 60)
            or
                (c.CategCode = 'CH' 
                and ((MonthlyYearly = 'month' and c.AnnualHshldIncome >= i.WomanChildMo) 
    or (c.AnnualHshldIncome >= i.WomanChildYr and MonthlyYearly ='year'))
                and DATEDIFF(YEAR,DOB,GETDATE()) > 6))

update Clients
set StatusID = 4 
where WIC =1
from Clients --ADD THIS FROM STATEMENT
join inserted --ADD THIS JOIN
on inserted.ClientID = Clients.ClientID

END

插入表?问题是我不知道如何编写,我可以在插入或插入后编写,但不知道如何在插入时而不是插入时编写。也许可以从更简单的开始。。。另外,你能解释一下为什么你认为这应该是一个触发器而不是触发器吗?最后一次更新的目的是什么?是否要更新表中的每一行?我想我需要它
而不是
,因为如果它只是用于插入,更新它会更改数据库中无法更改的现有数据。例如,申请该计划的人对家庭有一个要求,他在该计划中,他们的要求发生了变化,但他仍然应该在该计划中。如果我使用
进行插入,更新
会将他的状态设置为不合格。正因为如此,我需要在将客户机数据插入表之前检查它,以便它不会更改数据库中的exist记录。据我所知,
而不是INSERT
,它的功能与
Oracle
中使用的
更新之前的
相同。我尝试只更新要插入或更新的记录。但不要修改表中的任何现有数据。第一个变量可以工作,但它只检查DOB howewer Skipping
和((MonthlyEarly='month'和c.AnnualHshldIncome>=i.SeniorMo)或(c.AnnualHshldIncome>=i.SeniorYr和MonthlyEarly='year'))
部分,我不明白第二个部分的原因和这个部分不工作(年份,出生日期,GETDATE())>6)您是否认为有任何问题无法解决?所以,与您之前遇到的问题相同。我对此不确定,这就是为什么我没有尝试修改您的
where
子句。如果没有一些样本数据来练习,很难说。您的
where
子句中肯定有问题,但我不确定它在哪里。不过,我的第一个猜测是,它与括号的排列有关。
ALTER  trigger [dbo].[tr_EligebilityCheck]
on [dbo].[Clients]
 for INSERT,update
as 

BEGIN
UPDATE Clients 
SET 
StatusID = 5
WHERE 
ClientID IN (Select ClientID
            from Clients c 
            join inserted --ADD THIS JOIN
            on inserted.ClientID = c.ClientID
            join IncomeEligibility i 
            on c.HshldSize = i.HshldSize
            where StatusID in (1,2) 
            and  WIC = 0
            and (c.CategCode = 'SR' 
                and ((MonthlyYearly = 'month' and c.AnnualHshldIncome >= i.SeniorMo) 
                or (c.AnnualHshldIncome >= i.SeniorYr and MonthlyYearly ='year'))
                and DATEDIFF(YEAR,DOB,GETDATE()) < 60)
            or
                (c.CategCode = 'CH' 
                and ((MonthlyYearly = 'month' and c.AnnualHshldIncome >= i.WomanChildMo) 
    or (c.AnnualHshldIncome >= i.WomanChildYr and MonthlyYearly ='year'))
                and DATEDIFF(YEAR,DOB,GETDATE()) > 6))

update Clients
set StatusID = 4 
where WIC =1
from Clients --ADD THIS FROM STATEMENT
join inserted --ADD THIS JOIN
on inserted.ClientID = Clients.ClientID

END
ALTER  trigger [dbo].[tr_EligebilityCheck]
on [dbo].[Clients]
INSTEAD OF INSERT
as 

BEGIN

--First, set StatusID in the inserted table
UPDATE inserted
SET 
StatusID = 5
WHERE 
ClientID IN (Select ClientID
            from Clients c 
            join IncomeEligibility i 
            on c.HshldSize = i.HshldSize
            where StatusID in (1,2) 
            and  WIC = 0
            and (c.CategCode = 'SR' 
                and ((MonthlyYearly = 'month' and c.AnnualHshldIncome >= i.SeniorMo) 
                or (c.AnnualHshldIncome >= i.SeniorYr and MonthlyYearly ='year'))
                and DATEDIFF(YEAR,DOB,GETDATE()) < 60)
            or
                (c.CategCode = 'CH' 
                and ((MonthlyYearly = 'month' and c.AnnualHshldIncome >= i.WomanChildMo) 
    or (c.AnnualHshldIncome >= i.WomanChildYr and MonthlyYearly ='year'))
                and DATEDIFF(YEAR,DOB,GETDATE()) > 6));

update inserted
set StatusID = 4 
where WIC =1;

--Once the inserted table looks right, proceed with the insert
--You need to explicitly write an insert statement, or nothing will happen
INSERT INTO [dbo].[Clients]
  <column_list>
SELECT <column_list>
FROM inserted;