all groups > sql server replication > august 2003 >
You're in the

sql server replication

group:

conflict resolution


conflict resolution Paul de Goede
8/7/2003 2:36:59 AM
sql server replication:
Hi All,

I've got a merge publication in which I audit replicated
tables. Essentially there's an insert/update/delete
trigger on the table which keeps a changelog in an audit
table.

While replicating we had conflicts with rows being entered
into those audit tables - apparently the replication user
didn't have sufficient rights to insert into the table.

While we have adjusted the rights I want to resolve those
conflicts and insert the data.

While trying to use the interactive resolver it tells me
that the publisher identity range needs to be adjusted -
as a result of the publisher id range not matching the row
I'm trying to insert I assume. The ID column is marked as
not for replication. Firstly how do I resolve these
conflicts? Do I have to manually tear down the id
constraints and rebuild them? Secondly - is the
interactive resolve supposed to deal with this kind of
problem automatically?

Thanks in advance.
Paul

Details:
Merge Repl to SQL CE 2.0
RE: conflict resolution Paul de Goede
8/8/2003 2:54:33 AM
Hi William,

Thanks for the info however that's all pretty general
stuff and not really about my specific problem. I have
auto identity ranges set with replicating to sql ce 2.0.

My problem is that conflicts have occured due to triggers
on the published articles having a problem - thus when the
sql ce 2.0 subscribers merged the row's failed to insert
and were put into the conflict tables. I can now push that
data through without any changes (I have fixed the
triggers on the articles). But I need to know how to re-
insert that data.

The conflicting rows have been removed from the sql ce 2.0
subscribers so when they sync it doesn't insert that data.
And I can't resolve them via the interactive conflict
resolver since it gives me an error inserting into the
publisher range. Obviously it was a row created on the
subscriber so it violates the identity range management
constraints on that article at the publisher.

My question is how can I now re-insert those conflicts
into the publisher? Doesn't the interactive resolver
pretend to be a merge process thereby bypassing the
identity range constraints on those published articles?


My only avenue now is to remove the identity range
management on the published articles temporarily, insert
the data from the conflict tables via a sproc and then re-
add the constraints. This doesn't seem like a very safe
thing to do. Doesn't sql merge replication allow me to
deal with these conflicts in a better way?

thanks
Paul

[quoted text, click to view]
RE: conflict resolution v-rxwang NO[at]SPAM online.microsoft.com
8/8/2003 9:15:07 AM
Hi Paul,

You can manage identity values by:

- Allowing Microsoft? SQL Server? 2000 replication to automatically manage
identity columns by dynamically allocating ranges of identity values to the
Publisher and all the Subscribers.

- Using the Transact-SQL NOT FOR REPLICATION option when defining the
identity column.

- Using a primary key other than the identity column (for example, a
composite key or a rowguid column), if an identity column is not necessary.
This strategy eliminates the overhead of managing identity columns on the
replicated data.

You may want to refer to "Managing Identity Values" topic in SQL Books
Online for detailed information.

Replication problems are usually quite complex. It is usually best handled
by Microsoft Product Support Services. You may contact PSS through
http://support.microsoft.com/.

We need the following information to troubleshoot replication problems:

1. A SQLDIAG report from all servers that are involved in the problem. This
report includes all of the SQL Server error logs in addition to basic
information from your server, such as the output of the following stored
procedures: sp_helpdb, sp_who2, sp_lock, and sp_configure.

NOTE: There is a problem with SQLDIAG reports from clustered computers that
are running SQL Server. If your problem involves a clustered computer that
is running SQL Server, view the following article in the Microsoft
Knowledge Base:

233332.KB.EN-US: INF: How to Run SQLDIAG on a Clustered/Virtual SQL Server
http://support.microsoft.com/?id=233332

2. While the SQLDIAG report includes the SQL Server error logs, the
complete contents of the SQL Server \Log subfolder is also very valuable.
This folder not only includes the SQL Server Agent output files but also
any .dmp files that server exceptions or assertions generate. Additionally,
replication "exception logs" are stored here.

