很抱歉,我对SQL查询没有深入的了解。我必须修改记录和维护客户分期付款的现有应用程序。
为了这个目的,我有一个要求,我想将TableNo1
的日期复制到TableNo2
除了New_Amount
(TableNo1)列数据,然后为每个TableNo2
删除剩余的SalesInvoiceID
行。
实际上TablNo1
修改了分期付款计划,这就是为什么需要相应地修改TableNo2
。
TableNo1
New_ID SalesInvoiceID InsttNo DueDate New_Amount
1 30 1 2019-05-02 12000
2 30 2 2019-06-02 12000
3 30 3 2019-09-02 4000
4 30 4 2019-12-02 4000
TableNo2
Instt_ID SalesInvoiceID InsttNo DueDate PaymentDate Amount Status
51 30 1 2019-05-02 NULL 0 Up-Coming
52 30 2 2019-06-02 NULL 0 Up-Coming
53 30 3 2019-07-02 NULL 0 Up-Coming
54 30 4 2019-08-02 NULL 0 Up-Coming
55 30 5 2019-09-02 NULL 0 Up-Coming
56 30 6 2019-10-02 NULL 0 Up-Coming
57 30 7 2019-11-02 NULL 0 Up-Coming
58 30 8 2019-12-02 NULL 0 Up-Coming
所需输出(TableNo2)
Instt_ID SalesInvoiceID InsttNo DueDate PaymentDate Amount Status
51 30 1 2019-05-02 NULL 0 Up-Coming
52 30 2 2019-06-02 NULL 0 Up-Coming
53 30 3 2019-09-02 NULL 0 Up-Coming
54 30 4 2019-12-02 NULL 0 Up-Coming
如果我正确地猜测表之间的关系,这应该是您所需要的,但在删除任何生产数据之前,您应该在表的副本上测试它。
UPDATE t2
SET t2.DueDate = t1.DueDate
FROM
dbo.TableNo2 AS t2
JOIN
dbo.TableNo1 AS t1
ON t1.SalesInvoiceID = t2.SalesInvoiceID
AND t1.InsttNo = t2.InsttNo;
DELETE t2
FROM
dbo.TableNo2 AS t2
WHERE
NOT EXISTS
(
SELECT 1
FROM dbo.TableNo1 AS t1
WHERE t1.SalesInvoiceID = t2.SalesInvoiceID
AND t1.InsttNo = t2.InsttNo
);
CREATE TABLE #Table1 (New_ID INT IDENTITY (1,1) PRIMARY KEY,SalesInvoiceID
INT,InsttNo INT,PaymentDate DATE,New_Amount INT )
CREATE TABLE #Table2 (New_ID INT IDENTITY (1,1) PRIMARY KEY,Instt_ID INT,
SalesInvoiceID INT,InsttNo INT,DueDate DATE,PaymentDate DATE,Amount INT,Status
VARCHAR(20))
INSERT INTO #Table1 (SalesInvoiceID,InsttNo,PaymentDate,New_Amount) VALUES (30
,1,'2019-05-02','12000')
INSERT INTO #Table1 (SalesInvoiceID,InsttNo,PaymentDate,New_Amount) VALUES (30
,2,'2019-06-02','12000')
INSERT INTO #Table1 (SalesInvoiceID,InsttNo,PaymentDate,New_Amount) VALUES (30
,3,'2019-09-02','4000')
INSERT INTO #Table1 (SalesInvoiceID,InsttNo,PaymentDate,New_Amount) VALUES (30
,4,'2019-12-02','4000')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(51,30,1,'2019-05-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(52,30,2,'2019-06-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(53,30,3,'2019-07-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(54,30,4,'2019-08-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(55,30,5,'2019-09-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(56,30,6,'2019-10-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(57,30,7,'2019-11-02',NULL,0,'Up-Coming')
INSERT INTO #Table2
(Instt_ID,SalesInvoiceID,InsttNo,DueDate,PaymentDate,Amount,Status) VALUES
(58,30,8,'2019-12-02',NULL,0,'Up-Coming')
SELECT
T2.Instt_ID
,T1.SalesInvoiceID
,T1.InsttNo
,T1.PaymentDate DueDate
,T2.PaymentDate
,T2.Amount
,T2.Status
FROM #Table1 t1
INNER JOIN #Table2 T2 ON t1.New_ID = t2.New_ID
DROP TABLE #Table1,#Table2