我們有存儲在遠程服務器的表中的圖像。該表使用圖像列數據類型。將作業複製到作業中的遠程服務器會導致1024字節的無效數據長度
我們的複製客戶端(不是自定義的SQL服務器複製)使用一系列觸發器和作業從遠程位置將數據複製到中央數據庫中央數據庫。我發現,使用複製作業複製的圖像始終爲1024字節。
我寫了一個查詢來試圖找出爲什麼發生這種情況,這是我發現的。
如果我在SSMS中運行下面的查詢,會遇到問題,我可以更新和修復由原始複製作業複製的記錄的圖像數據。但是,如果我在作業中運行相同的查詢,則圖像列的大小始終爲1024個字節 - 與複製作業運行時的大小相同。
任何人都知道爲什麼會發生這種情況?
謝謝。
SET NOCOUNT ON
IF object_id('tempdb..#tmp') IS NOT NULL
DROP TABLE #tmp
DECLARE @msg VARCHAR(255) = NULL
,@cnt INT = 1
SET @msg = 'Count' + ' | ' + ' DocDate' + ' | ' + 'LocId' + ' | ' + ' TicketId' + ' | ' + 'DocSeq' + ' | ' + 'DataLen'
RAISERROR (
@msg
,10
,0
)
WITH NOWAIT
SET @msg = '----------------------------------------------------------------'
RAISERROR (
@msg
,10
,0
)
WITH NOWAIT
SELECT ptd.LocId
,pt.DocTime
,ptd.TicketId
,ptd.DocSeq
INTO #tmp
FROM TICKET_DOCUMENT (nolock) ptd
INNER JOIN TICKET (nolock) pt
ON pt.TicketId = ptd.TicketId
AND convert(DATE, pt.DocTime) BETWEEN '2/1/15' AND '2/14/15'
WHERE DATALENGTH(Data) = 1024
WHILE EXISTS (
SELECT 1
FROM #tmp
)
BEGIN
DECLARE @server VARCHAR(20)
,@LocId INT
,@sql VARCHAR(4000)
SELECT TOP 1 @LocId = LocId
FROM #tmp
SELECT @server = servername
FROM Location
WHERE LocId = @LocId
DECLARE @tmp TABLE (
TicketId BINARY (6)
,DocTime DATETIME
,DocSeq INT
,Data IMAGE
)
IF OBJECT_ID('tempdb..#lmt') IS NOT NULL
DROP TABLE #lmt
SELECT TOP 1 *
INTO #lmt
FROM #tmp
WHERE LocId = @LocId
SET @sql = 'SELECT rptd.TicketId
,t.DocTime
,rptd.DocSeq
,rptd.Data
FROM ' + @server + '.db_name.dbo.ticket_document rptd
INNER JOIN #lmt t
ON t.TicketId = rptd.TicketId
AND t.DocSeq = rptd.DocSeq
AND t.LocId = rptd.LocId'
INSERT INTO @tmp
EXEC (@sql)
WHILE EXISTS (
SELECT 1
FROM @tmp
)
BEGIN
DECLARE @TicketId BINARY (6)
,@DocSeq INT
,@DataLen INT
,@DocTime DATETIME
SELECT TOP 1 @TicketId = TicketId
,@DocTime = DocTime
,@DocSeq = DocSeq
,@DataLen = datalength(Data)
FROM @tmp
BEGIN TRANSACTION
UPDATE TICKET_DOCUMENT
SET Data = t.Data
FROM TICKET_DOCUMENT ptd
INNER JOIN @tmp t
ON t.TicketId = ptd.TicketId
AND t.DocSeq = ptd.DocSeq
WHERE t.TicketId = @TicketId
AND t.DocSeq = @DocSeq
COMMIT TRANSACTION
SET @msg = dbo.udf_PadString('left', convert(VARCHAR(6), @cnt), ' ', 5) + ' | ' + convert(VARCHAR, @DocTime, 101) + ' | ' + dbo.udf_PadString('left', convert(VARCHAR(2), @LocId), ' ', 7) + ' | 0x' + convert(VARCHAR, @TicketId, 2) + ' | ' + dbo.udf_PadString('left', convert(VARCHAR(3), @DocSeq), ' ', 6) + ' | ' + dbo.udf_PadString('left', convert(VARCHAR(11), @DataLen), ' ', 7)
RAISERROR (
@msg
,10
,0
)
WITH NOWAIT
DELETE
FROM @tmp
WHERE TicketId = @TicketId
AND DocSeq = @DocSeq
SET @cnt += 1
END
DELETE #tmp
FROM #tmp t
INNER JOIN #lmt l
ON l.LocId = t.LocId
AND l.TicketId = t.TicketId
AND l.DocSeq = t.DocSeq
END
可以嘗試使用臨時表#TMP2代替@tmp? – Tim3880