2015-05-05 83 views
0

下面的腳本基本上採用源表和目標表並複製所有記錄。它工作得很好,但我遇到的一個反覆出現的問題是它似乎截斷了一些字符串。以下是錯誤輸出之一:爲什麼我的腳本似乎隨機截斷字符串?

錯誤號:-2147217900描述:[微軟] [ODBC SQL服務器 程序] [SQL小號erver]閉合的引號的字符串後 「STRINGSEGMENT ^甕:UUID :e9 e91fe151-5w4c-12e1-bac5-25b3a0'。

INSERT INTO TableName VALUES('23189','23189','','','1 ^^','','12/5/2013 3:37:2 2 PM','fieldvalue '','somethinghere','somethinghere','2013年12月5日上午9:37:22 AM','123456','1234568798','STRINGSEGMENT^urn:uuid: e91fe151-5w4c-12e1- bac5-25b3a0

查詢長

如果你看一下源數據584個字符,被截斷的字符串看起來是這樣的:

STRINGSEGMENT ^甕:UUID:e91fe151-5w4c-12e1上,bac5-25b3a 004b00^STRINGSEGMENT

所以它切斷它從第53字符後(高亮)。 tSQL的整個長度只有584個字符。

這是怎麼發生的?

WScript.Echo "Setting Vars..." 
Dim sConnect, tConnect, resultSet, r 
Dim sDSN, sUserName, sPassWord 
Dim tDSN, tUserName, tPassWord 
Dim value 

sDSN  = "mydsn" 
sUsername = "myusername" 
sPassword = "mypassword" 
tDSN  = "LOCAL" 
tUsername = "myusername" 
tPassword = "mypassword" 
sTable = "sourceTable" 
tTable = "targetTable" 

sSQL  = "" 'see below 
sDSN  = "DSN=" & sDSN & ";UID=" & sUsername & ";PWD=" & sPassword & ";" 
tSQL  = "Select TOP 1 ID FROM " & tTable & " ORDER BY ID Desc" 
tDSN  = "DSN=" & tDSN & ";UID=" & sUsername & ";PWD=" & sPassword & ";" 



Set sConnect = CreateObject("ADODB.Connection") 
WScript.Echo "Opening connection to source..." 
sConnect.Open sDSN 

Set tConnect = CreateObject("ADODB.Connection") 
WScript.Echo "Opening connection to target..." 
tConnect.Open tDSN 

WScript.Echo "Finding Current Record..." 
Set r = tConnect.Execute(tSQL) 
On Error Resume Next 
r.MoveFirst 
if r.eof Then currentRecord = 1 
Err.Clear 

Do While Not r.eof 
    currentRecord = r("ID") + 1 
    r.MoveNext 
Loop 
r.Close 

sSQL ="Select * FROM " & sTable & " WHERE ID >= " & currentRecord 

WScript.Echo "Beginning shadow at record " & currentRecord & "..." 
Set resultSet = sConnect.Execute(sSQL) 

resultSet.MoveFirst 
Do While Not resultSet.eof 
    On Error Resume Next 
    tSQL = "INSERT INTO " & tTable & " VALUES ('" 
    For i = 0 To resultSet.fields.Count - 1 
    if NOT IsNull(resultSet(i)) Then 
     value = replace(resultSet(i),"'","") 
     'somewhere around here 
    else 
     value = "" 
    End If 
    tSQL = tSQL & value 
    if i < resultSet.fields.Count - 1 Then 
     tSQL = tSQL & "','" 
    end if 
    Next 
    tSQL = tSQL & "')" 
    'when the error occurs, the line above doesn't seem to be processed but the line below obviously is... 
    tConnect.Execute(tSQL) 
    If (Err.Number <> 0) Then 
    WScript.Echo "Error Number: " & Err.Number & " Description: " & Err.Description 
    WScript.Echo tSQL 
    WScript.Echo "Query is " & Len(tSQL) & " characters long" 
    WScript.StdIn.ReadLine 
    Err.Clear 
    End If 
    tSQL = "" 
    resultSet.MoveNext 
Loop 

resultSet.Close 
sConnect.Close 
Set sConnect = Nothing 

tConnect.Close 
Set tConnect = Nothing 
WScript.Quit(0) 
+0

它在'...- 25b3a0' ** 00 **'4b00^STRINGSEGMENT'中是'00'。我認爲一個'0x00'值被認爲是字符串終結符的字節......(我不知道'STRINGSEGMENT'的含義和它來自哪裏)... – JosefZ

+0

關於如何避免這種情況的任何想法? –

+0

STRINGSEGMENT也是我用來替換私有文本的文本。 :)我不認爲它在這個問題上有任何相關性。 –

回答

0

我不知道爲什麼會發生這種情況,但這是我的解決方法。我不會接受這個答案,只是想要記錄。

Function allowed(n) 
    allowed = true 
    if n = 13 Then allowed = false 
    if n = 14 Then allowed = false 
End Function 


Function sanitize(v,i) 'v=value i=index 
    mystr = "" 
    if allowed(i) Then 
     if Not IsNull(v) Then 
      mystr = replace(v,"'","") 
     End If 
    end if 
    sanitize = mystr 
End Function 

基本上我只是手動排除有問題的列。請注意,我確定了第二個。真正好奇的是,第12列和第13列在源數據庫中有相同的數據,但第12列的副本很好。

相關問題