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 2000 Forums
 SQL Server Development (2000)
 A Challenging Query...

Author  Topic 

Callam.Fletcher
Starting Member

3 Posts

Posted - 2006-05-14 : 05:33:15
Hello everyone!

What i am trying to accomplish is this: I have two tables; one of them full of records, the other empty. I need a query that will let me add a record to the empty table for each record in the full table.

I just don't know how to do it!

Thanks for any help!! :) - Cal

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-05-14 : 05:34:18
[code]insert into empty_table(col1, col2, col3, . . .)
select cola, colb, colc, . . .
from full_table f
where not exists (select * from empty_table x where x.pk = f.pk)[/code]


KH

Go to Top of Page

Callam.Fletcher
Starting Member

3 Posts

Posted - 2006-05-14 : 05:37:18
wow that was quick! ok i'll try that! :D thanks
Go to Top of Page

Callam.Fletcher
Starting Member

3 Posts

Posted - 2006-05-14 : 05:47:40
quote:


where not exists (select * from empty_table x where x.pk = f.pk)




what does this line do exactly?
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-05-14 : 06:35:19
it will only insert the records which are not there in the empty_table and exists in the full_table..

if empty_table has no data then you can remove this where cluase..

If Debugging is the process of removing Bugs then i Guess programming should be process of Adding them.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-05-14 : 09:13:02
Just to add on to what chiragkhabaria has stated. The where not exists will ensure that when you run the script the 2nd time you will not get any error as it will only insert any new records in full_table that is not exist in empty_table


KH

Go to Top of Page
   

- Advertisement -