我有一個使用SQL Server的getdate()
函數的MS Access中的鏈接表查詢。不過,我得到這個錯誤,當我試圖運行查詢:在功能在MS Access SQL Server鏈接表傳遞查詢
如何創建一個鏈接表,允許使用SQL Server的T-SQL語法
未定義功能GETDATE?我看到這被稱爲
pass through query
但我不知道如何設置它來使用鏈接表上的連接作爲傳遞查詢。目前使用Access 2010中的查詢是:
select getdate()
如果有幫助,我用生成的錶鏈接到SQL Server以下VBA代碼:
Function LinkTable(LinkedTableAlias As String, Server As String, Database As String, SourceTableName As String, OverwriteIfExists As Boolean, Username As String, Password As String) 'This method will also update the link if the underlying table definition has been modified. If (InStr(1, LinkedTableAlias, "MSys") > 0) Then Log "Skipping " & LinkedTableAlias Exit Function End If 'The overwrite parameter will cause it to re-map/refresh the link for LinktedTable Alias, but only if it was already a linked table. ' it will not overwrite an existing query or local table with the name specified in LinkedTableAlias. 'Links to a SQL Server table without the need to set up a DSN in the ODBC Console. Dim tdfLinked As DAO.TableDef ' Open a database to which a linked table can be appended. Dim dbsCurrent As Database Set dbsCurrent = CurrentDb() 'Check for and deal with the scenario ofthe table alias already existing If TableNameInUse(LinkedTableAlias) Then 'If InStr(dbsCurrent.TableDefs(LinkedTableAlias).Connect, "AccessBackup") Then ' Exit Function 'End If If (Not OverwriteIfExists) Then Log "Can't use name '" + LinkedTableAlias + "' because it would overwrite existing table." Exit Function End If 'delete existing table, but only if it is a linked table 'If IsLinkedTable(LinkedTableAlias) Then dbsCurrent.TableDefs.Delete LinkedTableAlias dbsCurrent.TableDefs.Refresh 'Else ' Log "Can't use name '" + LinkedTableAlias + "' because it would overwrite an existing query or local table." ' Exit Function 'End If End If 'Create a linked table Set tdfLinked = dbsCurrent.CreateTableDef(LinkedTableAlias) tdfLinked.SourceTableName = SourceTableName tdfLinked.Connect = "ODBC;DRIVER={SQL Server};SERVER=" & Server & ";DATABASE=" & Database & ";UID=" & Username & ";PWD=" & Password & ";" On Error Resume Next dbsCurrent.TableDefs.Append tdfLinked If (err.Number = 3626) Then 'too many indexes on source table for Access err.Clear On Error GoTo 0 If LinkTable(LinkedTableAlias, Server, Database, "vw" & SourceTableName, OverwriteIfExists, Username, Password) Then Log "Can't link directly to table '" + SourceTableName + "' because it contains too many indexes for Access to handle. Linked to view '" & "vw" & SourceTableName & "' instead." LinkTable = True Else Log "Can't link table '" + SourceTableName + "' because it contains too many indexes for Access to handle. Create a view named '" & "vw" & SourceTableName & "' that selects all rows/columns from '" & SourceTableName & "' and try again to circumvent this." LinkTable = False End If Exit Function End If On Error GoTo 0 '** Turn on error handling On Error GoTo ErrorHandler: tdfLinked.RefreshLink LinkTable = True Exit Function ErrorHandler: Log "refreshlink failed for " & tdfLinked.Name LinkTable = True
'getdate()'是數據源中的函數,它是ms-sql服務器。當有鏈接表時,訪問是否會自動轉換爲t-sql? – SumGuy 2014-08-31 19:57:54
@SumGuy: - getdate()是MSSQL中的一個函數,而不是MSAccess,這就是您遇到錯誤的原因。 – 2014-08-31 19:58:58
我明白,但這不是我的問題,我如何才能使用鏈接的數據源並在源數據庫上執行查詢?我更新了原來的問題,感謝您的幫助至今 – SumGuy 2014-08-31 23:40:04