为什么我的脚本似乎随机截断字符串?

时间:2015-05-05 15:04:07

标签: string vbscript hex adodb

下面的脚本基本上采用源表和目标表并复制所有记录。它运作得相当好,但我遇到的一个反复出现的问题是它似乎截断了一些字符串。这是错误输出之一:

  

错误号:-2147217900说明:[Microsoft] [ODBC SQL Server   驱动程序] [SQL S erver]字符串后的未闭合引号   ' STRINGSEGMENT ^ urn: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日九时37分22秒   AM'' 123456'' 1234568798',' STRINGSEGMENT ^瓮:UUID:   e91fe151-5w4c-12e1-bac5-25b3a0

     

查询长度为584个字符

如果查看源数据,截断的字符串如下所示:

  

STRINGSEGMENT ^瓮:UUID:e91fe151-5w4c-12E1-bac5-25b3a的 0 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)

1 个答案:

答案 0 :(得分: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列复制正常。