BEGIN TRAN
DECLARE @XMLDocument XML
SET @XMLDocument='<Root>
<Row><A>文涛</A><B>0</B><C>3123</C><D>2017-06-09 09:42:59.000</D></Row>
<Row><A>云芬</A><B>0</B><C>23432</C><D>2017-06-09 09:52:53.233</D></Row>
<Row><A>祥君</A><B>0</B><C>432432</C><D>2017-06-09 09:52:53.593</D></Row>
</Root>'
SELECT T.c.value('(A[1])', 'varchar(20)') AS Name,T.c.value('(B[1])','int') AS Cert_Type,T.c.value('(C[1])','varchar(30)') AS Cert_No, T.c.value('(D[1])','datetime') AS Time_stamp INTO #temp
FROM @XMLDocument.nodes('/Root/Row') AS T ( c )
--ROLLBACK TRAN
COMMIT TRAN
SELECT a.Cert_Type,* FROM #temp A FULL JOIN CustomerCredit.Compliance.Request B ON a.Cert_No=b.IdentityNo AND b.Source='VBS'
WHERE B.IdentityNo IS NULL
SELECT id FROM (
SELECT ROW_NUMBER() OVER( PARTITION BY c.IdentityNo ORDER BY C.mintime ASC) num ,id FROM ( SELECT ABS(DATEDIFF(SECOND, a.Time_stamp,b.CreateTime)) AS mintime ,B.ID,B.IdentityNo
FROM #temp A JOIN CustomerCredit.Compliance.Request B ON a.Cert_No=b.IdentityNo AND b.Source='VBS' ) C ) D WHERE D.num=1