I do not think your approach is the best or fast ...
Here are some ways to compare them:
Use this for tests.
create table test ( id int not null primary key);
insert into test VALUES(100),(200),(505766),(300),(400),(500),(458073),(600),(700),(464050),(800),(900),(1000)
GO
Here is your list of identifiers
declare @idsxml as xml = '<root><Id>505766</Id><Id>458073</Id><Id>460689</Id><Id>464050</Id></root>'
- . XML
- .data() - - ...
SELECT test.id
FROM test
WHERE @idsXml.exist('/root/Id[data(.)=sql:column("id")]') = 1;
- , XQuery
SELECT test.id
FROM test
WHERE @idsXml.exist('/root/Id[text()=sql:column("id")]') = 1;
- ... , ...
SELECT test.id
FROM test
WHERE @idsXml.exist('/root[Id=sql:column("id")]') = 1;
- INNER JOIN
WITH DerivedTable AS
(
SELECT i.value('.','int') AS id
FROM @idsxml.nodes('root/Id') AS A(i)
)
SELECT test.id
FROM test
INNER JOIN DerivedTable AS dt ON test.id=dt.id;
- ( in memory )
DECLARE @tbl TABLE(id INT NOT NULL PRIMARY KEY)
INSERT INTO @tbl
SELECT i.value('.','int') AS id
FROM @idsxml.nodes('root/Id') AS A(i);
SELECT test.id
FROM test
INNER JOIN @tbl AS tbl ON test.id=tbl.id;
GO
DROP TABLE test;