如何针对MS SQL DB匹配MS Access查询中的特殊字符?

时间:2012-01-05 04:35:58

标签: sql sql-server tsql ms-access

我正在尝试在MS SQL数据库中对有趣的字符串运行匹配。

有趣的字符串看起来像“AA-9999”,但我怀疑' - '不是标准' - '。

Sub mySub()

Dim rsExample As Recordset
Dim rsExample2 As Recordset
Dim db As Database
Dim SearchCriteria As String

    Set db = CurrentDb

    Set rsExample2 = db.OpenRecordset( _
                                    "select * from exTable2 order by dumbID", _
                                    dbOpenDynaset, _
                                    dbSeeChanges)


    Set rsExample = db.OpenRecordset( _
                                    "select * from exTable order by dumbID", _
                                    dbOpenDynaset, _
                                    dbSeeChanges)


    rsExample.MoveFirst

    while not rsExample2.eof       

        SearchCriteria = rsExample2("dumbID")

''        SearchCriteria = "dumbID = ""AA-9999"""

        rsExample.FindNext (SearchCriteria)

        If rsExample.NoMatch Then
            MsgBox ("Missing Record")
            rsExample.MoveFirst
        Else
            '' do stuff to record
        End If
    wend
End Sub

我用以下SQL查询它,它似乎表明它是标准' - '

SELECT ASCII (substring ([dumbID ],3,1)) as dumbIDascii
FROM exTable
where goodID like '1234'

给了我

dumbIDascii
-----------
45

我怎么能说出' - '到底是什么,还是我错过了其他什么?

2 个答案:

答案 0 :(得分:1)

如果你的“ - ”是非标准的,那么它在UNICODE中,而不是ASCII。尝试在SELECT语句中使用UNICODE()函数。

答案 1 :(得分:0)

进一步播放表明它正在找到记录,没有外循环。添加外部循环后,它再次停止查找记录。

通过移动调用rsExample.MoveFirst的位置来修改sub,如下所示:

Sub mySub()

Dim rsExample As Recordset
Dim rsExample2 As Recordset
Dim db As Database
Dim SearchCriteria As String

    Set db = CurrentDb

    Set rsExample2 = db.OpenRecordset( _
                                    "select * from exTable2 order by dumbID", _
                                    dbOpenDynaset, _
                                    dbSeeChanges)


    Set rsExample = db.OpenRecordset( _
                                    "select * from exTable order by dumbID", _
                                    dbOpenDynaset, _
                                    dbSeeChanges)

''  <--- from here 

    while not rsExample2.eof       

        rsExample.MoveFirst   '' <--- to here

        SearchCriteria = rsExample2("dumbID")

''        SearchCriteria = "dumbID = ""AA-9999"""

        rsExample.FindNext (SearchCriteria)

        If rsExample.NoMatch Then
            MsgBox ("Missing Record")
            rsExample.MoveFirst
        Else
            '' do stuff to record
        End If
    wend
End Sub

解决了这个问题。

我怀疑rsExample没有像我期望的那样排序,或者早先的rsExample.FindNext调用跳转到RecordSet中的某个点而不是我正在寻找的值。要添加到我的收藏集Always do a MoveFirst, before you do a FindNext (unless you are looking for that exact same thing that you just use FindNext to search for)的新规则。