I have two tables with the same number of columns without primary keys (I know this is not my mistake). Now I need to delete all the rows from table A that exist in table B (they are equal, each of which contains 30 columns).
The most immediate way I thought was to make an INNER JOIN and solve my problem. But the write conditions for all columns (worry about NULL ) are not elegant (maybe my tables are not elegant either).
I want to use INTERSECT . I do not know how to do that? This is my first question:
I tried ( SQL Fiddle ):
declare @A table (value int, username varchar(20)) declare @B table (value int, username varchar(20)) insert into @A values (1, 'User 1'), (2, 'User 2'), (3, 'User 3'), (4, 'User 4') insert into @B values (2, 'User 2'), (4, 'User 4'), (5, 'User 5') DELETE @A FROM (SELECT * FROM @A INTERSECT SELECT * from @B) A
But all rows have been removed from the @A table.
This led me to the second question: why does the DELETE @A FROM @B command delete all rows from the @A table?
Nizam source share