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.
Author |
Topic |
eJay
Starting Member
4 Posts |
Posted - 2011-08-18 : 08:38:10
|
Hi All,I have the following question. I have 1 database from which financial data is extracted. Employee names, Employee IDs, hours, kilometers, etc, come to mind.This works, but now i have to add the same data from a different database which has the exact same tables.A UNION came first to my mind, and while i thought it worked i eventually found out it did not work at all. Reasons for this that in the database that had to be add, Employee names and IDs did not match. The same appeared for other things like activities on which hours could be booked. e.g.: In database 1 you have (Item_ID = 1,Item_Name = Consultancy) In database 2 you have (Item_ID = 3,Item_Name = Consultancie)So not only the IDs are different, but sometimes even the description is different, although the latter is rare.I came to the conclusion that this is just not fit for a UNION, because duplication of IDS and descriptions appears. Perhaps it is not even very doable to achieve this.So i am wondering, do any of you guys and galls know of a different approach? Thanks in advance for any readers and responders. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-18 : 09:49:48
|
are databases in same server? if yes you can use likeuse db1select * from table1union allselect * from db2.dbo.table2if they're in different server, you need to first setup a linked server and use likeuse db1select * from table1union allselect * from server2.db2.dbo.table2------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-08-18 : 10:06:56
|
I would make a "translation table" that described that "Item_ID = 3,Item_Name = Consultancie" on DB2 should "map" onto "Item_ID = 1,Item_Name = Consultancy" on DB1.It could just have two columns - DB1_Item_ID and DB2_Item_ID. You'll have to keep it up to date manually though ...Or put a new column on the table in both databases and put a GUID in it - and keep that in Sync. so you can "join" on that. |
|
|
eJay
Starting Member
4 Posts |
Posted - 2011-08-18 : 10:08:58
|
quote: Originally posted by visakh16 are databases in same server? if yes you can use likeuse db1select * from table1union allselect * from db2.dbo.table2if they're in different server, you need to first setup a linked server and use likeuse db1select * from table1union allselect * from server2.db2.dbo.table2------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Hi Visakh,Unfortunately that does not solve the problem. Item_ID and Employee Names do not always match between the 2 database. This 1 Employee will end up with 2 names and the metrics will be off. The same goes for Activities. An Activity called Consultancy or Vacation could be duplicated as well if the names and IDs do not match.Thanks for answering though.The database are in the same server by the way. |
|
|
eJay
Starting Member
4 Posts |
Posted - 2011-08-18 : 10:16:55
|
quote: Originally posted by Kristen I would make a "translation table" that described that "Item_ID = 3,Item_Name = Consultancie" on DB2 should "map" onto "Item_ID = 1,Item_Name = Consultancy" on DB1.It could just have two columns - DB1_Item_ID and DB2_Item_ID. You'll have to keep it up to date manually though ...Or put a new column on the table in both databases and put a GUID in it - and keep that in Sync. so you can "join" on that.
Hello Kristen,Thank you for your response. You're first comment is something i considered. I was hoping to avoid this, exactly for the reason you mention. I never thought about your latter comment though. That would be a better option, unfortunately I have doubts i can alter the tables since there are a apart of a financial application the customer is using. Duplicating the tables through a stored procedure or something would make it doable though.In the end i think im just dealing with a 'dirty' environment. Thanks. |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-08-18 : 10:33:18
|
Perhaps create a VIEW that presents the IDs in "sanitised" format. That could include an additional table JOIN'd 1:1 to get you the GUID that reliable synchronises the records from different databases. Or, if the number of rows is few, it could just use a CASE statement or a pseudo-table)SELECT T1.Item_ID, ...FROM DB1.dbo.MyTable AS T1UNIONSELECT [Item_ID]=X.DB1_Item_ID, ...FROM DB2.dbo.MyTable AS T2 JOIN ( SELECT [DB1_Item_ID] = 1, [DB2_Item_ID] = 3 UNION ALL SELECT 2, 6 UNION ALL SELECT 3, 7 ... ) AS X ON DB2_Item_ID = T2.Item_IDWHERE ... then the maintenance of the "synchronisation" needs to take place within the script (or wrap that all in a VIEW if you need to use it from several places) |
|
|
eJay
Starting Member
4 Posts |
Posted - 2011-08-18 : 10:50:45
|
quote: Originally posted by Kristen Perhaps create a VIEW that presents the IDs in "sanitised" format. That could include an additional table JOIN'd 1:1 to get you the GUID that reliable synchronises the records from different databases. Or, if the number of rows is few, it could just use a CASE statement or a pseudo-table)SELECT T1.Item_ID, ...FROM DB1.dbo.MyTable AS T1UNIONSELECT [Item_ID]=X.DB1_Item_ID, ...FROM DB2.dbo.MyTable AS T2 JOIN ( SELECT [DB1_Item_ID] = 1, [DB2_Item_ID] = 3 UNION ALL SELECT 2, 6 UNION ALL SELECT 3, 7 ... ) AS X ON DB2_Item_ID = T2.Item_IDWHERE ... then the maintenance of the "synchronisation" needs to take place within the script (or wrap that all in a VIEW if you need to use it from several places)
I see what you mean. If the performance won't be an issue then i think this would be a good way to go.Thanks again Kristen, you've given me some alternatives. This forum doesn't award points but i give you a 10/10. |
|
|
|
|
|
|
|