Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 join

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2011-11-16 : 09:46:32
Hi,
Here are two separate sql queries.
They get the records which are available in one table but not in the other...


1)
insert into tblMain
select ...
from
@tbl1 left join @tbl2 on @tbl1.field1 = @tbl2.field1
and @tbl1.field2 = @tbl2.field2
where
@tbl2.field1 IS NULL
AND @tbl2.field2 IS NULL

2)
insert into tblMain
select...
from
@tbl2 left join @tbl1 on @tbl1.field1 = @tbl2.field1
and @tbl1.field2 = @tbl2.field2
where
@tbl1.field1 IS NULL
AND @tbl1.field2 IS NULL



Question:
How can the above two queries be joined into one sql?

Thanks

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-11-16 : 10:22:48
[code]
insert into tblMain
select ...
from
@tbl1 left join @tbl2 on @tbl1.field1 = @tbl2.field1
and @tbl1.field2 = @tbl2.field2
where
@tbl2.field1 IS NULL
AND @tbl2.field2 IS NULL

UNION ALL

select...
from
@tbl2 left join @tbl1 on @tbl1.field1 = @tbl2.field1
and @tbl1.field2 = @tbl2.field2
where
@tbl1.field1 IS NULL
AND @tbl1.field2 IS NULL

[/code]



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-16 : 10:28:16
[code]
insert into tblMain
select...
from
@tbl2 full outer join @tbl1 on @tbl1.field1 = @tbl2.field1
and @tbl1.field2 = @tbl2.field2
where
((@tbl1.field1 IS NULL
AND @tbl1.field2 IS NULL) OR (@tbl2.field1 IS NULL
AND @tbl2.field2 IS NULL))
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2011-11-16 : 10:46:06
Thanks
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-11-16 : 10:50:39
quote:
Originally posted by visakh16


insert into tblMain
select...
from
@tbl2 full outer join @tbl1 on @tbl1.field1 = @tbl2.field1
and @tbl1.field2 = @tbl2.field2
where
((@tbl1.field1 IS NULL
AND @tbl1.field2 IS NULL) OR (@tbl2.field1 IS NULL
AND @tbl2.field2 IS NULL))


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/




Now the passage ... is interesting isn't it?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-16 : 12:16:12
quote:
Originally posted by webfred

quote:
Originally posted by visakh16


insert into tblMain
select...
from
@tbl2 full outer join @tbl1 on @tbl1.field1 = @tbl2.field1
and @tbl1.field2 = @tbl2.field2
where
((@tbl1.field1 IS NULL
AND @tbl1.field2 IS NULL) OR (@tbl2.field1 IS NULL
AND @tbl2.field2 IS NULL))


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/




Now the passage ... is interesting isn't it?


No, you're never too old to Yak'n'Roll if you're too young to die.


Sorry I lost you

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -