Ignore records that exist in two sql server tables

Table 1

ID    Department     Category
555       16           test
888       16           test
0001      16           test

      

table 2

ID  Department     Date
555    67         2015-04-28
111    58         2015-04-28
000    45         2015-04-28

      

how to create a stored procedure, if I needed to pass table1.department = 16 parameter value, it should dump all records from table1, but if id is in table 2, it should ignore this record.

expected result if i pass parameter table1.department = 16

Output

ID    DEpartment       Category
888     16               test
0001    16               test

      

id 555 should be ignored.

what was done

select *
from table1 as t1 inner join table2 as t2 on t1.ID=t2.ID
where t1.department='16'

      

+3


source to share


1 answer


This query will return the desired result,



SELECT Table1.ID, 
        Table1.Dept,
        Table1.Category 
FROM table1 WHERE Table1.ID NOT IN (SELECT Table2.ID FROM table2)

      

+2


source







All Articles