Showing posts with label key. Show all posts
Showing posts with label key. Show all posts

Wednesday, March 28, 2012

merge replication without rowguid

hi,
I have a database that has Primary Key constraints on every table. Yet
when I run the replication wizard and try to configure merge
replication the wizard notifies me that it will create a rowguid on
the tables even though they already have a primary key.
anybody know how to avoid the rowguid column from being created?
regards,
Doron
what is the big deal with having rowguid column?
I am running the replication for 3 years with that column without any
prolem!
"DoronA" <doronal@.msn.com> wrote in message
news:OMPe$RgwEHA.824@.TK2MSFTNGP11.phx.gbl...
> hi,
> I have a database that has Primary Key constraints on every table. Yet
> when I run the replication wizard and try to configure merge
> replication the wizard notifies me that it will create a rowguid on
> the tables even though they already have a primary key.
> anybody know how to avoid the rowguid column from being created?
> regards,
> Doron
>
|||If your primary key is a unqueidentifier, then you can mark it as the
RowGuid column using ALTER TABLE, e.g.
ALTER TABLE dbo.MyTable
ALTER COLUMN MyPK ADD ROWGUIDCOL
Otherwise, you have to have a separate RowGuid column, as it's how
replication tracks the global identity of rows.
Paul
"DoronA" <doronal@.msn.com> wrote in message
news:OMPe$RgwEHA.824@.TK2MSFTNGP11t .phx.gbl...
> hi,
> I have a database that has Primary Key constraints on every table. Yet
> when I run the replication wizard and try to configure merge
> replication the wizard notifies me that it will create a rowguid on
> the tables even though they already have a primary key.
> anybody know how to avoid the rowguid column from being created?
> regards,
> Doron
>

merge replication without rowguid

hi,
I have a database that has Primary Key constraints on every table. Yet
when I run the replication wizard and try to configure merge
replication the wizard notifies me that it will create a rowguid on
the tables even though they already have a primary key.
anybody know how to avoid the rowguid column from being created?

regards,
Dorondoron_almog@.msn.com (Doron) wrote in message news:<995517bc.0411031605.30cf6318@.posting.google.com>...
> hi,
> I have a database that has Primary Key constraints on every table. Yet
> when I run the replication wizard and try to configure merge
> replication the wizard notifies me that it will create a rowguid on
> the tables even though they already have a primary key.
> anybody know how to avoid the rowguid column from being created?
> regards,
> Doron

Unless all of the replicas are read-only, how would you prevent the
same PK being allocated to two different rows in two different
replicas?

The solution you are being offered is to have rowguid be the primary
key - what's your proposed solution to the problem?

Merge Replication with identity keys

