我有一个使用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