473,398 Members | 2,165 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,398 software developers and data experts.

Merge Replication and Trigger Problem

Hi,

I have an issue with my replication at the moment. I will try to
describe the scenario accurately.

I am using MS SQL 2000 SP4 with Merge Replication. Subscribers connect
to the publisher to upload/download changes. I have a trigger set up on
one table which updates another, here is an example of the trigger:

"CREATE TRIGGER qt_t_projTotal ON dbo.qt_quotes
FOR INSERT, UPDATE, DELETE
AS
declare @projTotal as money
declare @projId as int
declare @projcurrtype as int

select @projId = project_id from inserted
select @projcurrtype = proj_curr_type from qt_projects where project_id
= @projId

--Get project total from the sum of table [qt_quotes]
select @projTotal = (select
sum(dbo.fConvertCurrency(quot_grnd_totl,quot_curr_ type,@projcurrtype))
as quoteTotal from qt_quotes where project_id = @projId)

--Update projects record with new project total
update qt_projects
set proj_act_totl = @projTotal
where project_id = @projId"

I feel my trigger maybe setup incorrectly in that replication thinks an
insert is occurring instead of an update. (Im quite new to triggers)
What is happening is a conflict is occuring with the following message:

"The row was inserted at Server.Publisher' but could not be inserted at
'Subscriber.database'. INSERT statement conflicted with COLUMN FOREIGN
KEY constraint 'FK_qt_quotes_qt_projects'. The conflict occurred in
database 'Publisher', table 'qt_projects', column 'project_id'."

What is also happening as a result of this conflict (I think) is the
record in question is getting deleted from the Publisher. This is
causing huge problems as it is proving quite difficult to get these
records back in the system due to identity values.

Can anyone guide me to what might be happeing here, is it the trigger?

Jan 4 '07 #1
8 7862
Benzine wrote:
Hi,

I have an issue with my replication at the moment. I will try to
describe the scenario accurately.

I am using MS SQL 2000 SP4 with Merge Replication. Subscribers connect
to the publisher to upload/download changes. I have a trigger set up on
one table which updates another, here is an example of the trigger:

"CREATE TRIGGER qt_t_projTotal ON dbo.qt_quotes
FOR INSERT, UPDATE, DELETE
AS
declare @projTotal as money
declare @projId as int
declare @projcurrtype as int

select @projId = project_id from inserted
select @projcurrtype = proj_curr_type from qt_projects where project_id
= @projId

--Get project total from the sum of table [qt_quotes]
select @projTotal = (select
sum(dbo.fConvertCurrency(quot_grnd_totl,quot_curr_ type,@projcurrtype))
as quoteTotal from qt_quotes where project_id = @projId)

--Update projects record with new project total
update qt_projects
set proj_act_totl = @projTotal
where project_id = @projId"
First thing to notice here is that your trigger is going to have issues
with any multi-row insert/update/delete statements. You need to get
that fixed.

However, I don't think that's your problem...
I feel my trigger maybe setup incorrectly in that replication thinks an
insert is occurring instead of an update. (Im quite new to triggers)
What is happening is a conflict is occuring with the following message:

"The row was inserted at Server.Publisher' but could not be inserted at
'Subscriber.database'. INSERT statement conflicted with COLUMN FOREIGN
KEY constraint 'FK_qt_quotes_qt_projects'. The conflict occurred in
database 'Publisher', table 'qt_projects', column 'project_id'."

What is also happening as a result of this conflict (I think) is the
record in question is getting deleted from the Publisher. This is
causing huge problems as it is proving quite difficult to get these
records back in the system due to identity values.

Can anyone guide me to what might be happeing here, is it the trigger?
Merge replication is funny. So far as I can work out, in 2000, you
cannot force the merges to happen in a particular order. So it's
possible for it to merge data in the referencing table before it merges
data in the referenced table, for a particular foreign key.

In our systems, we've marked all of the foreign keys as "not for
replication", which has eliminated these kinds of errors for us. I'm
not sure what your options are if you cannot cope with "orphan" rows
appearing for brief moments of time.

On a side note (not applicable to OP), in 2005 you can specify the
order in which articles are processed. But I can't see how that can be
useful to anyone, since, in general, you would want to process inserts
in one order (for foreign keys to always work), and deletes in the
opposite order, surely?

Damien

Jan 4 '07 #2
Thanks for your reply Damien,

Is unchecking "Enforce relationships for replication" the same as
marking FK "Not for Replication"

Damien wrote:
Benzine wrote:
Hi,

I have an issue with my replication at the moment. I will try to
describe the scenario accurately.

I am using MS SQL 2000 SP4 with Merge Replication. Subscribers connect
to the publisher to upload/download changes. I have a trigger set up on
one table which updates another, here is an example of the trigger:

