SQL 仅在达到最大日期时更新
SQL Update Only when Max Date reached
我有两个表:
表 1
-----------------------------------------
TID1 Name Status LastStatus
-----------------------------------------
1 A 1 3
表 2
-----------------------------------------
TID2 TID1 oDate Status
-----------------------------------------
1 1 2020-04-01 1
2 1 2020-04-03 2
3 1 2020-04-05 3
情况是:如果我在 TID2 = 2
上更新 Table2
,则不应更新 Table1
上的 LastStatus
,因为 [=] 上有一个最大日期14=] 与 TID1=1
。因此 Table1
上的 LastStatus
只会在 Table2
上有最大日期的更新时才会更新。
目前,我只在 Table2
上工作。它对 Table1
没有影响。下面是我的代码:
-- Insert Statement
Declare @TID1 int, @oDate DateTime, @Status int;
Set @TID1 = 1;
Set @oDate = '2020-04-05';
Set @Status = 3;
Insert into Table2 (TID1, oDate, Status) values (@TID1, @oDate, @Status)
-- Update Statement (Example only - if there's a row to be updated)
Update Table2 Set TID1=@TID1, oDate=@oDate, Status=@Status
where TID2 = 3
有谁知道如何解决这个问题?
理想情况下,您可以将 inserts/updates 合并到一个存储过程中的两个表中,您可以在其中执行类似以下操作:
-- Insert into Table2
insert into dbo.Table2 (TIDI1, oDate, [Status])
select @TIDI1, @oDate, @Status;
-- OR
-- Update Table2
update dbo.Table2 set
TID1 = @TIDI1
, oDate = @oDate
, [Status] = @Status
where TID2 = @TID2;
-- Then update table1 if the date we just added is the latest or more recent
update dbo.Table1 set
LastStatus = @Status
where TID1 = @TIDI1
and @oDate >= (select max(oDate) from dbo.Table2 T2 where T2.TID1 = @TID1);
if @@rowcount = 0 print 'Do nothing';
嗯,我找到了我的答案。非常感谢你回答我的问题。
update ActivityPlanHistory
set ActivityPlanId = ActivityPlanId, UpdateDate = @UpdateDate,
StatusId = @StatusId, Remarks = @Remarks
where ActivityPlanHistoryId = @ActivityPlanHistoryId;
if (select max(UpdateDate) from ActivityPlanHistory where ActivityPlanId = @ActivityPlanId) <= @UpdateDate
begin
update ActivityPlan
set LastStatus = @StatusId, LastUpdateDate = @UpdateDate,
LastRemarks = @Remarks
where ActivityPlanId = @ActivityPlanId
end
else
begin
print 'do nothing';
end
我有两个表:
表 1
-----------------------------------------
TID1 Name Status LastStatus
-----------------------------------------
1 A 1 3
表 2
-----------------------------------------
TID2 TID1 oDate Status
-----------------------------------------
1 1 2020-04-01 1
2 1 2020-04-03 2
3 1 2020-04-05 3
情况是:如果我在 TID2 = 2
上更新 Table2
,则不应更新 Table1
上的 LastStatus
,因为 [=] 上有一个最大日期14=] 与 TID1=1
。因此 Table1
上的 LastStatus
只会在 Table2
上有最大日期的更新时才会更新。
目前,我只在 Table2
上工作。它对 Table1
没有影响。下面是我的代码:
-- Insert Statement
Declare @TID1 int, @oDate DateTime, @Status int;
Set @TID1 = 1;
Set @oDate = '2020-04-05';
Set @Status = 3;
Insert into Table2 (TID1, oDate, Status) values (@TID1, @oDate, @Status)
-- Update Statement (Example only - if there's a row to be updated)
Update Table2 Set TID1=@TID1, oDate=@oDate, Status=@Status
where TID2 = 3
有谁知道如何解决这个问题?
理想情况下,您可以将 inserts/updates 合并到一个存储过程中的两个表中,您可以在其中执行类似以下操作:
-- Insert into Table2
insert into dbo.Table2 (TIDI1, oDate, [Status])
select @TIDI1, @oDate, @Status;
-- OR
-- Update Table2
update dbo.Table2 set
TID1 = @TIDI1
, oDate = @oDate
, [Status] = @Status
where TID2 = @TID2;
-- Then update table1 if the date we just added is the latest or more recent
update dbo.Table1 set
LastStatus = @Status
where TID1 = @TIDI1
and @oDate >= (select max(oDate) from dbo.Table2 T2 where T2.TID1 = @TID1);
if @@rowcount = 0 print 'Do nothing';
嗯,我找到了我的答案。非常感谢你回答我的问题。
update ActivityPlanHistory
set ActivityPlanId = ActivityPlanId, UpdateDate = @UpdateDate,
StatusId = @StatusId, Remarks = @Remarks
where ActivityPlanHistoryId = @ActivityPlanHistoryId;
if (select max(UpdateDate) from ActivityPlanHistory where ActivityPlanId = @ActivityPlanId) <= @UpdateDate
begin
update ActivityPlan
set LastStatus = @StatusId, LastUpdateDate = @UpdateDate,
LastRemarks = @Remarks
where ActivityPlanId = @ActivityPlanId
end
else
begin
print 'do nothing';
end