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 2005 Forums
 Transact-SQL (2005)
 Finding products code with a table

Author  Topic 

rookie_sql
Constraint Violating Yak Guru

443 Posts

Posted - 2008-05-29 : 12:05:35
Hi i've to table where i have a product code and a area code.

I want to insert the values from table 1 into table 2 if the product code and area code do not already exists in the table in table 2.

Am using this code below but it does not seem to be working.

select distinct p_code1, area_code
from table1
where not exists
(select p_code, area_code
from table 2 )

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-29 : 12:23:09
[code]Insert into table2 (product_code ,area_code)
select distinct p_code1, area_code
from table1 t1
left join table2 t2
on t2.p_code=t1.p_code1
and t2.area_code=t1.area_code
where t2.p_code is null
and t2.area_code is null[/code]

or


[code]select distinct p_code1, area_code
from table1 t1
where not exists
(
(select p_code, area_code
from table2
where p_code=t1.p_code1
and area_code=t1.area_code)[/code]
Go to Top of Page

rookie_sql
Constraint Violating Yak Guru

443 Posts

Posted - 2008-05-30 : 04:19:48
Thanks i got it to work i did not think of the where part in the query..
Go to Top of Page
   

- Advertisement -