"CREATE TRIGGER qt_t_projTotal ON dbo.qt_quotes
FOR INSERT, UPDATE, DELETE
AS
declare @projTotal as money
declare @projId as int
declare @projcurrtype as int

select @projId = project_id from inserted
select @projcurrtype = proj_curr_type from qt_projects where project_id
= @projId

--Get project total from the sum of table [qt_quotes]
select @projTotal = (select
sum(dbo.fConvertCurrency(quot_grnd_totl,quot_curr_ type,@projcurrtype))
as quoteTotal from qt_quotes where project_id = @projId)

--Update projects record with new project total
update qt_projects
set proj_act_totl = @projTotal
where project_id = @projId"
First thing to notice here is that your trigger is going to have issues
with any multi-row insert/update/delete statements. You need to get
that fixed.

However, I don't think that's your problem...
I feel my trigger maybe setup incorrectly in that replication thinks an
insert is occurring instead of an update. (Im quite new to triggers)
What is happening is a conflict is occuring with the following message:

"The row was inserted at Server.Publisher' but could not be inserted at
'Subscriber.database'. INSERT statement conflicted with COLUMN FOREIGN
KEY constraint 'FK_qt_quotes_qt_projects'. The conflict occurred in
database 'Publisher', table 'qt_projects', column 'project_id'."

What is also happening as a result of this conflict (I think) is the
record in question is getting deleted from the Publisher. This is
causing huge problems as it is proving quite difficult to get these
records back in the system due to identity values.

Can anyone guide me to what might be happeing here, is it the trigger?

Merge replication is funny. So far as I can work out, in 2000, you
cannot force the merges to happen in a particular order. So it's
possible for it to merge data in the referencing table before it merges
data in the referenced table, for a particular foreign key.

In our systems, we've marked all of the foreign keys as "not for
replication", which has eliminated these kinds of errors for us. I'm
not sure what your options are if you cannot cope with "orphan" rows
appearing for brief moments of time.

On a side note (not applicable to OP), in 2005 you can specify the
order in which articles are processed. But I can't see how that can be
useful to anyone, since, in general, you would want to process inserts
in one order (for foreign keys to always work), and deletes in the
opposite order, surely?

Damien
Jan 4 '07 #3
Benzine wrote:
Thanks for your reply Damien,

