Feb 11, 2009

selecting two different table values in multiple column unmatching in sqlserver

create a temp table to view unmatched fields from first table

create table #zip(
City varchar(100)collate Latin1_General_CI_AS,
State varchar(100)collate Latin1_General_CI_AS,
County varchar(100)collate Latin1_General_CI_AS)


declare @City varchar(100)
declare @State varchar(100)
declare @County varchar(100)

declare @City1 varchar(100)
-- declare @State1 varchar(100)
-- declare @County1 varchar(100)
DECLARE
c3 CURSOR FOR
select distinct City,
State,
County from T_Companies
OPEN c3
FETCH next FROM c3 INTO @City,@State,@County
WHILE @@FETCH_status=0
BEGIN
SET @City1=''
--insert into #zip
select @City1='1'
from T_Zipcodes where City=@City
and State=@State and County=@County
IF @City1 !='1'
BEGIN

insert into #zip
SELECT @City,@State,@County
END


FETCH next FROM c3 INTO @City,@State,@County
END
CLOSE c3
DEALLOCATE c3

select * from #zip

drop table #zip