更新语句错误地应用

时间:2015-01-29 17:46:28

标签: sql-server database tsql sql-server-2000

我正在尝试在SQL Server 2000 sp4数据库上更新缺少值的45000行。

我将尝试模拟下面的表设置和值条件: 我有两张桌子。一个是持有有效的交易而另一个在某些行中缺少值。

- 表#Trans保存有效交易

Create Table #trans (
    [DocumentNumber] [char](21) NOT NULL,
    [CustomerName] [char](21) NOT NULL,
    [CustomerID] [char](31) NOT NULL,
    [ACTINDX] [int] NOT NULL,
    [CRDTAMNT] [numeric](19, 5) NOT NULL,
    [DEBITAMT] [numeric](19, 5) NOT NULL,
    [TRXSORCE] [char](30) NOT NULL,
    [TRXDATE] [datetime] NOT NULL
    )

INSERT INTO #trans  ([DocumentNumber],[CustomerName],[CustomerID],[ACTINDX],[CRDTAMNT],[DEBITAMT],[TRXSORCE],[TRXDATE])
Values('INV20123','Andrew Sesinyi','A0001',2501,25620.00,0.000,'SALESTRN15012015','15-Jan-2015')
INSERT INTO #trans  ([DocumentNumber],[CustomerName],[CustomerID],[ACTINDX],[CRDTAMNT],[DEBITAMT],[TRXSORCE],[TRXDATE])
Values('INV20123','Andrew Sesinyi','A0001',2201,0.000,25620.00,'SALESTRN15012015','15-Jan-2015')
INSERT INTO #trans ([DocumentNumber],[CustomerName],[CustomerID],[ACTINDX],[CRDTAMNT],[DEBITAMT],[TRXSORCE],[TRXDATE])
Values('PMTRN00155','Bame Moonwa','B0001',1700,1550.00,0.0000,'PYMNTTRN17012015','17-Jan-2015')
INSERT INTO #trans ([DocumentNumber],[CustomerName],[CustomerID],[ACTINDX],[CRDTAMNT],[DEBITAMT],[TRXSORCE],[TRXDATE])
Values('PMTRN00155','Bame Moonwa','B0001',1900,0.0000,1550.00,'PYMNTTRN17012015','17-Jan-2015')
INSERT INTO #trans ([DocumentNumber],[CustomerName],[CustomerID],[ACTINDX],[CRDTAMNT],[DEBITAMT],[TRXSORCE],[TRXDATE])
Values('PMTRN00156','OLERATO PHAMA','OL0001',1900,0.0000,1020.00,'PYMNTTRN17012015','17-Jan-2015')
INSERT INTO #trans ([DocumentNumber],[CustomerName],[CustomerID],[ACTINDX],[CRDTAMNT],[DEBITAMT],[TRXSORCE],[TRXDATE])
Values('PMTRN00156','OLERATO PHAMA','OL0001',1700,1020.00,0.0000,'PYMNTTRN17012015','17-Jan-2015')
INSERT INTO #trans  ([DocumentNumber],[CustomerName],[CustomerID],[ACTINDX],[CRDTAMNT],[DEBITAMT],[TRXSORCE],[TRXDATE])
Values('INV20124','Bame Moonwa','B0001',2501,18000.00,0.000,'SALESTRN15012015','15-Jan-2015')
INSERT INTO #trans  ([DocumentNumber],[CustomerName],[CustomerID],[ACTINDX],[CRDTAMNT],[DEBITAMT],[TRXSORCE],[TRXDATE])
Values('INV20124','Bame Moonwa','B0001',2201,0.000,18000.00,'SALESTRN15012015','15-Jan-2015')

- 表#GL包含一些缺少值的事务,即--DocumentNumber,CustomerID A和CustomerName

Create Table #GL(
    [DocumentNumber] [char](21) ,
    [CustomerName] [char](21),
    [CustomerID] [char](31),
    [ACTINDX] [int] NOT NULL,
    [CRDTAMNT] [numeric](19, 5) NOT NULL,
    [DEBITAMT] [numeric](19, 5) NOT NULL,
    [ORTRXSORCE] [char](30) NOT NULL,
    [TRXDATE] [datetime] NOT NULL
    )