Hi ,
I am new in replication area . I have created a merge replication . The
tables which is present in source table has identity key, after creating
replication , I am able to add duplicate values in source as well as
destinations table having same identity values .
Secondly , When new table having identity values are added in article e.g
test1 ,Current Identity seed of test1 in different in destination database
.. I want to synchronise the data in 2 database . ie. when I am inserting
values in Test 1 table on DB1, identity seed should be taken correct seed
automatically and vice versa for destination table . Most of our tables are
containing identity keys , and all reports are based on that key .
Also ,pls let me know is there any helpful articles which simulatiing same
kind of problem.
Pls help me in this regard.
Regards,
Swati
Swati,
when you're ading the table, have a look at the table properties (this is an
elipsis button next to the table). There is a tab for identities. You can
select automatic range management here - ideally select a big enough range
so that there will not be any need to get another. This will ensure no
overlaps.
Rgds,
Paul Ibison SQL Server MVP, www.replicationanswers.com
(recommended sql server 2000 replication book:
http://www.nwsu.com/0974973602p.html)

Merge replication with foreign key constraints

Let's say I have a database with three tables. One for customers, and two
for the invoices. Customers have cust_id field, wich is 'surogate' key, it
contains the org_id where the customer was created. That table should be in
merge replication because I need to be able to add data and make changes on
all locations in the replication system.
The invoices are referenced between them self with foreign key (you can't
insert invoice_id in the detail table if the invoice_id doesn't exsist in
master table), and the invoices table is referenced with customer table with
foreign key, so you can't add cust_id to invoices wich doesn't exsist in
customers table).
The DDL is like this:
CREATE TABLE [dbo].[customers] (
[cust_id] [char] (5) COLLATE Croatian_CI_AS NOT NULL ,
[cust_name] [char] (50) COLLATE Croatian_CI_AS NOT NULL ,
[cust_address] [char] (150) COLLATE Croatian_CI_AS NOT NULL ,
[rowguid] uniqueidentifier ROWGUIDCOL NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[invoice_details] (
[invoice_id] [char] (5) COLLATE Croatian_CI_AS NOT NULL ,
[item_id] [int] NOT NULL ,
[item_description] [varchar] (250) COLLATE Croatian_CI_AS NOT NULL ,
[item_price] [decimal](18, 11) NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[invoices] (
[invoice_id] [char] (5) COLLATE Croatian_CI_AS NOT NULL ,
[cust_id] [char] (5) COLLATE Croatian_CI_AS NOT NULL ,
[invoice_date] [datetime] NOT NULL ,
[remark] [varchar] (250) COLLATE Croatian_CI_AS NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[customers] WITH NOCHECK ADD
CONSTRAINT [PK_customers] PRIMARY KEY CLUSTERED
(
[cust_id]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[invoice_details] WITH NOCHECK ADD
CONSTRAINT [PK_invoice_details] PRIMARY KEY CLUSTERED
(
[invoice_id],
[item_id]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[invoices] WITH NOCHECK ADD
CONSTRAINT [PK_invoices] PRIMARY KEY CLUSTERED
(
[invoice_id]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[customers] ADD
CONSTRAINT [DF__customers__rowgu__49C3F6B7] DEFAULT (newid()) FOR
[rowguid]
GO
CREATE UNIQUE INDEX [index_357576312] ON [dbo].[customers]([rowguid]) ON
[PRIMARY]
GO
ALTER TABLE [dbo].[invoice_details] ADD
CONSTRAINT [FK_invoice_details_invoices] FOREIGN KEY
(
[invoice_id]
) REFERENCES [dbo].[invoices] (
[invoice_id]
) NOT FOR REPLICATION
GO
ALTER TABLE [dbo].[invoices] ADD
CONSTRAINT [FK_invoices_customers] FOREIGN KEY
(
[cust_id]
) REFERENCES [dbo].[customers] (
[cust_id]
) NOT FOR REPLICATION
GO
For the purpose I'm creating two publications. One for the customers, with
no filtering, because I need to have all the subscribers share the same
data. The other one is for the invoices, but those are filtered within
invoice_id, because I don't want one subscriber to have data that doesn't
belong to it.
So, I create the first merge publication, and at the end SQL server gives me
this:
This publication contains references to foreign keys outside the
publication. The following tables are outside the publication, but contain
foreign keys that are referenced from inside the publication:
invoices (references 'customers')
To add tables to the publication, select the publication in the Create and
Manager Publications dialog box, and then click Properties & Subscriptions.
Why do I need to add invoices table to the publication?
The other one, when creating publication for the invoices gives me more
headache:
This publication contains references to primary keys outside the
publication.
The following tables are outside the publication, but contain primary keys
that are referenced from inside the publication:
-- customers (referencing table is 'invoices')
Although you can change existing data in the referencing tables, you will
not be able to add rows to those tables. If you want to add rows to the
referencing tables, include the referenced tables as articles in the
publication.
To add tables to the publication, select the publication in the Create and
Manager Publications dialog box, and then click Properties & Subscriptions.
Again, why do I need to have customers table in the same publication with
invocies' tables? Since I have several publications for the invoices, each
filtering for one particular subscriber, if I add customers to the
publication, I need to add it for every publication I create. This seems
like a waste of resources. Isn't it easier to have just one publication for
the customers?
This is, of course, just a small example derived from the real world
situation. I have several 'primary key' tables (customers, articles, users,
delivery_rates, tax_rates, organizational departments, stocks,
blaha-blaha-blaha), and several dozens of 'foreign key referencing' tables
(invocies, stock documents, bills, ...). And, somehow, putting ALL those
tables within the same publication seems a bit messy. I prefer having
similair groups of tables together (for instance, publication for stock
documents has 22 articles, but, the 'primary key tables' that those tables
reference to are in it's own seperate publications).
Am I doing something wrong with my design?
Any help much appreciated!
Mike
"I can do it quick. I can do it cheap. I can do it well. Pick any two."
Mario Splivalo
msplival@.jagor.srce.hr
The reason you would need to include all articles related by fk pk
constraints is that you might add an row to a child table which you are
replicating on the publisher where the row exists on the parent table. Then
this child row travels to the subscriber where the parent row does not
exist, and when the constraint is enforced, the transaction is rolled back
on the subscriber and publisher.
Similarly you might delete a parent record on the subscriber, and then when
it hits the publisher it might want to delete all child records belonging to
that parent row if you are not enforcing the constraint for replication, and
if you have cascading deletes and updates.
Under some circumstances you can ignore this warning.
Hilary Cotter
Looking for a SQL Server replication book?
Now available for purchase at:
http://www.nwsu.com/0974973602.html
"Mario Splivalo" <majk@.fly.srk.fer.hr> wrote in message
news:slrncqtm2o.pol.majk@.fly.srk.fer.hr...
> Let's say I have a database with three tables. One for customers, and two
> for the invoices. Customers have cust_id field, wich is 'surogate' key, it
> contains the org_id where the customer was created. That table should be
> in
> merge replication because I need to be able to add data and make changes
> on
> all locations in the replication system.
> The invoices are referenced between them self with foreign key (you can't
> insert invoice_id in the detail table if the invoice_id doesn't exsist in
> master table), and the invoices table is referenced with customer table
> with
> foreign key, so you can't add cust_id to invoices wich doesn't exsist in
> customers table).
> The DDL is like this:
> CREATE TABLE [dbo].[customers] (
> [cust_id] [char] (5) COLLATE Croatian_CI_AS NOT NULL ,
> [cust_name] [char] (50) COLLATE Croatian_CI_AS NOT NULL ,
> [cust_address] [char] (150) COLLATE Croatian_CI_AS NOT NULL ,
> [rowguid] uniqueidentifier ROWGUIDCOL NOT NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[invoice_details] (
> [invoice_id] [char] (5) COLLATE Croatian_CI_AS NOT NULL ,
> [item_id] [int] NOT NULL ,
> [item_description] [varchar] (250) COLLATE Croatian_CI_AS NOT NULL ,
> [item_price] [decimal](18, 11) NOT NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[invoices] (
> [invoice_id] [char] (5) COLLATE Croatian_CI_AS NOT NULL ,
> [cust_id] [char] (5) COLLATE Croatian_CI_AS NOT NULL ,
> [invoice_date] [datetime] NOT NULL ,
> [remark] [varchar] (250) COLLATE Croatian_CI_AS NOT NULL
> ) ON [PRIMARY]
> GO
> ALTER TABLE [dbo].[customers] WITH NOCHECK ADD
> CONSTRAINT [PK_customers] PRIMARY KEY CLUSTERED
> (
> [cust_id]
> ) ON [PRIMARY]
> GO
> ALTER TABLE [dbo].[invoice_details] WITH NOCHECK ADD
> CONSTRAINT [PK_invoice_details] PRIMARY KEY CLUSTERED
> (
> [invoice_id],
> [item_id]
> ) ON [PRIMARY]
> GO
> ALTER TABLE [dbo].[invoices] WITH NOCHECK ADD
> CONSTRAINT [PK_invoices] PRIMARY KEY CLUSTERED
> (
> [invoice_id]
> ) ON [PRIMARY]
> GO
> ALTER TABLE [dbo].[customers] ADD
> CONSTRAINT [DF__customers__rowgu__49C3F6B7] DEFAULT (newid()) FOR
> [rowguid]
> GO
> CREATE UNIQUE INDEX [index_357576312] ON [dbo].[customers]([rowguid]) ON
> [PRIMARY]
> GO
> ALTER TABLE [dbo].[invoice_details] ADD
> CONSTRAINT [FK_invoice_details_invoices] FOREIGN KEY
> (
> [invoice_id]
> ) REFERENCES [dbo].[invoices] (
> [invoice_id]
> ) NOT FOR REPLICATION
> GO
> ALTER TABLE [dbo].[invoices] ADD
> CONSTRAINT [FK_invoices_customers] FOREIGN KEY
> (
> [cust_id]
> ) REFERENCES [dbo].[customers] (
> [cust_id]
> ) NOT FOR REPLICATION
> GO
>
> For the purpose I'm creating two publications. One for the customers, with
> no filtering, because I need to have all the subscribers share the same
> data. The other one is for the invoices, but those are filtered within
> invoice_id, because I don't want one subscriber to have data that doesn't
> belong to it.
>
> So, I create the first merge publication, and at the end SQL server gives
> me
> this:
> This publication contains references to foreign keys outside the
> publication. The following tables are outside the publication, but
> contain
> foreign keys that are referenced from inside the publication:
> invoices (references 'customers')
> To add tables to the publication, select the publication in the Create
> and
> Manager Publications dialog box, and then click Properties &
> Subscriptions.
> Why do I need to add invoices table to the publication?
> The other one, when creating publication for the invoices gives me more
> headache:
> This publication contains references to primary keys outside the
> publication.
> The following tables are outside the publication, but contain primary
> keys
> that are referenced from inside the publication:
> -- customers (referencing table is 'invoices')
> Although you can change existing data in the referencing tables, you
> will
> not be able to add rows to those tables. If you want to add rows to the
> referencing tables, include the referenced tables as articles in the
> publication.
> To add tables to the publication, select the publication in the Create
> and
> Manager Publications dialog box, and then click Properties &
> Subscriptions.
> Again, why do I need to have customers table in the same publication with
> invocies' tables? Since I have several publications for the invoices, each
> filtering for one particular subscriber, if I add customers to the
> publication, I need to add it for every publication I create. This seems
> like a waste of resources. Isn't it easier to have just one publication
> for
> the customers?
> This is, of course, just a small example derived from the real world
> situation. I have several 'primary key' tables (customers, articles,
> users,
> delivery_rates, tax_rates, organizational departments, stocks,
> blaha-blaha-blaha), and several dozens of 'foreign key referencing' tables
> (invocies, stock documents, bills, ...). And, somehow, putting ALL those
> tables within the same publication seems a bit messy. I prefer having
> similair groups of tables together (for instance, publication for stock
> documents has 22 articles, but, the 'primary key tables' that those tables
> reference to are in it's own seperate publications).
> Am I doing something wrong with my design?
> Any help much appreciated!
> Mike
> --
> "I can do it quick. I can do it cheap. I can do it well. Pick any two."
> Mario Splivalo
> msplival@.jagor.srce.hr
|||On 2004-12-02, Hilary Cotter <hilary.cotter@.gmail.com> wrote:
> The reason you would need to include all articles related by fk pk
> constraints is that you might add an row to a child table which you are
> replicating on the publisher where the row exists on the parent table. Then
> this child row travels to the subscriber where the parent row does not
> exist, and when the constraint is enforced, the transaction is rolled back
> on the subscriber and publisher.
Yes, I'm aware of that. But, both tables do exsits on both publisher and
subscriber. The application run at subscriber can't violate constraint, so
can't application on the publisher. Constraints are created with NOT FOR
REPLICATION, so if replication job first inserts child table - it will work.

> Similarly you might delete a parent record on the subscriber, and then when
> it hits the publisher it might want to delete all child records belonging to
> that parent row if you are not enforcing the constraint for replication, and
> if you have cascading deletes and updates.
Yes, the same thing.

> Under some circumstances you can ignore this warning.
Thank you for your response. I recreated publisher/subscriber situation in
'lab', and it seems to be working fine (i just ignored the warning). I'm
able to insert data into foreign key table (as long as I have primary key
table with up-to-date data), replication works fine, everything is ok.
But, now I have another issue. Since the number of subscribers is going to
be rather high, some 200-300 subscribers (all MSDEs on laptops) i wanted to
script the subscriptions. In snapshot options for the particular publication
i choose the snapshot agent to DROP and recreate tables on the subscriber,
referential integrity included. But, publication wizzard tells me it can't
create foreign key constraints because the reffered tables are not in that
publication. Wich I understand, because, if I first push the invoices
publication to the subscriber, there is customers table missing, so, realy
no constraints to that tabe could be created.
Is there workaround for this? I need to have initial snapshots to delete all
the data on the remote side. But, putting all the tables to one publications
seems like a LOT of mess here. In my example, if I have 50 subscriptions I
should publish the customers table 50 times, instead of just once.
So, my question is again, am I doing something wrong when designing the
replication?
Mike
"I can do it quick. I can do it cheap. I can do it well. Pick any two."
Mario Splivalo
msplival@.jagor.srce.hr

Monday, March 26, 2012

Merge replication UNIQUEIDENTIFIER Column?

Hi all,
I am using SQL 2005 sp1 to setup Merge replication for PDA access.
i created my Primary Key column a GUID using NEWID() as the default value.
When the snapshot was created it still went and added anothe GUID to all my
tables called rowguid.
i thought it was suposed to use the existing Unique GUID column before
creating a new one?
thanks for you advice. i hope i have the corect groups here?
inkquote:
If a published table does not have a uniqueidentifier column with the
ROWGUIDCOL property and a unique index, replication adds one
So, check for the rowguidcol property...
MC
"iKiLL" <iKill@.NotMyEmail.com> wrote in message
news:uS1$gVbWHHA.4796@.TK2MSFTNGP05.phx.gbl...
> Hi all,
> I am using SQL 2005 sp1 to setup Merge replication for PDA access.
> i created my Primary Key column a GUID using NEWID() as the default value.
> When the snapshot was created it still went and added anothe GUID to all
> my tables called rowguid.
> i thought it was suposed to use the existing Unique GUID column before
> creating a new one?
> thanks for you advice. i hope i have the corect groups here?
> ink
>|||Sorry my point was that i had created one and SQL2005 still created its own.
Now from what i can tell i think i have figgerd it out.
The behaviour i was expecting was how SQL 2000 handled the row GUID column
for snapshots.
i am using SQL 2005.
it seems that there is in fact a property of the column called RowGuid that
must be set to Yes before creating the first snapshot.
Then SQL2005 will use that column instead of creating it's own. Just setting
the data type and making it the primary key is not enough.
Thanks for your input Marko.
"MC" <marko.culoNOSPAM@.gmail.com> wrote in message
news:eruu6p$pcf$1@.ss408.t-com.hr...
> quote:
> If a published table does not have a uniqueidentifier column with the
> ROWGUIDCOL property and a unique index, replication adds one
>
> So, check for the rowguidcol property...
>
> MC
>
> "iKiLL" <iKill@.NotMyEmail.com> wrote in message
> news:uS1$gVbWHHA.4796@.TK2MSFTNGP05.phx.gbl...
>|||Yes, you need RowGuid property. Point is, you can have any number of
uniqueidentifiers in a table, but one of them needs to have this property
set. Since SQL Server doesnt want to guess which one would you like to have
as the 'main' GUID in a table, it adds another with rowguid property set.
Offcourse, if you allready have one it doesnt need to add it.
MC
"iKiLL" <iKill@.NotMyEmail.com> wrote in message
news:e2jo3%23bWHHA.600@.TK2MSFTNGP05.phx.gbl...
> Sorry my point was that i had created one and SQL2005 still created its
> own.
> Now from what i can tell i think i have figgerd it out.
> The behaviour i was expecting was how SQL 2000 handled the row GUID column
> for snapshots.
> i am using SQL 2005.
> it seems that there is in fact a property of the column called RowGuid
> that must be set to Yes before creating the first snapshot.
> Then SQL2005 will use that column instead of creating it's own. Just
> setting the data type and making it the primary key is not enough.
> Thanks for your input Marko.
>
>
>
> "MC" <marko.culoNOSPAM@.gmail.com> wrote in message
> news:eruu6p$pcf$1@.ss408.t-com.hr...
>

Merge replication UNIQUEIDENTIFIER Column?

Hi all,
I am using SQL 2005 sp1 to setup Merge replication for PDA access.
i created my Primary Key column a GUID using NEWID() as the default value.
When the snapshot was created it still went and added anothe GUID to all my
tables called rowguid.
i thought it was suposed to use the existing Unique GUID column before
creating a new one?
thanks for you advice. i hope i have the corect groups here?
ink
Sorry my point was that i had created one and SQL2005 still created its own.
Now from what i can tell i think i have figgerd it out.
The behaviour i was expecting was how SQL 2000 handled the row GUID column
for snapshots.
i am using SQL 2005.
it seems that there is in fact a property of the column called RowGuid that
must be set to Yes before creating the first snapshot.
Then SQL2005 will use that column instead of creating it's own. Just setting
the data type and making it the primary key is not enough.
Thanks for your input Marko.
"MC" <marko.culoNOSPAM@.gmail.com> wrote in message
news:eruu6p$pcf$1@.ss408.t-com.hr...
> quote:
> If a published table does not have a uniqueidentifier column with the
> ROWGUIDCOL property and a unique index, replication adds one
>
> So, check for the rowguidcol property...
>
> MC
>
> "iKiLL" <iKill@.NotMyEmail.com> wrote in message
> news:uS1$gVbWHHA.4796@.TK2MSFTNGP05.phx.gbl...
>

Wednesday, March 21, 2012

Merge replication Primary Key violations

I have a Merge Replication Problem and I'm not sure how to proceed.

Given the following:

Multiple clients have merge subscriptions to a central Server's DB.
DB contains a table tblUser
tblUser has a Varchar field; UserLoginName, which is Primary Key.
It also has an integer field, UserValue


The replication is configured to resolve to the greater integer value.

Client 1 creates a new User
INSERT tblUser
(UserLoginName, UserValue)
VALUES('Fred',1)
At the same time Client 2 does the same thing, but with a larger value
INSERT tblUSer
(UserLoginName, UserValue)
VALUES('Fred', 2)


One of the inserts will get to the Central Server first.
The other will form a conflict.

A row insert at 'Client2.dbUsers' could not be propagated to 'CentralServer.dbUsers'.
This failure can be caused by a constraint violation.
Violation of PRIMARY KEY constraint 'PK_tblUser_1'. Cannot insert duplicate key in object 'dbo.tblUser'.

So now I have two different value fields and a conflict that cannot be resolved.


Is this fixable?
Do I need to make changes at athe Architechture level?

Hi Brian,

if you need to store both records from all subscribers, You should add a column for client id .So once tables replicates from all subscribers each client has their own id.

Example: if client 1 has the code 100 and client 2 has the code 200

the table at subscriber will be : 100,'Fred',1 (Record comes from client 1)

200,'Fred',1 (Record comes from client 2)

if you need to resolve the conflict and to store 1 record only you should assign priority for each subscriber

For more details check the following URL : http://support.microsoft.com/kb/315514

Regards,

Tarek Ghazali

SQL Server MVP

http://www.sqlmvp.com

|||

I need to store only the higher of the two.

If the record already exists and both try to update the record, only the higher is stored, as desired.

But when the record does not exist, both subscribers do an insert, and it does not store only the higher.

Reading all the KB articles on merge replication, it appears that Updates and Inserts are supposed to be resolved the same.

I can't use subscriber priority, as I don't care WHERE the record came from, only the value stored.

Monday, March 19, 2012

Merge replication of Foreign keys

Hi.
I need to add a new foreign key to a merge replicated table (It references
the PK in the same table). Is there anyway of replicating this to the other
subscribers without invalidating the snapshot or applying it separately on
each server?
AFAIK - no!
Hilary Cotter
Looking for a book on SQL Server replication?
http://www.nwsu.com/0974973602.html
"robham" <nospam@.co.uk> wrote in message
news:uXAUdaeiEHA.704@.TK2MSFTNGP12.phx.gbl...
> Hi.
> I need to add a new foreign key to a merge replicated table (It references
> the PK in the same table). Is there anyway of replicating this to the
other
> subscribers without invalidating the snapshot or applying it separately on
> each server?
>
|||Robham,
is the FK to be just a constraint? If so then I don't think it is possible
directly. I suppose you could use a combination of sp_repladdcolumn and
sp_repldropcolumn to achieve your aim but in some cases it is easier to
restart and do a noinit initialization.
If it is a new column, then sp_repladdcolumn should do it.
HTH,
Paul Ibison

Monday, March 12, 2012

Merge Replication issue (dupe key)

My invoice header table has a primary key of Location# and Invoice#. If a
subscriber does the following…
Creates invoice with the primary key of Location 4, Invoice 100.
Replicates the data.
Deletes the record and recreates it.
Replicates.
I will get an duplicate primary key entry in the conflict table.
Is this because the replication ROWGUID is NOT the primary key?
Thanks,
Steve
Steve,
the ROWGUID doesn't need to be the PK and almost always shouldn't be the PK.
When you delete the record on the subscriber are you adding the record back
on the publisher before synchronizing, or are you adding the record back on
the publisher? I'll try to duplicate this behaviour once you post back.
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com
(recommended sql server 2000 replication book:
http://www.nwsu.com/0974973602p.html)
|||Paul,
Everything is happening on the subscriber. Is created there, replicates,
deleted and recreated on the subscriber, then replicates again.
Thanks,
Steve
"Paul Ibison" wrote:

> Steve,
> the ROWGUID doesn't need to be the PK and almost always shouldn't be the PK.
> When you delete the record on the subscriber are you adding the record back
> on the publisher before synchronizing, or are you adding the record back on
> the publisher? I'll try to duplicate this behaviour once you post back.
> Cheers,
> Paul Ibison SQL Server MVP, www.replicationanswers.com
> (recommended sql server 2000 replication book:
> http://www.nwsu.com/0974973602p.html)
>
>
|||Steve,
sorry but I still can't replicate this. Perhaps there is something else
happening? Is the record changed on the publisher at any time? Also, can you
post up the exact error message.
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com
(recommended sql server 2000 replication book:
http://www.nwsu.com/0974973602p.html)
|||Paul,
Thanks for trying to replicate this and help us out. We are fairly
inexperienced in this area.
The merge agent history is showing conflicts on the last replication, but
the conflict resolver doesn't show any. Not sure how to find them. It says
28 resolved conflicts, how can I see what they were?
After looking into the situation more, there may be some other (user)
things going on which we will need to investigate further. The may be
updating and replicating from multiple P.C.'s in a manner we did not
anticipate.
After talking with them, if we can not determine what is going on, we may
have to ask again with more details.
Thanks again,
Steve
"Paul Ibison" wrote:

> Steve,
> sorry but I still can't replicate this. Perhaps there is something else
> happening? Is the record changed on the publisher at any time? Also, can you
> post up the exact error message.
> Cheers,
> Paul Ibison SQL Server MVP, www.replicationanswers.com
> (recommended sql server 2000 replication book:
> http://www.nwsu.com/0974973602p.html)
>
>

Friday, March 9, 2012

Merge replication foreign key problem

Hi there. I'm somewhat new to merge replication, and I've been having an issue with one of the scenarios that I've been trying to get working. I am using SQL 2005 on the server, with 2005 express on the client. I have 2 tables:

Photo - which has a PhotoID primary key

PhotoData - which has a PhotoDataID primary key, and a PhotoID foreign key

both primary keys are int, and set to identity. I only want the Photo table to replicate for the merge, because I want the data in the PhotoData table to only be called by demand through a web service (since the images in that table are too large to be included in the normal replication). However, when a client adds a photo to his local database (which adds a record in the photo table, and then it's actual image data in the photodata table), I can't sync the photo table any longer. The error I get is:

Could not drop object 'dbo.Photo' because it is referenced by a FOREIGN KEY constraint

I have the foreign key relationship marked as "not for replication", but that doesn't seem to help. Is there another way I should be doing this? Thanks for any help!

-PHil

Does the client have the Photo and the PhotoData tables with some data in them before it is initilaized?

If so and you dont want to lose them, then you can select the article's property of @.pre_creation_cmd='none'. By default this is set to 'drop ' and at first synchronization, the tables are tried to drop. Now when there is data and the Photo table is tried to drop, it will conflict with the FK.

|||

Hi, Philip!

Maybe, a good solution would be using data filters in merge replication? You can define data filters to filter rows of published data according on condition, dependent on current SQL server. In this case you must include both tables into publication, but data from 2nd table will not be exchanged.

Another solution - to use vertical filtering, by columns. You can exclude column, containing actual image data, from 2nd table in publication.

Both methods are well described in SQL Server Books Online (Replication -> Replication Options -> Filtering Published Data)

David.

|||

p.s. Note, that horizontal filtering (row-based) is more complex technology, and requires careful testing before using in real working system!

Good luck!

|||

Wouldn't vertical filtering not work out when I need to add the photo data into the client database after the replication (via a web service, not replication)? I need that column to exist on the client's PhotoData table, but I just don't want it's data to be replicated. My understanding of vertical filtering is that it completely omits the column entirely, but if there is a way around that, then that woudl be useful.

Otherwise, I'm not sure I follow what you mean with the horizontal filtering. Do a filter where I sync all PhotoData rows with a PhotoDataID < 0 (i.e., no rows at all), and then just add them via the web service afterwards?

Thanks for your help!

|||

Yes, the client will have data inside it before initialization, and it'll be a pull subscription. Sorry for a basic question, but where do I change this setting you're talking about?

|||The property is for sp_addmergearticle or you can set it in UI too.|||

You are right, vertical filtering will remove the column from table at subscriber.

Try to use horizontal filtering as you specified (with simple condition to synchronize no rows).

I think, this will work.

|||

I just tried your suggestions, and got the following error when I tried to synchronize:

Message
2006-12-26 11:47:07.177 {call sp_MSsetconflicttable (N'Photo', N'MSmerge_conflict_EvidenceToolPublication_Photo', N'DEVSERVER', N'EvidenceToolDatabase', N'EvidenceToolPublication')}
2006-12-26 11:47:07.364 Category:COMMAND
Source: Failed Command
Number: 0
Message: {call sp_MSsetconflicttable (N'Photo', N'MSmerge_conflict_EvidenceToolPublication_Photo', N'DEVSERVER', N'EvidenceToolDatabase', N'EvidenceToolPublication')}
2006-12-26 11:47:07.442 Category:SQLSERVER
Source: JOHNSON-9400
Number: 102
Message: Incorrect syntax near 'PhotoID'.

Where Devserver is the server's name, EvidenceToolPublication is the name of the publication, and EvidenceToolDatabase is the name of the database.

|||I've just tried it this way, and it doesn't work. It still gives me a foreign key issue, since the data isn't being replicated.|||This seems like some issue when creating the conflict procs/tables. Would you be able to get a trimmed down version of the repro script and post it here?

Saturday, February 25, 2012

Merge Replication Conflict - Primary Key Constraint

I have an application that uses Merge Replication. In my database design,
before I created the merge replication publication, I modified the tables and
set my identity columns to Yes (Not for replication) option.
I am hitting a problem however, when I try to insert a new row in one of the
tables and then replicate the data back to the server. I am getting a
conflict with the reason being:
Reason Type 5, Reason code 2627
Reason Text:
The row was inserted at Subscriber.x' but could not be inserted at Server.X.
Violation of PRIMARY KEY constraint 'PK_X'. Cannot insert duplicate key in
object X.
I thought that having Not for replication option set for identity columns
would cause replication to use the server and/or subscriber environment to
generate identity column values on inserts.
Any help would be greatly appreciated.
Hi Guy - you'll need to partition the identity ranges to avoid identity
conflicts.
The easiest, most maintainable way is to change the article properties to
enable Automatic identity range management and then reinitialize.
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com .
|||Thanks Paul. I will try this out and see if the problem is resolved. My
apologies for this message posting so many times. My browser was acting up
and reporting an error when I posted. So I thought my post had failed.
One Question: Do you know why this is happening. I must not be
understanding the purpose of not for replication option. Because I thought
this is what would resolve this type of problem.
"Paul Ibison" wrote:

> Hi Guy - you'll need to partition the identity ranges to avoid identity
> conflicts.
> The easiest, most maintainable way is to change the article properties to
> enable Automatic identity range management and then reinitialize.
> Cheers,
> Paul Ibison SQL Server MVP, www.replicationanswers.com .
>
>
|||The NFT replication allows replication agents to do an identity insert when
distributing changes. However if the renge isn't partitioned, there will
still be a conflict.
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com .