I have two tables with the following data. I need rows
where i = 4, 5, 6 and 7. Rows 1, 2, 3 should be eliminated
as they are available in both the tables. Can this be done
within a select statement using subqueries or do I need to
use cursor.
Filtering criteria should be based on columns i, fname,
lname.
Basically, elminate all records
where tab1.i = tab2.i and tab1.fname = tab2.fname and
tab1.lname = tab2.lname. Is there anything like a minus
operator.
i fname lname another_column
1 Fname1 Lname1 asdfj
2 Fname2 Lname2 NULL
3 Fname3 Lname3 lsadfj
4 Fname4 Lname4 NULL
5 Fname5 Lname5 NULL
i fname lname another_column
1 Fname1 Lname1 NULL
2 Fname2 Lname2 NULL
3 Fname3 Lname3 NULL
6 Fname6 Lname6 NULL
7 Fname7 Lname7 NULL
You can use the sample script to create the tables as
temporary tables.
set nocount on
create table #tmp1 (
i int,
fname char(20),
lname char(20),
another_column char(20) null
)
create table #tmp2 (
i int,
fname char(20),
lname char(20),
another_column char(20) null
)
insert into #tmp1
select 1, 'Fname1', 'Lname1', 'asdfj'
insert into #tmp1
select 2, 'Fname2', 'Lname2', null
insert into #tmp1
select 3, 'Fname3', 'Lname3', 'lsadfj'
insert into #tmp1
select 4, 'Fname4', 'Lname4', null
insert into #tmp1
select 5, 'Fname5', 'Lname5', null
insert into #tmp2
select 1, 'Fname1', 'Lname1', null
insert into #tmp2
select 2, 'Fname2', 'Lname2', null
insert into #tmp2
select 3, 'Fname3', 'Lname3', null
insert into #tmp2
select 6, 'Fname6', 'Lname6', null
insert into #tmp2
select 7, 'Fname7', 'Lname7', null
select * from #tmp1
select * from #tmp2
drop table #tmp1
drop table #tmp2
set nocount off
Please try to help me.
Thanks.