473,434 Members | 1,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

Comparison of two different databases

347 100+
I have two different databases on the same server using SQL 2000. Every night database 1 is updated via an automated process. I currently have an query that will then move that data to a "static" table that I can manipulate however I need. What I'm trying to build is either a trigger or a SP that will allow me to check the differences between the newly uploaded data and the "static" data and then move whatever is new to a table called "newclients." I've looked at a lot of solutions on line but most seem to involve a long convoluted process to do this. Is there a relatively simple and easy way to write this. Currently, this is what I have:

Expand|Select|Wrap|Line Numbers
  1. create trigger newcustomers
  2. on newclients
  3. for insert as
  4. insert into msbtotal.dbo.members
  5. SELECT * FROM tcms_members.dbo.memberdata
Thanks

Doug
Aug 19 '10 #1
11 2042
NeoPa
32,556 Expert Mod 16PB
Doug,

Could you rewrite this do you think. It's hard to follow what you're trying to say. I suggest you use object names and then it will be clearer what you're referring to.
Aug 19 '10 #2
dougancil
347 100+
Neo,

Ok I'll try to write this as simply as I can.

I have 2 databases. Database A is filled every night by an application that produces the data at midnight. So every night at midnight the data in database A is overwritten. Database B is a copy of database a before it's overwritten every night. What I'd like to do is before the data in database A is overwritten every night is to compare A and B, see whats different between the two and then whatever has been edited or added is then added to table "New." Does that make more sense?
Aug 19 '10 #3
NeoPa
32,556 Expert Mod 16PB
Yes it does thank you. Not many names, but it is clearer so you get away with that ;) For now we can call the table in database A TableA and the table from database B TableB. Remembering of course that TableB holds the original data while TableA holds the updated data.

Essentially then I guess you want a table of Deltas, or effective changes, that have been made to the table during the process. Let me know if I have this wrong, but that would mean it should include all of :
  1. Additions. Simple enough after-image of the data.
  2. Amendments to existing records. This could be done as Before- & After-Images, or for less demanding situations simply an After-Image may do.
  3. Deletions. These may not be required but they would be if you would need to be in a position to reproduce the changes.

Assuming access to both tables simultaneously then, you would create a query to append (into [New]) all records from TableA where the matching record from TableB (LEFT OUTER JOIN on all fields) doesn't exist. If a different flag is required for an After-Image of an amendment as opposed to that for an addition, then join on a unique index and compare the other data in the WHERE clause.

Pretty well the same technique can be used in reverse to find deletions and Before-Images of amendments. Two relatively straightforward queries can do the whole job for you.

PS. It's not about simplicity of the explanation, but rather about the clarity. Your original post was relatively simple, but not clear.
Aug 19 '10 #4
ck9663
2,878 Expert 2GB
If you're talking with just one table, you might be able to get away with simple code that compares the two tables and spit out the difference into another table. If it's multiple tables try the more complex world of triggers. If it's almost the entire server-to-server comparison, try the more complicated REPLICATION.

Good Luck!!!

~~ CK
Aug 19 '10 #5
dougancil
347 100+
Neo ...

This is what I've come up with for now. What this doesnt call into clarity is any kind of error checking or anything like that .. and I am eventually going to create a "trigger" off of it. Just not at this time:

Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM   tcms_members.dbo.memberdata left outer join
  3.        msbtotal.dbo.members
  4.   on   tcms_members.dbo.memberdata.id = msbtotal.dbo.members.id
  5. where  msbtotal.dbo.members.id is null
Aug 19 '10 #6
NeoPa
32,556 Expert Mod 16PB
Doug,

Your posted SQL will show items that have been added, but only those items. I would have thought you might require more, but as long as you understand that's all it will do, and you're happy that's actually all you want, then you should be fine.

If you want more, but are still unsure of exactly how to go about it then let us know and I'm sure we can fill in more details for you.
Aug 19 '10 #7
dougancil
347 100+
neo,

Actually what I'd like to do is to add some sort of error checking. Right now, if there's no data in the query, SQL returns an error and I'd like the query to check for that.
Aug 20 '10 #8
dougancil
347 100+
Also, Neo, I just thought of this too ... my ID field in my table is my primary key which of course can't be overwritten. Essentially what I'll need to have happen is that daily both my databases will be overwritten, but if there is any new data in database 1 (between the last data dump and today) that data is added to database 2 but also "parsed" and the new information dumped to the newclient table.
Aug 20 '10 #9
NeoPa
32,556 Expert Mod 16PB
Doug, there seems to be a bit of a problem here. I'm trying to answer a question for you, but the question is moving the closer we get to it. It really is very important to express the whole question clearly in the first post. All relevant details should be included as they will effect the answer which is most appropriate. If you feed in details piece-meal then the thread becomes a mess and is little use to anyone else with a similar problem (as well as being quite frustrating for anyone who finds themselves answering various questions as they change after each solution).

