Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 将数据从一个表#1复制到表#2,然后删除表#2中特定销售发票ID的其余行_Sql Server - Fatal编程技术网

Sql server 将数据从一个表#1复制到表#2,然后删除表#2中特定销售发票ID的其余行

Sql server 将数据从一个表#1复制到表#2,然后删除表#2中特定销售发票ID的其余行,sql-server,Sql Server,很抱歉,我对SQL查询没有深入的了解。我必须修改记录和维护客户分期付款的现有应用程序 为此,我有一个要求,我想复制的日期 TableNo1进入TableNo2除了New\u Amount(TableNo1)列数据,然后删除每个saleinvoiceid的TableNo2的剩余行 实际上,tableno1已经修改了分期付款计划,这就是为什么需要相应地修改TableNo2 表1 New_ID SalesInvoiceID InsttNo DueDate New_Amount

很抱歉,我对SQL查询没有深入的了解。我必须修改记录和维护客户分期付款的现有应用程序

为此,我有一个要求,我想复制的日期
TableNo1
进入
TableNo2
除了
New\u Amount
(TableNo1)列数据,然后删除每个
saleinvoiceid
TableNo2
的剩余行

实际上,
tableno1
已经修改了分期付款计划,这就是为什么需要相应地修改
TableNo2

表1

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 
表2

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
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
所需输出(表2)


如果我猜对了表之间的关系,这应该是您所需要的全部,但是在删除任何生产数据之前,您应该在表的副本上测试这一点

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
    );

你能澄清你的问题吗?是否要
插入
更新
表2中的现有行?因为这就是我所理解的。您希望使用表1中的DueDate和Amount字段更新表2(与Inst_ID匹配)。然后从TableNo2中删除TableNo1中不存在的行。输出架构标记为
TableNo1
,但架构匹配
TableNo2
。我想我们需要更多关于您尝试完成的内容的详细信息。我想只使用与SalesInvoiceID匹配的TableNo1中的DueDate更新TableNo2。然后删除TableNo2中不存在的行TableNo1@EricBrandt抱歉,我已经更新了所需的表架构名称,即TableNo2。那么您的问题是什么?如何为特定的SalesInvoiceID,例如SalesInvoiceID=30,因此它不会影响与其他SalesInvoices相关的其他数据
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