簡述發布訂閱中遇到的問題
問題一
數據同步過程中發現部分表未能同步
解決方案
檢查發布屬性,發現部分表沒有設置主鍵,導致無法選中發布
無法選中表
修改表結構,設置主鍵(自增),再次查看發布屬性,選中表,確定,重新初始化訂閱
問題二
自檢所有未見主鍵表,并自建
解決方案
# 單表 建 id 主鍵
alter table xxx add constraint PK_xxx_ID primary key (id)
# 檢查所有表,自建主鍵
declare @tablename sysname
declare @strsql nchar(500)
declare tableNameCursor cursor for
select b.name from sysobjects b where xtype='U' and b.name not in
(select object_name(a.parent_obj) from sysobjects a where xtype='PK' )
open tableNameCursor
fetch next from tableNameCursor into @tablename
while @@FETCH_STATUS = 0
begin
print @tablename
set @strsql= 'alter table [' + @tablename + '] add constraint PK_' + @tablename + '_ID primary key (id) '
print @strsql
exec (@strsql)
fetch next from tableNameCursor into @tablename
end
close tableNameCursor
deallocate tableNameCursor
后續問題一
無 id
列,或 id
列可為 NULL
,創建識別,按提示修改即可
消息 1911,級別 16,狀態 1,第 1 行
列名 'id' 在目標表或視圖中不存在。
消息 1750,級別 16,狀態 0,第 1 行
無法創建約束。請參閱前面的錯誤消息。
消息 8111,級別 16,狀態 1,第 1 行
無法在表 'T_xxxx' 中可為 Null 的列上定義 PRIMARY KEY 約束。
消息 1750,級別 16,狀態 0,第 1 行
無法創建約束。請參閱前面的錯誤消息。
后續問題二
主鍵列內有重復數據
解決辦法
方案一,檢查重復,后續手動修改
SELECT COUNT(ID) AS '重復次數', ID
FROM T_xxxx
GROUP BY ID
HAVING (COUNT(*) > 1)
ORDER BY ID DESC
方案二,重置自增 ID
DBCC CHECKIDENT (T_xxxx,reseed,0)