开始,下边的内容是我在工作中,碰到的问题,并且拿出来进行分析和解决,是对其过程的描述。由于时间原因没有进行繁体简体的转换。在一些字眼或术语中,简体繁体之间可能存在些差异。 如有不明可以通过文章后面的留言发表意见和建议,或发Email与我联系。
問題描述
在SQL Server 2005(版本:Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)),帶篩選的合併複製中,發現有這樣的一個問題,在訂閱者Insert數據,數據上傳到發行者,然後下載至訂閱者,發現訂閱者的部份數據被Delete,如圖描述:
下面就真實環境中的問題,創建一個模擬環境來分析說明。這裡列舉一個數據庫名為:ReplicationDB的同步情況:
各資料表之間的關係圖:
創建資料表結構和初始化數據腳本:
Use [ReplicationDB]
Go
/* 創建表結構Andy 2011-10-13*/
--(1/5) DataOwner
If object_id('[DataOwner]') Is Null
Begin
Create Table [DataOwner]
(
[ID] smallint Identity(1,1) Not Null ,
[Owner] nvarchar(50) Null
)
Alter Table [DataOwner] Add Constraint [PK_DataOwner] Primary Key Clustered ([ID] Asc)
End
--(2/5) Data1
If object_id('[Data1]') Is Null
Begin
Create Table [Data1]
(
[ID] uniqueidentifier Not Null ,
[OwnerID] smallint Not Null
)
Alter Table [Data1] Add Constraint [PK_Data1] Primary Key Nonclustered ([ID] Asc)
Alter Table [Data1] Add Constraint [FK_Data1_DataOwner] Foreign Key ([OwnerID]) References [DataOwner] ([ID])
End
--(3/5) Data2
If object_id('[Data2]') Is Null
Begin
Create Table [Data2]
(
[ID] uniqueidentifier Not Null ,
[ParentID] uniqueidentifier Not Null
)
Alter Table [Data2] Add Constraint [PK_Data2] Primary Key Nonclustered ([ID] Asc)
Alter Table [Data2] Add Constraint [FK_Data2_Data1] Foreign Key ([ParentID]) References [Data1] ([ID])
End
--(4/5) SysDataType
If object_id('[SysDataType]') Is Null
Begin
Create Table [SysDataType]
(
[ID] smallint Identity(1,1) Not Null ,
[Name] nvarchar(50) Null
)
Alter Table [SysDataType] Add Constraint [PK_SysDataType] Primary Key Nonclustered ([ID] Asc)
End
--(5/5) DataRelation
If object_id('[DataRelation]') Is Null
Begin
Create Table [DataRelation]
(
[ID] uniqueidentifier not null ,
[ParentID] uniqueidentifier Null ,
[DataTypeID] smallint Null
)
Alter Table [DataRelation] Add Constraint [PK_DataRelation] Primary Key Nonclustered ([ID] Asc)
Alter Table [DataRelation] Add Constraint [FK_DataRelation_DataTypeID] Foreign Key ([DataTypeID]) References [SysDataType] ([ID])
Alter Table [DataRelation] Add Constraint [U_DataRelation_ParentID_DataTypeID] Unique Nonclustered ([ParentID],[DataTypeID])
End
Go
if Not Exists(Select 1 From SysDataType)
Insert into SysDataType (Name)
Select N'DataType1' Union All
Select N'DataType2' Union All
Select N'DataType3'
if Not Exists(Select 1 From DataOwner)
Insert into DataOwner ([Owner])
Select N'PC1' Union All
Select N'PC2' Union All
Select N'PC3'
go
--Procedures
Go
If object_id('sp_InsertData') Is not null Drop Procedure sp_InsertData
Go
Create Procedure sp_InsertData
(
@ID uniqueidentifier,
@ParentID uniqueidentifier,
@DataType1 Smallint=null,
@DataType2 Smallint=null,
@DataType3 Smallint=null
)
As
Begin Try
Begin tran
Insert into Data2(ID,ParentID)
Values(@ID,@ParentID)
;With CTE_Data As
(
Select @DataType1 As DataTypeID Union All
Select @DataType2 As DataTypeID Union All
Select @DataType3 As DataTypeID
)
Insert into DataRelation(ID,ParentID,DataTypeID)
Select newid(),@ID,DataTypeID
From CTE_Data
Where DataTypeID is not null
Commit Tran
End Try
Begin Catch
Declare @Error nvarchar(2047)
Set @Error=Error_message()
Raiserror 50001 @Error
Rollback Tran
End Catch
Go
If object_id('sp_DeleteData') Is not null Drop Procedure sp_DeleteData
Go
Create Procedure sp_DeleteData
(
@ID uniqueidentifier
)
As
Begin Try
Begin tran
Delete c
From Data1 As a
Inner Join Data2 As b On b.ParentID=a.ID
Inner Join DataRelation As c On c.ParentID=b.ID
Where a.ID=@ID
Delete b
From Data1 As a
Inner Join Data2 As b On b.ParentID=a.ID
Where a.ID=@ID
Delete From Data1 Where ID=@ID
Commit Tran
End Try
Begin Catch
Declare @Error nvarchar(2047)
Set @Error=Error_message()
Raiserror 50001 @Error
Rollback Tran
End Catch
Go
Go
/* 創建表結構Andy 2011-10-13*/
--(1/5) DataOwner
If object_id('[DataOwner]') Is Null
Begin
Create Table [DataOwner]
(
[ID] smallint Identity(1,1) Not Null ,
[Owner] nvarchar(50) Null
)
Alter Table [DataOwner] Add Constraint [PK_DataOwner] Primary Key Clustered ([ID] Asc)
End
--(2/5) Data1
If object_id('[Data1]') Is Null
Begin
Create Table [Data1]
(
[ID] uniqueidentifier Not Null ,
[OwnerID] smallint Not Null
)
Alter Table [Data1] Add Constraint [PK_Data1] Primary Key Nonclustered ([ID] Asc)
Alter Table [Data1] Add Constraint [FK_Data1_DataOwner] Foreign Key ([OwnerID]) References [DataOwner] ([ID])
End
--(3/5) Data2
If object_id('[Data2]') Is Null
Begin
Create Table [Data2]
(
[ID] uniqueidentifier Not Null ,
[ParentID] uniqueidentifier Not Null
)
Alter Table [Data2] Add Constraint [PK_Data2] Primary Key Nonclustered ([ID] Asc)
Alter Table [Data2] Add Constraint [FK_Data2_Data1] Foreign Key ([ParentID]) References [Data1] ([ID])
End
--(4/5) SysDataType
If object_id('[SysDataType]') Is Null
Begin
Create Table [SysDataType]
(
[ID] smallint Identity(1,1) Not Null ,
[Name] nvarchar(50) Null
)
Alter Table [SysDataType] Add Constraint [PK_SysDataType] Primary Key Nonclustered ([ID] Asc)
End
--(5/5) DataRelation
If object_id('[DataRelation]') Is Null
Begin
Create Table [DataRelation]
(
[ID] uniqueidentifier not null ,
[ParentID] uniqueidentifier Null ,
[DataTypeID] smallint Null
)
Alter Table [DataRelation] Add Constraint [PK_DataRelation] Primary Key Nonclustered ([ID] Asc)
Alter Table [DataRelation] Add Constraint [FK_DataRelation_DataTypeID] Foreign Key ([DataTypeID]) References [SysDataType] ([ID])
Alter Table [DataRelation] Add Constraint [U_DataRelation_ParentID_DataTypeID] Unique Nonclustered ([ParentID],[DataTypeID])
End
Go
if Not Exists(Select 1 From SysDataType)
Insert into SysDataType (Name)
Select N'DataType1' Union All
Select N'DataType2' Union All
Select N'DataType3'
if Not Exists(Select 1 From DataOwner)
Insert into DataOwner ([Owner])
Select N'PC1' Union All
Select N'PC2' Union All
Select N'PC3'
go
--Procedures
Go
If object_id('sp_InsertData') Is not null Drop Procedure sp_InsertData
Go
Create Procedure sp_InsertData
(
@ID uniqueidentifier,
@ParentID uniqueidentifier,
@DataType1 Smallint=null,
@DataType2 Smallint=null,
@DataType3 Smallint=null
)
As
Begin Try
Begin tran
Insert into Data2(ID,ParentID)
Values(@ID,@ParentID)
;With CTE_Data As
(
Select @DataType1 As DataTypeID Union All
Select @DataType2 As DataTypeID Union All
Select @DataType3 As DataTypeID
)
Insert into DataRelation(ID,ParentID,DataTypeID)
Select newid(),@ID,DataTypeID
From CTE_Data
Where DataTypeID is not null
Commit Tran
End Try
Begin Catch
Declare @Error nvarchar(2047)
Set @Error=Error_message()
Raiserror 50001 @Error
Rollback Tran
End Catch
Go
If object_id('sp_DeleteData') Is not null Drop Procedure sp_DeleteData
Go
Create Procedure sp_DeleteData
(
@ID uniqueidentifier
)
As
Begin Try
Begin tran
Delete c
From Data1 As a
Inner Join Data2 As b On b.ParentID=a.ID
Inner Join DataRelation As c On c.ParentID=b.ID
Where a.ID=@ID
Delete b
From Data1 As a
Inner Join Data2 As b On b.ParentID=a.ID
Where a.ID=@ID
Delete From Data1 Where ID=@ID
Commit Tran
End Try
Begin Catch
Declare @Error nvarchar(2047)
Set @Error=Error_message()
Raiserror 50001 @Error
Rollback Tran
End Catch
Go