UPDATE VS4TGLTrans SET TGLTRefNo=(SELECT TIvoNo FROM VS4TARInvoice WHERE VS4TGLTrans.TGLTNo=VS4TARInvoice.TIvoNo),
TGLTRefDate=(SELECT TIvoDate FROM VS4TARInvoice WHERE VS4TGLTrans.TGLTNo=VS4TARInvoice.TIvoNo),
TGLTRefNo2=(SELECT TIvoNo FROM VS4TARInvoice WHERE VS4TGLTrans.TGLTNo=VS4TARInvoice.TIvoNo),
TGLTRefDate2=(SELECT TIvoDate FROM VS4TARInvoice WHERE VS4TGLTrans.TGLTNo=VS4TARInvoice.TIvoNo)
WHERE TGLTInputSource='IV'
GO

UPDATE VS4TGLTrans SET TGLTRefNo=(SELECT TDBNNo FROM VS4TARDebitNote WHERE VS4TGLTrans.TGLTNo=VS4TARDebitNote.TDBNNo),
TGLTRefDate=(SELECT TDBNDate FROM VS4TARDebitNote WHERE VS4TGLTrans.TGLTNo=VS4TARDebitNote.TDBNNo),
TGLTRefNo2=(SELECT TDBNNo FROM VS4TARDebitNote WHERE VS4TGLTrans.TGLTNo=VS4TARDebitNote.TDBNNo),
TGLTRefDate2=(SELECT TDBNDate FROM VS4TARDebitNote WHERE VS4TGLTrans.TGLTNo=VS4TARDebitNote.TDBNNo)
WHERE TGLTInputSource='DB' 
GO

UPDATE VS4TGLTrans SET TGLTRefNo=(SELECT TRCNNo FROM VS4TARCreditNote WHERE VS4TGLTrans.TGLTNo=VS4TARCreditNote.TRCNNo),
TGLTRefDate=(SELECT TRCNDate FROM VS4TARCreditNote WHERE VS4TGLTrans.TGLTNo=VS4TARCreditNote.TRCNNo),
TGLTRefNo2=(SELECT TRCNNo FROM VS4TARCreditNote WHERE VS4TGLTrans.TGLTNo=VS4TARCreditNote.TRCNNo),
TGLTRefDate2=(SELECT TRCNDate FROM VS4TARCreditNote WHERE VS4TGLTrans.TGLTNo=VS4TARCreditNote.TRCNNo)
WHERE TGLTInputSource='CN' 
GO