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)
 How do you do this?

Author  Topic 

ajthepoolman
Constraint Violating Yak Guru

384 Posts

Posted - 2003-08-07 : 14:51:39
Hello all. I am wondering how to go about something that is probably pretty elementary.

If I have two databases and I need to pipe data into the second one, how do I structure the code so that the existing data does not get blown away?

For instance, I have a table tblPerson in both the databases. How do I pipe new data from the first database over to the second database?

Thanks!

Aj

X002548
Not Just a Number

15586 Posts

Posted - 2003-08-07 : 14:57:34
There are two ways off the top of my head...

First (and easiest) is if you have the rows with an add datetime and update datetime... based on those (and a batch window table), you can create your INSERTS and DELETES

With out those, you need to create a delta process by comparing the 2.

How much data are we talking about?



Brett

8-)

SELECT POST=NewId()
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-08-07 : 15:31:30
why not leave the table in 1 database? why have it in both?

you can access a table from another database on the same server by saying:

select * from database.owner.tablename

and you can even create a View in 1 database that links into the other, so you can just always work with that View and not have to bother with fully qualifying the database name.

- Jeff
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-08-07 : 15:43:15


Here's a sample (this is more of a batch process)

(Or you could do what Jeff said, or you could write triggers)



USE Northwind
GO
-- Create some test data
SELECT * INTO EMP1 FROM Employees
SELECT * INTO EMP2 FROM Employees
GO
-- Take a Look
SELECT * FROM EMP1
GO
-- Create some changes
DELETE FROM EMP1 WHERE EmployeeID = 1
INSERT INTO EMP1 (LastName, FirstName) SELECT 'Kaiser','Brett'
UPDATE EMP1 SET LastName = 'White' WHERE EmployeeId = 4
GO

-- Find New records

SELECT l.* FROM EMP1 l LEFT JOIN EMP2 r ON l.EmployeeId = r.EmployeeId WHERE r.EmployeeID Is NULL

-- Find deleted records

SELECT r.* FROM EMP1 l RIGHT JOIN EMP2 r ON l.EmployeeId = r.EmployeeId WHERE l.EmployeeID Is NULL

-- Find Updated Records

SELECT * FROM EMP1 l INNER JOIN EMP2 r ON l.EmployeeId = r.EmployeeId
WHERE ( l.LastName <> r.LastName
OR l.FirstName <> r.FirstName)

GO

-- Add New reocrds
INSERT INTO EMP2 (LastName, FirstName)
SELECT l.LastName, l.FirstName FROM EMP1 l LEFT JOIN EMP2 r ON l.EmployeeId = r.EmployeeId WHERE r.EmployeeID Is NULL
-- Delete missing
DELETE FROM EMP2
WHERE EmployeeID IN (
SELECT r.EmployeeID FROM EMP1 l RIGHT JOIN EMP2 r ON l.EmployeeId = r.EmployeeId WHERE l.EmployeeID Is NULL)

-- Update the changes ....update all columns for simplicity

UPDATE r
SET r.LastName = l.Lastname
, r.FirstName = l.Firstname
FROM EMP1 l INNER JOIN EMP2 r ON l.EmployeeId = r.EmployeeId
WHERE ( l.LastName <> r.LastName
OR l.FirstName <> r.FirstName)
GO
-- Take a peek

SELECT * FROM EMP1
UNION ALL
SELECT * FROM EMP2
ORDER BY EmployeeID
GO

-- Clean up this mess

DROP TABLE EMP1
DROP TABLE EMP2
GO




Brett

8-)

SELECT POST=NewId()
Go to Top of Page

glen34
Starting Member

2 Posts

Posted - 2004-01-31 : 01:30:55
Finally 3 Hours looking through code to find what I needed. Thank You
Go to Top of Page

Stoad
Freaky Yak Linguist

1983 Posts

Posted - 2004-01-31 : 14:18:44
Congrats, glen! Keep on learning!!
Go to Top of Page
   

- Advertisement -