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
 Other Forums
 MS Access
 How to add records only if not already present?

Author  Topic 

LeBaron
Starting Member

3 Posts

Posted - 2004-06-14 : 05:19:23
Hi

I have a table1 that contains all goods we are selling.
In a second table2 ar all the goods we are selling trough distriburors.

Now I would like to add records marked as sold by distriburors into table2, but only if the record is not already in table2

How could this be achieved.

Thank you

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2004-06-14 : 09:48:24
Do a select to check if the record already exists first...
Go to Top of Page

LeBaron
Starting Member

3 Posts

Posted - 2004-06-14 : 16:04:19
This is exactly where I am stuck, I don't know how to do it.

pseudo code

select * from Table1
for each record in table1
if part of it is in table2
update part of it in table2
else insert part of it in table2

if part of it isin table3
update part of it in table3
else insert part of it in table3

same for two more tables



Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-06-14 : 23:43:52
[code]
UPDATE T2
SET MyCol1 = T1.MyCol1,
MyCol2 = T1.MyCol2
FROM MyTable1 T1
JOIN MyTable2 T2
ON T2.MyPKCol = T1.MyPkCol

INSERT INTO Table2 (MyCol1, MyCol2)
SELECT MyCol1, MyCol2
FROM Table1 T1
WHERE NOT EXISTS(SELECT * FROM Table2 T2 WHERE T2.MyPKCol = T1.MyPKCol)
[/code]
Any good?

Kristen
Go to Top of Page

LeBaron
Starting Member

3 Posts

Posted - 2004-06-15 : 02:30:37
Thank you. I am trying it today when I am back from work.

Go to Top of Page
   

- Advertisement -