Having a generated PK (Like AutoNumber) where the creation of the record would typically not generate the same data every time, would mean that my earlier suggested approach would not be appropriate for you. Otherwise it would be of course. I'm not clear from your recent comments whether or not this is the case, but I suspect it may be from reading between the lines.

If irreplaceable unique IDs are involved then the earlier suggested approach would need to be amended to using three separate queries instead of the two suggested earlier.
  1. DELETE any items that are no longer extant.
  2. UPDATE any items where the associated data has changed.
  3. APPEND any new items.
The design of the table to take the new data would need to be somewhat different, as specific IDs would be needed to be stored, rather than new IDs generated automatically on APPEND (as would be the case with an AutoNumber ID).

PS. While I'm happy to give further help where possible, I cannot always guarantee to be able to help if you decide not to follow the guidelines I lay down. Sometimes I can, but other times I may either not have much experience in a solution of that sort, or it may simply be a poor approach that I'm not willing to help anyone with. This is related more to indicating, by my involvement, that it is a valid approach (for other readers of the thread) than it is about any sense of imposing my views on the OP. Ultimately, only you shouild decide how to approach your project.
Aug 23 '10 #10
dougancil
347 100+
Neo,

Sorry for the confusion, but this project has been a moving target for me as well. The person who "tasked" me with this project uttered those infamous words to me .."It's simple all you have to do is ...." and of course after spending time with the concept, I realized that they were missing a lot of what I consider checks and balances and error checking, so I apologize for my last minute edition to the issue. Where I stand today is as follows:

I have taken off the primary key on the ID fields, for now. MY query needs to overwrite the data in database 2 anytime a new "data dump" is done in database 1 (in our case it's every day at midnight) and then whatever is new is also put into a table called NewClients.

Here is what I have so far:

Expand|Select|Wrap|Line Numbers
  1. (SELECT * FROM tcms_members.dbo.memberdata left outer join
  2. msbtotal.dbo.members on tcms_members.dbo.memberdata.id = msbtotal.dbo.members.id where msbtotal.dbo.members.id is null)
  3. insert into msbtotal.dbo.newclients
Aug 24 '10 #11
NeoPa
32,556 Expert Mod 16PB
Doug,

If I'm honest I'm still struggling to understand what you're asking. I feel my earlier response (post #4) answers the fundamental question given your latest comment about ignoring PKs for now at least.

Your latest post includes some SQL, but doesn't really ask a question with it. Are you saying that isn't working (Error messages generally help in such a situation)? Or are you asking what else needs doing on top of this (indicating you're happy with your current SQL)?

I would expect your current SQL to look like the following, but I'm often surprised to find T-SQL has other ways of doing things than I'm aware of :
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO msbtotal.dbo.newclients
  2. SELECT      *
  3. FROM        tcms_members.dbo.memberdata AS tMD
  4.             LEFT OUTER JOIN
  5.             msbtotal.dbo.members AS tM
  6.   ON        tMD.ID = tM.ID
  7. WHERE       tM.ID IS NULL
Aug 25 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: UgoWeb | last post by:
Is it possible to execute a query between tables on different databases? thanx in advance
3
by: Henri | last post by:
Hello, I am quite new to ms-sql and I have a problem : I want to create an SQL request which would copy serveral records from a table in a given database to another table (with exactly the same...
2
by: Kenneth Fosse | last post by:
Hi. I'm currently working on a project which involves the creation of a web page which reports selected data to customers from two back-end systems. I use ASP script language on IIS, the server...
3
by: Dov MORYUSEF | last post by:
Hi there, Is it possible to have a primary key - foreign key relationship between tables in different databases in DB2 ? (databases are installed in different physical servers) Thanks a lot...
2
by: Bill Moran | last post by:
Boy, I'm just full of questions today ... Is there a way to provide different persistent settings for DateStyle for different databases within the same cluster? Simply put, I've got two...
0
by: deepaks85 | last post by:
I need to create a CMS in which we will create user and each user should connected with the different airlines databases and that user will be able to upload files and view files in PDF and print...
2
by: =?Utf-8?B?VGVycnk=?= | last post by:
Is it possible to join tables from different databases? If so, what is the format of the select statement? Both databases are on the same server. TIA, -- Terry
18
by: asi242 | last post by:
Hello, Good day to all... I just want to ask.. How would you connect two tables from 2 different databases using PHP scripts? Because, one database that contains customer table is dedicated to...
3
by: Aswanth | last post by:
Hi.., I'm having Two Stored Procedures in Two Different Databases.. Database1 - Employee Stored Procedure - Employee_Details Database2 - Company Stored Procedure - Company_Details
4
by: sganeshsvk | last post by:
sir, i want to store the same data values in two different databases at that same time in mysql using php programming. suppose any one databases data will lose then we use the other...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.