3. Save the System and Application event logs as text (.txt) files from all
the servers in your topology for the support engineer that is working your
case. We can use this information to diagnose any potential
operating-system level problems, such as network connectivity or general
hardware issues.

4. The exact error messages that the replication agents in your topology
are receiving are also extremely important in finding the correct solution
to your problem. In addition to getting the verbatim error message,
understanding the operations that lead up to the error this is occurring is
also very important. The best way to get this information is in the form of
an agent output file. For additional information about how to configure
this file, click the article number below to view the article in the
Microsoft Knowledge Base:

312292.KB.EN-US: HOW TO: Enable Replication Agents for Logging to Output
Files
http://support.microsoft.com/?id=312292

For more information on necessary information on troubleshooting
replication issues, you may check the following article:
315642.KB.EN-US INF: Information That PSS Needs to Troubleshoot SQL Server
Replication
http://support.microsoft.com/default.aspx?scid=KB;EN-US;315642

This posting is provided "AS IS" with no warranties, and confers no rights.

Sincerely,

William Wang
Microsoft Partner Online Support
--------------------
| Content-Class: urn:content-classes:message
| From: "Paul de Goede" <paul@nospam.kineticmds.com>
| Sender: "Paul de Goede" <paul@nospam.kineticmds.com>
| Subject: conflict resolution
| Date: Thu, 7 Aug 2003 02:36:59 -0700
| Lines: 30
| Message-ID: <146301c35cc7$731c55f0$3501280a@phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcNcx3McM4smZ+LORba/v1zeUI4KGA==
| Newsgroups: microsoft.public.sqlserver.replication
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.sqlserver.replication:42331
| NNTP-Posting-Host: TK2MSFTNGXA06 10.40.1.53
| X-Tomcat-NG: microsoft.public.sqlserver.replication
|
| Hi All,
|
| I've got a merge publication in which I audit replicated
| tables. Essentially there's an insert/update/delete
| trigger on the table which keeps a changelog in an audit
| table.
|
| While replicating we had conflicts with rows being entered
| into those audit tables - apparently the replication user
| didn't have sufficient rights to insert into the table.
|
| While we have adjusted the rights I want to resolve those
| conflicts and insert the data.
|
| While trying to use the interactive resolver it tells me
| that the publisher identity range needs to be adjusted -
| as a result of the publisher id range not matching the row
| I'm trying to insert I assume. The ID column is marked as
| not for replication. Firstly how do I resolve these
| conflicts? Do I have to manually tear down the id
| constraints and rebuild them? Secondly - is the
| interactive resolve supposed to deal with this kind of
| problem automatically?
|
| Thanks in advance.
| Paul
|
| Details:
| Merge Repl to SQL CE 2.0
| running SQL2K sp2
|
RE: conflict resolution v-rxwang NO[at]SPAM online.microsoft.com
8/11/2003 12:28:12 PM
Hi Paul,

I am still not sure the real situation on your side. Could you please post
the exact reproduce steps so that we could perform further research?

Sincerely,

William Wang
Microsoft Partner Online Support

