Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

verifique se o id existe em várias tabelas


Você pode usar UNION (remove as duplicatas antecipadamente) ou UNION ALL :
SELECT CASE WHEN EXISTS 
  ( SELECT 1 FROM ( SELECT m_id FROM  A 
                    UNION 
                    SELECT m_id FROM  B 
                    UNION 
                    SELECT m_id FROM  C 
                    UNION 
                    SELECT m_id FROM  D 
                    UNION 
                    SELECT m_id FROM  E ) All 
    WHERE  All.m_id = 'some_id') 
THEN 1 ELSE 0 END AS ContainsID