2
我有一個傳遞查詢MyPassThru
。這是查詢的SQL的簡化版本:傳遞查詢失去對SQL所做的更改
SET NOCOUNT ON
DECLARE @FromDate datetime = '1/25/2014'
DECLARE @ThruDate datetime = '3/1/2014'
SELECT *
FROM MtgDailyTrans M
WHERE M.TransDate >= @FromDate
AND M.TransDate <= @ThruDate
我需要更改@FromDate和@ThruDate參數。我寫了下面的函數來做到這一點(source for RegExReplace):
Private gDb As DAO.Database
Function Db() As DAO.Database
If gDb Is Nothing Then Set gDb = CurrentDb
Set Db = gDb
End Function
Sub MyPassThruSetDates(FromDate As Date, ThruDate As Date)
Const FromPattern As String = "(@FromDate datetime = ')([\d/]+)'"
Const ThruPattern As String = "(@ThruDate datetime = ')([\d/]+)'"
Dim qd As DAO.QueryDef
Set qd = Db.QueryDefs("MyPassThru")
qd.SQL = RegExReplace(FromPattern, qd.SQL, "$1" & Format(FromDate, "m/d/yyyy") & "'")
qd.SQL = RegExReplace(ThruPattern, qd.SQL, "$1" & Format(ThruDate, "m/d/yyyy") & "'")
Set qd = Nothing
End Sub
的問題是,當我爲MyPassThru
更改SQL,然後運行MyPassThruSetDates()
過程中,我對SQL所做的更改都會回滾。爲什麼?