我有2张桌子。一个是销售表(header
),另一个是销售线(lines
)。
我想要做的是找出销售表或销售线中的任何记录是否被修改。如果它们被修改,我想将整个销售订单插入另一个表中。我在数据库ModifiedDateTime
中使用了一列(销售行中的更改不会影响salestable中的修改日期,反之亦然)。
例如 - 销售表:
SalesID DeliveryAddress ModifiedDatetime
1 Street1 2017-04-20 2:00:00
2 Street3 2017-04-19 3:00:00
销售热线
SalesID ItemId Qty ModifiedDateTime
1 100 2 2017-04-20 3:00:00
1 101 3 2017-04-20 2:00:00
2 100 5 2017-04-19 1:00:00
所以我有一个存储过程,它从这些表中选择记录并将它们插入到其他表中。它使用修改的日期时间来了解哪些记录已被修改。我想要做的是将整个订单及其行插入到集成表中。
我现在所做的是
Insert into IntegrationSalesTable (salesid, deliveryaddress, modifieddatetime)
select salesid, deliveryaddress, modifieddatetime
from salestable
where modifieddatetime > 2017-04-20 2:30:00
对于销售线
Insert into IntegrationSalesLine(salesid, itemid, qty, modifieddatetime)
select salesid, itemid, qty, modifieddatetime
from salestable
where modifieddatetime > 2017-04-20 2:30:00
现在,我得IntegrationSalesTable
为空 - 而IntegrationSalesLine
有这一行:
SalesID ItemId Qty ModifiedDateTime
1 100 2 2017-04-20 3:00:00
我想要实现的目标:
IntegrationSalesTable:
SalesID DeliveryAddress ModifiedDatetime
1 Street1 2017-04-20 2:00:00
IntegrationSalesLine:
SalesID ItemId Qty ModifiedDateTime
1 100 2 2017-04-20 3:00:00
1 101 3 2017-04-20 2:00:00
我知道我需要在行和表中选择所有已修改的销售订单,然后使用该数据运行查询。但我无法想办法。有什么建议我怎样才能达到这个结果?
答案 0 :(得分:0)
这应该做你需要的。它会选择SalesID
的所有条目,salesline
中的日期与salestable
中的日期不匹配
use testdb;
-- This stores all the SalesID's where something changed
DECLARE @modsalesids TABLE
(
SalesID INT
)
-- Select SalesID's where dates are different
insert into @modsalesids(SalesID)
select st.SalesID from salestable st
join salesline sl on st.SalesID=sl.SalesID
where
-- use '<>' if you want to include _all_ changed dates, or > when you want only changes where the item list changed after the sale
sl.ModifiedDateTime <> st.ModifiedDateTime
-- Remove duplicate SalesID's, make sure to keep the ';'
;
WITH CTE AS
(
SELECT *,ROW_NUMBER() OVER (PARTITION BY SalesID ORDER BY SalesID) AS RN
FROM @modsalesids
)
DELETE FROM CTE WHERE RN<>1
-- Select records for the modified salesIds
insert into integrationsalesline(SalesID, ItemId, Qty, ModifiedDateTime)
select SalesID, ItemId, Qty, ModifiedDateTime from salesline where SalesID in (SELECT SalesID from @modsalesids);
insert into integrationsalestable(SalesID, DeliveryAddress, ModifiedDateTime)
select SalesID, DeliveryAddress, ModifiedDateTime from salestable where SalesID in (SELECT SalesID from @modsalesids);
-- Debug stuff
select * from @modsalesids;
select * from integrationsalesline;
select * from integrationsalestable;
如果您感到好奇,这里是删除重复项的部分的source。 这就是我设置测试用例的方法:
-- create testdb;
--create table salestable (
-- SalesID INT,
-- DeliveryAddress VARCHAR(200),
-- ModifiedDateTime VARCHAR(200)
--)
--create table salesline (
-- SalesID INT,
-- ItemId INT,
-- Qty INT,
-- ModifiedDateTime VARCHAR(200)
--)
--create table integrationsalestable (
-- SalesID INT,
-- DeliveryAddress VARCHAR(200),
-- ModifiedDateTime VARCHAR(200)
--)
--create table integrationsalesline (
-- SalesID INT,
-- ItemId INT,
-- Qty INT,
-- ModifiedDateTime VARCHAR(200)
--)
--insert INTO salestable values(1,'Street1','2017-04-20 2:00:00')
--insert INTO salestable values(2,'Street3','2017-04-20 3:00:00')
--insert INTO salesline values(1, 100, 2, '2017-04-20 3:00:00')
--insert INTO salesline values(1, 107, 7, '2017-04-20 3:00:00')
--insert INTO salesline values(1, 101, 3, '2017-04-20 2:00:00')
--insert INTO salesline values(2, 100, 5, '2017-04-20 1:00:00')