Is unchecking "Enforce relationships for replication" the same as
marking FK "Not for Replication"
Um, yes, I believe so. (Being a poncy type, I tend to do all this kind
of work through writing SQL rather than using Enterprise Manager, but
it looks like it's the sensible choice).

Damien

Jan 4 '07 #4
Damien wrote:
On a side note (not applicable to OP), in 2005 you can specify the
order in which articles are processed. But I can't see how that can be
useful to anyone, since, in general, you would want to process inserts
in one order (for foreign keys to always work), and deletes in the
opposite order, surely?
I suppose it could work if (1) the order is reversed for deletes, either
automatically or by request, or (2) cascade-delete triggers are used.
Jan 4 '07 #5
Thanks allot for your help.
I will make the changes and issue a new snapshot then cross my fingers.

On Jan 5, 1:02 am, "Damien" <Damien_The_Unbelie...@hotmail.comwrote:
Benzine wrote:
Thanks for your reply Damien,
Is unchecking "Enforce relationships for replication" the same as
marking FK "Not for Replication"Um, yes, I believe so. (Being a poncy type, I tend to do all this kind
of work through writing SQL rather than using Enterprise Manager, but
it looks like it's the sensible choice).

Damien
Jan 4 '07 #6
Hi Damien,

You wouldnt by chance have a script that can update the
status_for_replication to Not_For_Replication for all FK's?

Ben

On Jan 4, 7:37 pm, "Damien" <Damien_The_Unbelie...@hotmail.comwrote:
Benzine wrote:
Hi,
I have an issue with my replication at the moment. I will try to
describe the scenario accurately.
I am using MS SQL 2000 SP4 with Merge Replication. Subscribers connect
to the publisher to upload/download changes. I have a trigger set up on
one table which updates another, here is an example of the trigger:
"CREATE TRIGGER qt_t_projTotal ON dbo.qt_quotes
FOR INSERT, UPDATE, DELETE
AS
declare @projTotal as money
declare @projId as int
declare @projcurrtype as int
select @projId = project_id from inserted
select @projcurrtype = proj_curr_type from qt_projects where project_id
= @projId
--Get project total from the sum of table [qt_quotes]
select @projTotal = (select
sum(dbo.fConvertCurrency(quot_grnd_totl,quot_curr_ type,@projcurrtype))
as quoteTotal from qt_quotes where project_id = @projId)
--Update projects record with new project total
update qt_projects
set proj_act_totl = @projTotal
where project_id = @projId"First thing to notice here is that your trigger is going to have issues
with any multi-row insert/update/delete statements. You need to get
that fixed.

However, I don't think that's your problem...
I feel my trigger maybe setup incorrectly in that replication thinks an
insert is occurring instead of an update. (Im quite new to triggers)
What is happening is a conflict is occuring with the following message:
"The row was inserted at Server.Publisher' but could not be inserted at
'Subscriber.database'. INSERT statement conflicted with COLUMN FOREIGN
KEY constraint 'FK_qt_quotes_qt_projects'. The conflict occurred in
database 'Publisher', table 'qt_projects', column 'project_id'."
What is also happening as a result of this conflict (I think) is the
record in question is getting deleted from the Publisher. This is
causing huge problems as it is proving quite difficult to get these
records back in the system due to identity values.
Can anyone guide me to what might be happeing here, is it the trigger?Merge replication is funny. So far as I can work out, in 2000, you
cannot force the merges to happen in a particular order. So it's
possible for it to merge data in the referencing table before it merges
data in the referenced table, for a particular foreign key.

In our systems, we've marked all of the foreign keys as "not for
replication", which has eliminated these kinds of errors for us. I'm
not sure what your options are if you cannot cope with "orphan" rows
appearing for brief moments of time.

On a side note (not applicable to OP), in 2005 you can specify the
order in which articles are processed. But I can't see how that can be
useful to anyone, since, in general, you would want to process inserts
in one order (for foreign keys to always work), and deletes in the
opposite order, surely?

Damien- Hide quoted text -- Show quoted text -
Jan 5 '07 #7
Benzine wrote:
Hi Damien,

You wouldnt by chance have a script that can update the
status_for_replication to Not_For_Replication for all FK's?

Ben
I'm afraid not. I believe that the first time I encountered this
problem for a project, what I ended up doing was scripting out all of
the foreign keys to a script file. Then using an editor with support
for regular expressions in find and replace (in my case, Visual
Studio), I edited the file to include the "NOT FOR REPLICATION" at the
appropriate places in the file (see Books On Line for ALTER TABLE to
find the right syntax). Then I wrote another script which just tore
down all existing foreign keys in the database. Applying these scripts
in the correct order produced the necessary changes.

On the second project, I had it included from the start, so I've never
had to do this again.

Damien

Jan 5 '07 #8
Thanks again.

Damien wrote:
Benzine wrote:
Hi Damien,

You wouldnt by chance have a script that can update the
status_for_replication to Not_For_Replication for all FK's?

Ben
I'm afraid not. I believe that the first time I encountered this
problem for a project, what I ended up doing was scripting out all of
the foreign keys to a script file. Then using an editor with support
for regular expressions in find and replace (in my case, Visual
Studio), I edited the file to include the "NOT FOR REPLICATION" at the
appropriate places in the file (see Books On Line for ALTER TABLE to
find the right syntax). Then I wrote another script which just tore
down all existing foreign keys in the database. Applying these scripts
in the correct order produced the necessary changes.

On the second project, I had it included from the start, so I've never
had to do this again.

Damien
Jan 5 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Karthik | last post by:
Hi, We have setup a publisher and a distributor in SQL 2000 running SP3. Alongwith this we have setup a merge replication agent and its running successfully inside the same network. I allow...
0
by: Gert Wurzer | last post by:
Hi! I hope anyone can help me with this very important problem! Since configurig one subscriber in our merge replication scenario to a subscribing publisher we get a lot of merge conflicts...
0
by: hd | last post by:
Are there any standard approaches used to purge records from database in merge replication senario ? We are using merge replication between two sql server 2000 databases. These databasess have...
3
by: jaykchan | last post by:
One of the table that is in a merge replication somehow is missing an index. Strangely, only the table in one of the subscriber of the merge replication is missing the index; another subscriber and...
1
by: tedd_n_alex | last post by:
I have database on SQL Server 2000 set up with a merge publication. This publication is configured with a number of dynamic filters to reduce the amount of data sent to each client. Each client has...
1
by: EoRaptor013 | last post by:
We have a situation almost exactly like that in the MS documentation vis a vis peer-to-peer replication. We have three servers and three user groups, one each in Chicago, New York, and Bermuda....
0
by: sk.rasheedfarhan | last post by:
Hi all, I set the configuration for Merge replication for Subscription on one database and I have created the Merge replication for publication on another machine. And I updated columns of...
0
by: Piotr Wojtysiak | last post by:
Hi. I have a problem with trigger FOR in merge-replication. My trigger is not "NOT FOR REPLICATION" and it's modified table with beyond replication. In replication this trigger not fire. Why?...
0
by: ho.horace | last post by:
Dear All, I am using SQL 2005 as pub and SQL EXPRESS as sub with Merge replication. Got the following error message The schema script 'CD_InTransit_v_153.sch' could not be propagated to the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.