INSERT INTO #GL  ([DocumentNumber],[CustomerName],[CustomerID],[ACTINDX],[CRDTAMNT],[DEBITAMT],[ORTRXSORCE],[TRXDATE])
Values('','Andrew Sesinyi','A0001',2501,25620.00,0.000,'SALESTRN15012015','15-Jan-2015')
INSERT INTO #GL ([DocumentNumber],[CustomerName],[CustomerID],[ACTINDX],[CRDTAMNT],[DEBITAMT],[ORTRXSORCE],[TRXDATE])
Values('','Andrew Sesinyi','A0001',2201,0.000,25620.00,'SALESTRN15012015','15-Jan-2015')
INSERT INTO #GL ([DocumentNumber],[CustomerName],[CustomerID],[ACTINDX],[CRDTAMNT],[DEBITAMT],[ORTRXSORCE],[TRXDATE])
Values('','Bame Moonwa','B0001',1700,1550.00,0.0000,'PYMNTTRN17012015','17-Jan-2015')
INSERT INTO #GL ([DocumentNumber],[CustomerName],[CustomerID],[ACTINDX],[CRDTAMNT],[DEBITAMT],[ORTRXSORCE],[TRXDATE])
Values('','Bame Moonwa','B0001',1900,0.0000,1550.00,'PYMNTTRN17012015','17-Jan-2015')
INSERT INTO #GL ([DocumentNumber],[CustomerName],[CustomerID],[ACTINDX],[CRDTAMNT],[DEBITAMT],[ORTRXSORCE],[TRXDATE])
Values('','','',1900,0.0000,1020.00,'PYMNTTRN17012015','17-Jan-2015')
INSERT INTO #GL ([DocumentNumber],[CustomerName],[CustomerID],[ACTINDX],[CRDTAMNT],[DEBITAMT],[ORTRXSORCE],[TRXDATE])
Values('','','',1700,1020.00,0.0000,'PYMNTTRN17012015','17-Jan-2015')
INSERT INTO #GL  ([DocumentNumber],[CustomerName],[CustomerID],[ACTINDX],[CRDTAMNT],[DEBITAMT],[ORTRXSORCE],[TRXDATE])
Values('INV20124','','',2501,18000.00,0.000,'SALESTRN15012015','15-Jan-2015')
INSERT INTO #GL  ([DocumentNumber],[CustomerName],[CustomerID],[ACTINDX],[CRDTAMNT],[DEBITAMT],[ORTRXSORCE],[TRXDATE])
Values('INV20124','','',2201,0.000,18000.00,'SALESTRN15012015','15-Jan-2015')

当我运行以下更新语句时,会向#GL记录呈现不正确的更新。在这种情况下是否有替代方法或更好的方法来应用更新记录。

UPDATE #GL
SET DocumentNumber = TR.DocumentNumber
    , CustomerName = TR.CustomerName
    ,CustomerID =TR.CustomerID
FROM #GL GL
INNER JOIN #trans TR ON GL.ORTRXSORCE = TR.TRXSORCE
WHERE GL.ACTINDX = TR.ACTINDX
    AND GL.DEBITAMT = TR.DEBITAMT
    OR GL.CRDTAMNT = TR.CRDTAMNT
    AND GL.TRXDATE = TR.TRXDATE

N.B我正在尝试使用此方法更新缺少值的45000条记录。 N.B多个交易可以批量发布到#GL,因此共享TRXSORCE

非常感谢您提前了解。

1 个答案:

答案 0 :(得分:0)

我打赌你的意思

WHERE GL.ACTINDX = TR.ACTINDX
  AND (GL.DEBITAMT = TR.DEBITAMT OR GL.CRDTAMNT = TR.CRDTAMNT)
  AND GL.TRXDATE = TR.TRXDATE