Get Secure! - www.microsoft.com/security
=====================================================
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
=====================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| Content-Class: urn:content-classes:message
| From: "Paul de Goede" <paul@nospam.kineticmds.com>
| Sender: "Paul de Goede" <paul@nospam.kineticmds.com>
| References: <146301c35cc7$731c55f0$3501280a@phx.gbl>
<hAEcT2YXDHA.1856@cpmsftngxa06.phx.gbl>
| Subject: RE: conflict resolution
| Date: Fri, 8 Aug 2003 02:54:33 -0700
| Lines: 216
| Message-ID: <074201c35d93$11843ac0$a601280a@phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcNdkxGEDaPoYdJjQ2uzDAhOdIU1GQ==
| Newsgroups: microsoft.public.sqlserver.replication
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.sqlserver.replication:42377
| NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
| X-Tomcat-NG: microsoft.public.sqlserver.replication
|
| Hi William,
|
| Thanks for the info however that's all pretty general
| stuff and not really about my specific problem. I have
| auto identity ranges set with replicating to sql ce 2.0.
|
| My problem is that conflicts have occured due to triggers
| on the published articles having a problem - thus when the
| sql ce 2.0 subscribers merged the row's failed to insert
| and were put into the conflict tables. I can now push that
| data through without any changes (I have fixed the
| triggers on the articles). But I need to know how to re-
| insert that data.
|
| The conflicting rows have been removed from the sql ce 2.0
| subscribers so when they sync it doesn't insert that data.
| And I can't resolve them via the interactive conflict
| resolver since it gives me an error inserting into the
| publisher range. Obviously it was a row created on the
| subscriber so it violates the identity range management
| constraints on that article at the publisher.
|
| My question is how can I now re-insert those conflicts
| into the publisher? Doesn't the interactive resolver
| pretend to be a merge process thereby bypassing the
| identity range constraints on those published articles?
|
|
| My only avenue now is to remove the identity range
| management on the published articles temporarily, insert
| the data from the conflict tables via a sproc and then re-
| add the constraints. This doesn't seem like a very safe
| thing to do. Doesn't sql merge replication allow me to
| deal with these conflicts in a better way?
|
| thanks
| Paul
|
| >-----Original Message-----
| >Hi Paul,
| >
| >You can manage identity values by:
| >
| >- Allowing Microsoft? SQL Server? 2000 replication to
| automatically manage
| >identity columns by dynamically allocating ranges of
| identity values to the
| >Publisher and all the Subscribers.
| >
| >- Using the Transact-SQL NOT FOR REPLICATION option when
| defining the
| >identity column.
| >
| >- Using a primary key other than the identity column (for
| example, a
| >composite key or a rowguid column), if an identity column
| is not necessary.
| >This strategy eliminates the overhead of managing
| identity columns on the
| >replicated data.
| >
| >You may want to refer to "Managing Identity Values" topic
| in SQL Books
| >Online for detailed information.
| >
| >Replication problems are usually quite complex. It is
| usually best handled
| >by Microsoft Product Support Services. You may contact
| PSS through
| >http://support.microsoft.com/.
| >
| >We need the following information to troubleshoot
| replication problems:
| >
| >1. A SQLDIAG report from all servers that are involved in
| the problem. This
| >report includes all of the SQL Server error logs in
| addition to basic
| >information from your server, such as the output of the
| following stored
| >procedures: sp_helpdb, sp_who2, sp_lock, and sp_configure.
| >
| >NOTE: There is a problem with SQLDIAG reports from
| clustered computers that
| >are running SQL Server. If your problem involves a
| clustered computer that
| >is running SQL Server, view the following article in the
| Microsoft
| >Knowledge Base:
| >
| >233332.KB.EN-US: INF: How to Run SQLDIAG on a
| Clustered/Virtual SQL Server
| >http://support.microsoft.com/?id=233332
| >
| >2. While the SQLDIAG report includes the SQL Server error
| logs, the
| >complete contents of the SQL Server \Log subfolder is
| also very valuable.
| >This folder not only includes the SQL Server Agent output
| files but also
| >any .dmp files that server exceptions or assertions
| generate. Additionally,
| >replication "exception logs" are stored here.
| >
| >3. Save the System and Application event logs as text
| (.txt) files from all
| >the servers in your topology for the support engineer
| that is working your
| >case. We can use this information to diagnose any
| potential
| >operating-system level problems, such as network
| connectivity or general
| >hardware issues.
| >
| >4. The exact error messages that the replication agents
| in your topology
| >are receiving are also extremely important in finding the
| correct solution
| >to your problem. In addition to getting the verbatim
| error message,
| >understanding the operations that lead up to the error
| this is occurring is
| >also very important. The best way to get this information
| is in the form of
| >an agent output file. For additional information about
| how to configure
| >this file, click the article number below to view the
| article in the
| >Microsoft Knowledge Base:
| >
| >312292.KB.EN-US: HOW TO: Enable Replication Agents for
| Logging to Output
| >Files
| >http://support.microsoft.com/?id=312292
| >
| >For more information on necessary information on
| troubleshooting
| >replication issues, you may check the following article:
| >315642.KB.EN-US INF: Information That PSS Needs to
| Troubleshoot SQL Server
| >Replication
| >http://support.microsoft.com/default.aspx?scid=KB;EN-
| US;315642
| >
| >This posting is provided "AS IS" with no warranties, and
| confers no rights.
| >
| >Sincerely,
| >
| >William Wang
| >Microsoft Partner Online Support
| >--------------------
| >| Content-Class: urn:content-classes:message
RE: conflict resolution Paul de Goede
8/12/2003 4:21:10 AM
Hi William,

