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
 General SQL Server Forums
 New to SQL Server Programming
 Insert data from another DB

Author  Topic 

murva
Starting Member

3 Posts

Posted - 2011-01-08 : 05:24:58
Hello,

I am working on a project, and I need to load data from one DB to another one, based on specific conditions. Basically I need to verify if there are new students added on the Students1 database and, if so, I will insert part of data to table Students2.
The first thing that crossed my mind was to create a stored procedure that will go over each student on Students1 and verify if same entry is found on Students2. If not, another store procedure is called and perform the insert. Well, I am not sure this is the best approach, I will be very happy of you could advice me or send me tutorials that could help.Thanks a lot!!

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-01-08 : 09:59:54
Youre thinking isn't set based.
Don't think you have to check and do something with one row at a time.

An approach could be (with the given info a better help isn't possible...):

insert second_db..students(col1, col2, ...)
select col1, col2, ... from first_db..students as s1
where not exists(select * from second_db..students as s2 where s2.studentId = s1.studentId)


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-01-08 : 13:07:08
make sure if dbs are not in same server you add a linked server before hand to query between both tables

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

Go to Top of Page
   

- Advertisement -