Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

controlla se l'id esiste in più tabelle

Potresti usare UNION (rimuove preventivamente i duplicati) o 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