Esta es una opción que puede funcionarte. Realice pruebas enviandole 342 códigos a la función de tipo tabla.
Le envías una cadena con todos tus códigos separados por una coma:
'CM004808,CM004809,XY89800,CM004808,CM004809,XY89800'
En la función proceso el texto primero creando una tabla con los códigos, después le elimino las letras y por ultimo el resultado le elimino los ceros al inicio.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Danilo Luna>
-- Create date: <30/05/2019>
-- Description: <Función para procesar codigos >
-- =============================================
alter FUNCTION dbo.[FnProcesarTexto]
(
@delimited NVARCHAR(MAX),--delimitado
@delimiter NVARCHAR(MAX)--delimitador
)
RETURNS @t TABLE (Id INT IDENTITY(1,1), Valor NVARCHAR(MAX), ValorProcesado NVARCHAR(MAX))
AS
BEGIN
DECLARE @xml XML
declare @pos int
declare @string varchar(max)
SET @xml = N'<t>' + REPLACE(@delimited,@delimiter,'</t><t>') + '</t>'
INSERT INTO @t(Valor)
SELECT r.value('.','varchar(MAX)') as item
FROM @xml.nodes('/t') as records(r) OPTION ( OPTIMIZE FOR ( @xml = null) )
declare @PRows int, @PContador int=1
set @PRows=(select max(id) from @t)
while @PContador <= @PRows
begin
set @string=(select valor from @t where Id=@PContador)
while isnumeric(@string+'e0') = 0
begin
set @pos = (select patindex('%[^0-9]%',@string))
set @string = (select replace(@string,substring(@string,@pos,1),''))
set @string= (select substring(@string, patindex('%[^0]%',@string), 10) )
update @t set Valor=@string where Id=@PContador
end
set @PContador=@PContador + 1
end
RETURN
END
GO
De esta forma envías los datos a la función:
SELECT id
,valor
FROM dbo.FnProcesarTexto('CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,',')
Y el resultado es el siguiente:

Y a la tabla de resultado le haces un join a las tablas que ya tienes para comparar.
declare @CodigosProcesados table(
Id Int,
Codigo INT
)
insert into @CodigosProcesados(Id,Codigo)
SELECT id
,valor
FROM dbo.FnProcesarTexto('CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89900,CM004808,CM004809,XY89600,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,
CM004808,CM004809,XY89800,CM004808,CM004809,XY89800,CM004808,CM004709,XY89800,CM004808,CM004809,XY89800', ',')
declare @OINV table (
DocNum int,
CardCode int,
DocDate datetime
)
insert into @OINV (DocNum,CardCode,DocDate)values(1,'4809',getdate())
insert into @OINV (DocNum,CardCode,DocDate)values(2,'89800',getdate())
select distinct(t0.DocNum)
from @OINV t0
inner join @CodigosProcesados b on t0.CardCode=b.Codigo
Resultado:

No se tardó ni un segundo en procesar la información. Aún así si te parece mi respuesta, trata de optimizar su funcionamiento, ya que siempre hay mejores formas de resolver los problemas.