Sorry if I haven't been clear. Let me illustrate with an
example.

We have sql2ksp3 publisher and distributor on same server.
It serves up publication PubTest
Publication PubTest has 1 article in it: Article1.
Article1 has automatic identity range management on it.

Subsriber Sub1 is a SQL CE 2.0 subscription (anonymous).
It connects and get's a snapshot. It then does a local
insert into Article1. It inserts a row at ID 1001. It then
executes a merge at the publisher. Pushlisher has range 1-
1000, subscribers get a 1000 long (wide? big? sized?)
range.

In the process of this merge there is a trigger on
Article1 which fails. (in this case there was an
insert/update/delete trigger on Article1 which created a
copy of the old values and inserted them into an Audit
table for audit trail purposes however the merge agent
didn't have sufficient rights to insert into the audit
table thus the trigger failed to execute). So the insert
into the publisher Article1 table fails. This causes a row
to be inserted at conflict_PubTest_Article1.

I then change the rights of the identity the merge agent
runs under to be able to insert into the audit table. I
now want to push those conflicts through. I go to the
interactive conflict resolver. I choose the looser and say
resolve with these values. It fails. The error message is
that the publisher identity range is full (it is not full).
Obviously the problem is that the publisher id constraints
(marked as not replication) are forcing the conflict
resolver to fail the insert. However I would expect the
interactive conflict resolver to be able to bypass these
constraints so I can resolve this conflict.

It seems like a bug in the resolver to me.

Paul
[quoted text, click to view]
RE: conflict resolution v-rxwang NO[at]SPAM online.microsoft.com
8/13/2003 3:34:56 PM
Hi Paul,

Thank you for your update! In my opinion, as a workaround, you may manually
run UPDATE statement to solve the conflict instead of using the conflict
viewer. For information about UPDATE, please refer to SQL Books
Online\Transact-SQL Reference\UPDATE.

However, I would like to continue researching this issue to see if there is
other solution, to expedite the process, coule you please post the
following information?

- the table schema
- the trigger
- the log table's schema

Thanks,

William Wang
Microsoft Partner Online Support

