1

値の 1 つが UDF によって返されるクエリがあります。

select name,coord,convertCoord(coord) from testTable;

convertCoord()Regexとオブジェクトを使用MatchCollectionしてその値を返します。

Dim re As New RegExp
Dim mtch As Match
Dim matches As MatchCollection

Function convertCoord(str As String) As String

re.Pattern = "(find|this)pattern"
Set matches = re.Execute(str)
If matches.Count > 0 Then
    Set mtch = matches(1)
    convertCoord = mtch.Value
Else
    convertCoord = ""
End If

End Function

クエリを高速化しようとしていますが、 、remtchおよびの 1 つのインスタンスを作成matchesし、 を呼び出すたびに参照できるようにする方法があるかどうか疑問に思っていますconvertCoord()。私が正しく理解していれば、クエリのすべての結果行が を呼び出しconvertCoord()、すべてのオブジェクトを繰り返し構築および破棄し、このすべてのオブジェクトの作成によりクエリが遅くなります。

それとも、それらは既に静的であり、関数の外で宣言しているため、一度しか構築されていませんか?

4

1 に答える 1

1

Staticを宣言するときにキーワードを使用できますRegExp。ただし、プロシージャ (関数またはサブルーチン) 内でのみ使用できます。モジュール レベルの変数に使用しようとすると、コンパイラ エラーが発生します。

ある関数呼び出しから次の関数呼び出しまで値を保持したくないので、mtchmatches asを宣言する必要はないと思います。Staticモジュール レベルの変数にする必要がある理由もわかりません。そのため、関数に対してローカルにしました。

Function convertCoord(str As String) As String
Static re As RegExp
Dim mtch As Match
Dim matches As MatchCollection

If re Is Nothing Then
    Debug.Print "RegExp Is Nothing"
    Set re = New RegExp
    re.pattern = "(find|this)pattern"
Else
    Debug.Print "RegExp active"
End If

' insert code which uses the RegExp

End Function

クエリで同様の関数をテストします。"RegExp Is Nothing"が 1 回しか出力されないことを確認したら、Debug.Printステートメントを破棄したくなるでしょう。:-)

于 2013-02-12T05:33:36.280 に答える