How to implement a central subscriber model replication? - sql-server

I have a problem please help me.
suppose I have three databases Db1,Db2,DbCenter and tbl_country exists in all of databases.
tbl_country in Db1 has following records:
tbl_country
Id Name
1 US
2 Germany
tbl_country in Db2 has following records:
tbl_country
Id Name
1 Australia
2 Italy
and the merged records of tbl_country in Db1 and Db2 must be merged in DbCenter,
so tbl_country in DbCenter has following entries:
tbl_country
Id Name
1 Us
2 Germany
3 Australia
4 Italy
the "id" column in all of tables is primary key and identity.
What should I do to the records of two Db1 and Db2 be added in the last of tbl_country of DbCenter.
I'm using transactional replication. set Db1 and Db2 as publishers and specify DbCenter as subscriber and in article properties of Db2 set the action of "Action if name in use" to "Keep existing object unchanged" but it delete the records in DbCenter and substitute them with new ones when the records in "id" column are the same.
Db1 and Db2 are Sql server 2000 version and Db3 is Sql server 2008R2.

Maintain the original key values in the DbCenter database, along with metadata that specifies which database it came from. Then generate a surrogate key that will be used as the PK. That way you both have unique references for each Country records and a way to link back to the source information.
For example:-
ID_SK SOURCE_ID SOURCE_DB NAME
1 1 Db1 US
2 2 Db1 Germany
3 1 Db2 Australia
4 2 Db2 Italy

If Db1 and Db2 have overlapping primary key values, I don't think there is any way to do this. If you have control over the schema, and are able to change it, you might want to change the primary keys to the uniqueidentifier type (instead of int) which is guaranteed to be globally unique.

Related

Need SQL Query to compare the same table name should have the view created in a different database

We have a central database, and there were other databases with have year naming convention. We move the tables periodically to another database. After moving the table, we create a view with the same name in the source database.
For Example
Have Database name: ABC (which has the table’s daily load)
Have another database name: ABC2023Q1 (The tables we move from ABC to this database, and we create a view in ABC table with the same table name)
Is it the right approach to check with information_schema.tables and information_schema. Views with left join? Please let me know if you have any other approach.

SSIS designer Visual Studio foreign keys integration

I need to integrate two similar databases into third DB3. DB3 is almost the same as DB1.
First database DB1:
Addresses table with: primary key AddressId
People table with: primary key PersonId , foreign key AddressId
Second database DB2:
It is pretty similar, but in other language
Data from DB1 to DB3 flows smoothly, table after table. For example I have 1000 records in DB3 table named Addresses from DB1 and 1000 records in table named People from DB1.
Let's suppose Person with number 30 in DB3 (after transfering from DB1) has the IdAddress number 20.
Address with number 40 in DB2 has the ID number 1040 in DB3 and the Person has ID number 30 in DB2 and 1030 in DB3.
While transferring table People from B2 to B3 we need to know the address ID is not 40 but 1040.
I'm trying to use lookup to find existing record, but I'm newbie in SSIS VS designer. Could you help me? How can I resolve this problem?
Suggestions
You can do this using Lookup Transformation component as you mentioned, but first you have to:
Select the basic information of each table that can distinguish each logical entity. Example if talking about Persons you can choose the Fullname+ Mothername + Date Of Birth , ...
After selecting this attributes you have to add a Lookup Transformation
Map thiese columns between Source and Lookup table
Select the ID column (from Lookup table) as Output and rename the column to be NewID
Select Ignore failure option to handle non matches situation
After doing these steps, if the same person was inserted previously you will get the ID in NewID column, else NewID is NULL
Additional Information
Microsoft Docs - Lookup Transformation
UNDERSTAND SSIS LOOKUP TRANSFORMATION WITH AN EXAMPLE STEP BY STEP
SSIS Lookup Transformation
Lookup Transformation in SSIS

Transfer data from one database to another database with different schema

i have problem to Transfer data from one sqlserver 2008 r2 to another sql server 2012 databases with different schema, here is some different scenario,
database 1
database 1 with tables Firm and Client, these both have FirmId and ClientId primary key as int datatype,
FirmId is int datatype as reference key used in Client table.
database 2
database 2 with same tables Firm and Client, these both have FirmId and ClientId but primary key as uniqueidentifier,
FirmId is uniqueidentifier datatype as reference key used in Client table.
problem
the problem is not to copy data from 1 database table to 2 database table, but the problem is to maintain the reference key's Firm table into Client table. because there is datatype change.
i am using sql server 2008 r2 and sql server 2012
please help me to resolve / find the solution, i really appreciate your valuable time and effort. thanks
I'll take a stab at it even if I am far from an expert on SQLServer - here is a general procedure (you will have to repeat it for all tables where you have to replace INT with UID, of course...).
I will use Table A to refer to the parent (Firm, if I understand your example clearly) and Table B to refer to the child (Client, I believe).
Delete the relations pointing to Table A
Remove the identity from the id column of Table A
Create a new column with Uniqueidentifier on Table A
Generate values for the Uniqueidentifier column
Add the new Uniqueidentifier column in all the child tables (Table B)
Use the OLD id column to map your child record & update the new Uniqueidentifier value from your parent table.
Drop all the id columns
Recreate the relations
Having said that, I just want to add a warning to you: converting to UID is, according to some, a very bad idea. But if you really need to do that, you can script (and test) the above mentioned procedure.

SQL Server Best Practices Table Linking

I am new to SQL server and am wanting to make sure I am using best practices. What I am doing is creating 7 tables.
(Transaction,Customer,Business,Vehicle,Seller,Lien,Mailto)
Transaction is my main table where it creates a TransactionID. Then in the other 6 tables I will also have a TransactionID column so I can link them all together.
In the other 6 tables they each have there own ID as well.
For example
(CustomerID, BusinessID, VehicleID, SellerID, LienID, MailtoID)
My question is in my transaction table do I have to list all of those IDs or does having just the TransactionID allow them all to connect.
Transaction Table 1 Example
ID
Type
DateTime
Transaction Table 2 Example
ID
Type
CustomerID
BusinessID
VehicleID
MailtoID
SellerID
LienID
DateTime
(For the transaction ID I am wanting it to be created and then automatically fill the same in for the other tables as those fields are submitted using foreign keys I believe)
Any help on this would be greatly appreciated!!
do I have to list all of those IDs- NO!.
having just the "TransactionID" allow them all to connect.

How do I do a cross-DB MERGE INTO into an identical table while maintaining an identity key?

I have two tables on two different databases, DB1.Category and DB2.Category.
I need to merge all values so that DB1.Category and DB2.Category are identical, but I need to maintain the PK ID, CategoryID.
The CategoryID is an identity column with an increment and seed of 1 in DB1, but no identity in DB2.
Is there a way to sync all data in these tables from DB1 to DB2 while maintaining the PK?
This is what I have so far:
MERGE DB1.dbo.Category AS TARGET
USING DB2.dbo.Category AS SOURCE
ON (TARGET.MarketplaceName = SOURCE.MarketplaceName
AND TARGET.MarketplaceCategoryCode = SOURCE.MarketplaceCategoryCode
AND TARGET.MarketplaceCategoryName = SOURCE.MarketplaceCategoryName)
WHEN NOT MATCHED BY TARGET
INSERT(--*FIELDS*-
)
I am a little confused by your question. The Title says "Identical table" but the body of your question you point out difference. Can you provide the structure of the 2 tables?

Resources