Get Secure! - www.microsoft.com/security
=====================================================
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
=====================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| Content-Class: urn:content-classes:message
| From: "Paul de Goede" <paul@nospam.kineticmds.com>
| Sender: "Paul de Goede" <paul@nospam.kineticmds.com>
| References: <146301c35cc7$731c55f0$3501280a@phx.gbl>
<hAEcT2YXDHA.1856@cpmsftngxa06.phx.gbl>
<074201c35d93$11843ac0$a601280a@phx.gbl>
<zo9AOQAYDHA.2000@cpmsftngxa06.phx.gbl>
| Subject: RE: conflict resolution
| Date: Tue, 12 Aug 2003 04:21:10 -0700
| Lines: 338
| Message-ID: <0c8601c360c3$d4d23120$a401280a@phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Thread-Index: AcNgw9TPOM9pvkNuSjuMcjxBOXkWLQ==
| Newsgroups: microsoft.public.sqlserver.replication
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.sqlserver.replication:42478
| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
| X-Tomcat-NG: microsoft.public.sqlserver.replication
|
| Hi William,
|
| Sorry if I haven't been clear. Let me illustrate with an
| example.
|
| We have sql2ksp3 publisher and distributor on same server.
| It serves up publication PubTest
| Publication PubTest has 1 article in it: Article1.
| Article1 has automatic identity range management on it.
|
| Subsriber Sub1 is a SQL CE 2.0 subscription (anonymous).
| It connects and get's a snapshot. It then does a local
| insert into Article1. It inserts a row at ID 1001. It then
| executes a merge at the publisher. Pushlisher has range 1-
| 1000, subscribers get a 1000 long (wide? big? sized?)
| range.
|
| In the process of this merge there is a trigger on
| Article1 which fails. (in this case there was an
| insert/update/delete trigger on Article1 which created a
| copy of the old values and inserted them into an Audit
| table for audit trail purposes however the merge agent
| didn't have sufficient rights to insert into the audit
| table thus the trigger failed to execute). So the insert
| into the publisher Article1 table fails. This causes a row
| to be inserted at conflict_PubTest_Article1.
|
| I then change the rights of the identity the merge agent
| runs under to be able to insert into the audit table. I
| now want to push those conflicts through. I go to the
| interactive conflict resolver. I choose the looser and say
| resolve with these values. It fails. The error message is
| that the publisher identity range is full (it is not full).
| Obviously the problem is that the publisher id constraints
| (marked as not replication) are forcing the conflict
| resolver to fail the insert. However I would expect the
| interactive conflict resolver to be able to bypass these
| constraints so I can resolve this conflict.
|
| It seems like a bug in the resolver to me.
|
| Paul
| >-----Original Message-----
| >Hi Paul,
| >
| >I am still not sure the real situation on your side.
| Could you please post
| >the exact reproduce steps so that we could perform
| further research?
| >
| >Sincerely,
| >
| >William Wang
| >Microsoft Partner Online Support
| >
| >Get Secure! - www.microsoft.com/security
| >=====================================================
| >When responding to posts, please "Reply to Group" via
| >your newsreader so that others may learn and benefit
| >from your issue.
| >=====================================================
| >
| >This posting is provided "AS IS" with no warranties, and
| confers no rights.
| >--------------------
| >| Content-Class: urn:content-classes:message
| >| From: "Paul de Goede" <paul@nospam.kineticmds.com>
| >| Sender: "Paul de Goede" <paul@nospam.kineticmds.com>
| >| References: <146301c35cc7$731c55f0$3501280a@phx.gbl>
| ><hAEcT2YXDHA.1856@cpmsftngxa06.phx.gbl>
| >| Subject: RE: conflict resolution
| >| Date: Fri, 8 Aug 2003 02:54:33 -0700
| >| Lines: 216
| >| Message-ID: <074201c35d93$11843ac0$a601280a@phx.gbl>
| >| MIME-Version: 1.0
| >| Content-Type: text/plain;
| >| charset="iso-8859-1"
| >| Content-Transfer-Encoding: 7bit
| >| X-Newsreader: Microsoft CDO for Windows 2000
| >| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| >| Thread-Index: AcNdkxGEDaPoYdJjQ2uzDAhOdIU1GQ==
| >| Newsgroups: microsoft.public.sqlserver.replication
| >| Path: cpmsftngxa06.phx.gbl
| >| Xref: cpmsftngxa06.phx.gbl
| microsoft.public.sqlserver.replication:42377
| >| NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
| >| X-Tomcat-NG: microsoft.public.sqlserver.replication
| >|
| >| Hi William,
| >|
| >| Thanks for the info however that's all pretty general
| >| stuff and not really about my specific problem. I have
| >| auto identity ranges set with replicating to sql ce 2.0.
| >|
| >| My problem is that conflicts have occured due to
| triggers
| >| on the published articles having a problem - thus when
| the
| >| sql ce 2.0 subscribers merged the row's failed to
| insert
| >| and were put into the conflict tables. I can now push
| that
| >| data through without any changes (I have fixed the
| >| triggers on the articles). But I need to know how to re-
| >| insert that data.
| >|
| >| The conflicting rows have been removed from the sql ce
| 2.0
| >| subscribers so when they sync it doesn't insert that
| data.
| >| And I can't resolve them via the interactive conflict
| >| resolver since it gives me an error inserting into the
| >| publisher range. Obviously it was a row created on the
| >| subscriber so it violates the identity range management
| >| constraints on that article at the publisher.
| >|
| >| My question is how can I now re-insert those conflicts
| >| into the publisher? Doesn't the interactive resolver
| >| pretend to be a merge process thereby bypassing the
| >| identity range constraints on those published articles?
| >|
| >|
| >| My only avenue now is to remove the identity range
RE: conflict resolution v-rxwang NO[at]SPAM online.microsoft.com
8/18/2003 8:12:09 AM
no response from customer.
RE: conflict resolution Paul de Goede
8/20/2003 5:56:44 AM
Hi William,

