Unix Technical Forum

Merge Replication and Trigger Problem

This is a discussion on Merge Replication and Trigger Problem within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, I have an issue with my replication at the moment. I will try to describe the scenario accurately. ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 11:58 AM
Benzine
 
Posts: n/a
Default 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?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 11:58 AM
Damien
 
Posts: n/a
Default Re: Merge Replication and Trigger Problem

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 11:58 AM
Benzine
 
Posts: n/a
Default Re: Merge Replication and Trigger Problem

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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-01-2008, 11:58 AM
Damien
 
Posts: n/a
Default Re: Merge Replication and Trigger Problem

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-01-2008, 11:58 AM
Ed Murphy
 
Posts: n/a
Default Re: Merge Replication and Trigger Problem

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-01-2008, 11:59 AM
Benzine
 
Posts: n/a
Default Re: Merge Replication and Trigger Problem

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.com> wrote:
> 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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-01-2008, 11:59 AM
Benzine
 
Posts: n/a
Default Re: Merge Replication and Trigger Problem

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.com> 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- Hide quoted text -- Show quoted text -


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 03-01-2008, 11:59 AM
Damien
 
Posts: n/a
Default Re: Merge Replication and Trigger Problem

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 03-01-2008, 11:59 AM
Benzine
 
Posts: n/a
Default Re: Merge Replication and Trigger Problem

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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:02 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com