使用JOIN访问SQL - UPDATE ODBC表,而不链接Access中的ODBC表

时间:2014-07-21 20:27:37

标签: sql-server vba ms-access odbc access-vba

运行MS Access 2010和SQL Server 2008 SP1

我需要弄清楚如何在DoCmd.RunSQL查询上成功运行UPDATE,其中正在更新的表是未链接的 ODBC SQL Server表。

这是我尝试过的,但我得到ERROR 3135: SYNTAX ERROR IN JOIN OPERATION。我知道这是一个UPDATE在Access中具有JOIN的正确表单:

变异失败1:

UPDATE [ODBC;DSN=MyDSNname;UID=MyUserID;PWD=MyPassword].[myTbl]
INNER JOIN [myTbl]      
ON ([ODBC;DSN=MyDSNname;UID=MyUserID;PWD=MyPassword].[myTbl].[CDL] = [myTbl].[CDL])      
    AND ([ODBC;DSN=MyDSNname;UID=MyUserID;PWD=MyPassword].[myTbl].[Archive ID] = [myTbl].[Archive ID])      
    AND ([ODBC;DSN=MyDSNname;UID=MyUserID;PWD=MyPassword].[myTbl].[TimeInserted] = [myTbl].[TimeInserted])      
    and ([ODBC;DSN=MyDSNname;UID=MyUserID;PWD=MyPassword].[myTbl].[Exported] = [myTbl].[Exported])  
SET [ODBC;DSN=MyDSNname;UID=MyUserID;PWD=MyPassword].[myTbl].[RecordStatus] = [myTbl].[RecordStatus]  
WHERE Nz([ODBC;DSN=MyDSNname;UID=MyUserID;PWD=MyPassword].[myTbl].[RecordStatus],'0') <> [myTbl].[RecordStatus] 

变异失败2:

UPDATE [ODBC;DSN=MyDSNname;UID=MyUserID;PWD=MyPassword].[myTbl]  as a
INNER JOIN [myTbl] as b   
    ON a.[CDL] = b.[CDL]      
    AND a.[Archive ID] = b.[Archive ID]      
    AND a.[TimeInserted] = b.[TimeInserted]      
    and a.[Exported] = b.[Exported] 
SET a.[RecordStatus] = b.[RecordStatus]  
WHERE Nz(a.[RecordStatus],'0') <> b.[RecordStatus] 

我能够成功SELECT具有类似查询条件的记录:

 SELECT a.*
 FROM [ODBC;DSN=MyDSNname;UID=MyUserID;PWD=MyPassword].[myTbl]  as a
 INNER JOIN [myTbl] as b    
     ON a.[CDL] = b.[CDL]      
     AND a.[Archive ID] = b.[Archive ID]      
     AND a.[TimeInserted] = b.[TimeInserted]      
     and a.[Exported] = b.[Exported] 
 WHERE Nz(a.[RecordStatus],'0') <> b.[RecordStatus]

我可以使用INSERT查询中使用的架构/表约定成功UPDATE条记录:

INSERT INTO [ODBC;DSN=MyDSNname;UID=MyUserID;PWD=MyPassword].[myTbl] 
SELECT a.* FROM  [myTbl] AS a WHERE a.[RecordStatus] IS NULL; 
对于那些好奇的人来说,

编辑 - ,以下是我如何从VBA执行SQL语句:

Function fn_UpdateSQLServer()

    Dim sTblNm As String
    Dim sTypExprt As String
    Dim sCnxnStr As String, vStTime As Variant
    Dim db As Database, tbldef As DAO.TableDef

    On Error GoTo ExportTbls_Error

    sTypExprt = "ODBC Database" 'Export Type
    sCnxnStr = "ODBC;DSN=MyDSNname;UID=MyUserID;PWD=MyPassword" 'Create the connection string

    vStTime = Timer
    Application.Echo False, "Visual Basic code is executing."

    Set db = CurrentDb()

    'need a reference to Microsoft DAO 3.x library
    DoCmd.SetWarnings False
    For Each tbldef In db.TableDefs

        ' UPDATE
        If (tbldef.Name = "myTbl") Then
            'Debug.Print tbldef.Name
            sTblNm = tbldef.Name
            sSQL2 = " UPDATE [ODBC;DSN=MyDSNname;UID=MyUserID;PWD=MyPassword].[myTbl]  as a " _
                    & " INNER JOIN [myTbl] as b " _
                    & "     ON a.[CDL] = b.[CDL] " _
                    & "     AND a.[Archive ID] = b.[Archive ID] " _
                    & "     AND a.[TimeInserted] = b.[TimeInserted] " _
                    & "     and a.[Exported] = b.[Exported] " _
                    & " SET a.[RecordStatus] = b.[RecordStatus] " _
                    & " WHERE Nz(a.[RecordStatus],'0') <> b.[RecordStatus] "
            Debug.Print sSQL2
            DoCmd.RunSQL (sSQL2)
        End If

    Next tbldef
    DoCmd.SetWarnings True

    MsgBox ("Done! Time taken= " & Timer & vStTime)

    On Error GoTo 0

SmoothExit_ExportTbls:
    Set db = Nothing
    Application.Echo True
    Exit Function

ExportTbls_Error:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ExportTblsODST"
    Resume SmoothExit_ExportTbls

End Function

1 个答案:

答案 0 :(得分:0)

好的 - 我明白了。我的OP中显示的更新查询是正确写入的;但是,它们仅在SQL Server表具有主键时才起作用。一旦我定义了一个,我就能够成功执行VBA / SQL。