We did write some sql to manually move the stuff over from
the conflict table to the article after disabling the
constraint for id ranges on that article.

We had this on quite a few tables - here is the table
schema, conflict table schema and audit trigger from one
of the simpler ones.
The failure occured because the replication user didn't
have sufficient rights to insert rows into Audit_Action
table. All of this I understand after some investigation
into the MS_merge_XXXXX role etc. I just would have
thought that the conflict resolution could have bypassed
the publisher id constraint when resolving conflicts.

thanks.

CREATE TABLE [Action] (
[iID] [int] IDENTITY (1, 1) NOT FOR REPLICATION
NOT NULL ,
[cTitle] [nvarchar] (50) COLLATE
Latin1_General_CI_AI NOT NULL ,
[cDetail] [nvarchar] (200) COLLATE
Latin1_General_CI_AI NULL ,
[iCreator] [int] NOT NULL ,
[dCreateDate] [datetime] NOT NULL ,
[dRemindDate] [datetime] NULL ,
[dActionDate] [datetime] NULL ,
[iPriority] [int] NULL ,
[iAssignee] [int] NOT NULL ,
[iActionObjectID] [int] NOT NULL ,
[iActionObjectType] [int] NOT NULL ,
[iState] [int] NOT NULL ,
[rowguid] uniqueidentifier ROWGUIDCOL NOT NULL
CONSTRAINT [DF__Action__rowguid__0C4DD891] DEFAULT (newid
()),
CONSTRAINT [PK_Action] PRIMARY KEY CLUSTERED
(
[iID]
) WITH FILLFACTOR = 90 ON [PRIMARY] ,
CONSTRAINT [FK_Action_Action_Priority] FOREIGN KEY
(
[iPriority]
) REFERENCES [ActionPriority] (
[iID]
),
CONSTRAINT [FK_Action_Assignee_Employee] FOREIGN
KEY
(
[iAssignee]
) REFERENCES [Employee] (
[iID]
),
CONSTRAINT [FK_Action_BusinessObject] FOREIGN KEY
(
[iActionObjectType]
) REFERENCES [BusinessObject] (
[iID]
),
CONSTRAINT [FK_Action_Creator_Employee] FOREIGN
KEY
(
[iCreator]
) REFERENCES [Employee] (
[iID]
),
CONSTRAINT
[repl_identity_range_pub_D6537857_4A0F_4435_A6D8_C10DFA6DA1
2D] CHECK NOT FOR REPLICATION ([iID] > 4500 and [iID] <
5500)
) ON [PRIMARY]
GO



CREATE TABLE [conflict_KMDSHeinekenTMDSP6_Action] (
[iID] [int] NOT NULL ,
[cTitle] [nvarchar] (50) COLLATE
Latin1_General_CI_AI NOT NULL ,
[cDetail] [nvarchar] (200) COLLATE
Latin1_General_CI_AI NULL ,
[iCreator] [int] NOT NULL ,
[dCreateDate] [datetime] NOT NULL ,
[dRemindDate] [datetime] NULL ,
[dActionDate] [datetime] NULL ,
[iPriority] [int] NULL ,
[iAssignee] [int] NOT NULL ,
[iActionObjectID] [int] NOT NULL ,
[iActionObjectType] [int] NOT NULL ,
[iState] [int] NOT NULL ,
[rowguid] uniqueidentifier ROWGUIDCOL NOT NULL ,
[origin_datasource] [nvarchar] (255) COLLATE
Latin1_General_CI_AI NULL ,
[conflict_type] [int] NULL ,
[reason_code] [int] NULL ,
[reason_text] [nvarchar] (720) COLLATE
Latin1_General_CI_AI NULL ,
[pubid] [uniqueidentifier] NULL ,
[MSrepl_create_time] [datetime] NULL CONSTRAINT
[DF__conflict___MSrep__740D0603] DEFAULT (getdate())
) ON [PRIMARY]
GO



