背景: 我在MS SQL数据库中有一个表,其中列出了公司和相关供应商及其有效的起始日期和日期。每个公司一次只能有一个供应商。
表结构如下;
_sRID DataAreaID Supplier ApplicableFrom ApplicableTo 1 Comp1 Supplier1 2018-06-01 (NULL) 2 Comp2 Supplier2 2018-06-01 (NULL) 3 Comp3 Supplier3 2018-06-01 (NULL)
_sRID是身份。
我的触发器就是这个;
ALTER TRIGGER [trans].[ICOSupplierIDs_AutoEnd]
ON [trans].[ICOSupplierIDs]
AFTER INSERT,UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
update base
set base.ApplicableTo = Getdate()
from trans.ICOSupplierIDs as base
join inserted as ins on ins.DataAreaID = base.DataAreaID
where base.ApplicableTo is null
and base._sRID != (select max(_sRID) from trans.ICOSupplierIDs where DataAreaID = ins.DataAreaID);
END
因此,在插入" comp1"表后,该表如下所示;
_sRID DataAreaID Supplier ApplicableFrom ApplicableTo 1 Comp1 Supplier1 2018-06-01 2018-06-07 2 Comp2 Supplier2 2018-06-01 (NULL) 3 Comp3 Supplier3 2018-06-01 (NULL) 4 Comp1 Supplierx 2018-06-07 (NULL)
有没有一种更简洁的方法来执行此操作而不是最大身份的结束查询并再次点击表格?
TIA