SELECT
  CN.CMSCreditNoteID,
  'Xero' AS AccountBookType,
  CN.AccountBookID,
  XE.XeroEntityName AS AccountBookName,
  CN.CreditNoteDate,
  CN.CreditNoteAccount,
  CN.TaxType AS XeroTaxAccount,
  CN.InvoiceNumber,
  CN.CreditNoteNumber,
  CN.ExternalCreditNoteID,
  CN.ActionDateTime,
  CN.Approver,
  CN.Updated,
  CN.Allocated,
  CN.CreditNoteAmount
FROM
  taCreditNote AS CN
    LEFT JOIN taXeroEntity AS XE ON (XE.XeroEntityID = CN.AccountBookID)
WHERE
  (CN.Updated = 0) AND
  ((CN.CreditNoteNumber IS NULL) OR (CN.CreditNoteNumber <> 'VOID'))
  @pAccountBookID
  @pFilter
ORDER BY
  CN.AccountBookID,
  CN.CMSCreditNoteID