CREATE TRIGGER TR_Audit_Action
ON Action
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
DECLARE @columnchanges varbinary(100)
DECLARE @columnlist nvarchar(1000)
DECLARE @columninsertedlist nvarchar(1000)
DECLARE @audittype int
DECLARE @deletecount int
DECLARE @insertcount int
DECLARE @personid int
IF (ISNUMERIC(HOST_NAME()) = 1)
BEGIN
SET @personid = CONVERT(int,HOST_NAME())
END
ELSE
BEGIN
SET @personid = null
END

SELECT @insertcount = COUNT(1) FROM inserted
SELECT @deletecount = COUNT(1) FROM deleted

IF @insertcount = 0 SET @audittype = 2 /* delete */
ELSE
BEGIN
IF @deletecount = 0 SET @audittype = 0 /* insert */
ELSE SET @audittype = 1 /* update */
END

SET @columnchanges = COLUMNS_UPDATED()

EXEC sp_ColumnlistFromVarbinary @tablename = 'Action',
@columnbits = @columnchanges,
@alwaysreturnidandrowguidcolumns = 1, @pretext = '',
@imagepretext = '', @@columnlist = @columnlist OUTPUT

DECLARE @sqlstring nvarchar(2000)
SELECT
inserted.iID,inserted.cTitle,inserted.cDetail,inserted.iCre
ator,inserted.dCreateDate,inserted.dRemindDate,inserted.dAc
tionDate,inserted.iPriority,inserted.iAssignee,inserted.iAc
tionObjectID,inserted.iActionObjectType,inserted.iState,ins
erted.rowguid, GETUTCDATE() AS auditcolumn_audittime,
@personid AS auditcolumn_auditperson, @columnchanges AS
auditcolumn_auditcolumnschanged, @audittype AS
auditcolumn_auditaction, CURRENT_USER AS
auditcolumn_currentuser INTO #TR_Audit_ActionTempTable
FROM inserted INNER JOIN Action ON inserted.iID =
Action.iID
IF @insertcount > 0
BEGIN
SET @sqlstring = 'INSERT INTO Audit_Action(' +
@columnlist + ' , auditcolumn_audittime,
auditcolumn_auditperson, auditcolumn_auditcolumnschanged,
auditcolumn_auditaction, auditcolumn_currentuser) SELECT '
+ @columnlist + ', auditcolumn_audittime,
auditcolumn_auditperson, auditcolumn_auditcolumnschanged,
auditcolumn_auditaction, auditcolumn_currentuser FROM
#TR_Audit_ActionTempTable'
END
ELSE
BEGIN
IF @deletecount > 0
BEGIN
SELECT iID, GETUTCDATE() AS
auditcolumn_audittime, @personid AS
auditcolumn_auditperson, @columnchanges AS
auditcolumn_auditcolumnschanged, @audittype AS
auditcolumn_auditaction, CURRENT_USER AS
auditcolumn_currentuser INTO #TR_Audit_ActionTempTable2
FROM deleted
SET @sqlstring = 'INSERT INTO Audit_Action
(iID , auditcolumn_audittime, auditcolumn_auditperson,
auditcolumn_auditcolumnschanged, auditcolumn_auditaction,
auditcolumn_currentuser) SELECT iID,
auditcolumn_audittime, auditcolumn_auditperson,
auditcolumn_auditcolumnschanged, auditcolumn_auditaction,
auditcolumn_currentuser FROM #TR_Audit_ActionTempTable2'
END
ELSE
BEGIN
SET @sqlstring = null
END
END
EXEC(@sqlstring)
DROP TABLE #TR_Audit_ActionTempTable
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

[quoted text, click to view]
AddThis Social Bookmark Button