Sqlserver
 sql >> Baza danych >  >> RDS >> Sqlserver

T-SQL przy użyciu Cross-Apply z instrukcją Delete

Nie widzę tu żadnych korzyści w stosowaniu cross. Oto proste rozwiązanie, które spełnia swoje zadanie:

declare @t table(recordid int)
declare @tableone table(recordid int)
declare @tabletwo table(recordid int)
declare @tablethree table(recordid int)
insert @t values(101),(102),(103),(104),(105),(106)

insert @tableone values(101),(102),(103),(104)
insert @tablethree values(101),(102)

delete t
from @t t
where not exists (select 1 from @tableone where t.recordid = recordid)
and exists (select 1 from @tableone)
or not exists (select 1 from @tabletwo where t.recordid = recordid)
and exists (select 1 from @tabletwo)
or not exists (select 1 from @tablethree where t.recordid = recordid)
and exists (select 1 from @tablethree)

Wynik:

recordid
101
102


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Zapytania FREETEXT w SQL Server 2008 nie są dopasowywane do fraz

  2. SQL Server — dynamiczna tabela przestawna — wstrzyknięcie SQL

  3. Przekazywanie oddzielonego ciągu do procedury składowanej w celu przeszukania bazy danych

  4. Wstaw dane z db do innego db

  5. SQL Server 2008:INSERT, jeśli nie wychodzi, zachowaj unikalną kolumnę