HOWTO: Use Updatable Joins in an MFC ODBC ApplicationLast reviewed: July 31, 1997Article ID: Q135109 |
The information in this article applies to:
SUMMARYOne way to use updatable joins with MFC ODBC applications utilizing the Microsoft Desktop ODBC Drivers is to use dynaset recordsets based on a predefined query stored in the database. In order to use this approach, your application must do all of the following:
- Use unique indexes on the primary key and all matching foreign key columns in all tables in the join. - Create a predefined query (a.k.a. stored procedure) which performs the join and returns all fields you will reference in your application. - Use dynasets in your MFC application. To use dynasets with a 16-bit Visual C++ application, see article Q124915,"SAMPLE: Using Dynasets with the 16-Bit MFC Database Classes". VC++ 2.x directly supports the use of dynasets. - If you open the predefined query using the "{CALL QueryName}" syntax you must force the CRecordset-derived class members m_bUpdatable and m_bAppendable TRUE to enable updatability. MORE INFORMATION
BackgroundA join links records from multiple tables matching one or more key fields in each table to create a "view." When a join is updated, the fields in the view and the corresponding fields in the tables are modified. Generally, a join is performed with a SQL statement of this nature: SELECT Table1.colA, Table2.colB FROM Table1, Table2 WHERE Table1.primary_key=Table2.foreign_key You can specify such SQL in your recordset's SELECT statement, but the resulting join will not be updatable for reasons to be discussed.
When are joins not updatable? The Microsoft desktop ODBC drivers do not support updatable joins of the form just described in the following two cases:
- Using snapshots. - Executing a SQL SELECT statement with multiple tablesUsing snapshots forces joins non-updatable. MFC snapshots by default loads the cursor library (see appendix G of the ODBC 2.0 Programmer's Reference for more information about the cursor library) which doesn't support updatable joins. The alternative to using snapshots is to use dynasets which require you to explicitly inhibit the loading of the cursor library. If you attempt to use snapshots with joins, you will first encounter the fact that MFC will mark the recordset read only. Even if you set m_bUpdatable and m_bAppendable members of CRecordset TRUE yourself, you will receive the following error:
In VC++ 1.5x: Positioned request cannot be performed because result set was generated by a join condition State:SL002[Microsoft][ODBC Cursor Library] In VC++ 2.x: Positioned request cannot be performed because result set was generated by a join condition State:SL002,Native:0,Origin:[Microsoft][ODBC Cursor Library]The Microsoft Desktop Drivers are unable to process SQLSetPos updates on a recordset opened using a join in its select statement. This means that a dynaset won't allow updatable joins unless you use a predefined query as mentioned above. If the SELECT statement contains a join, the driver will reject attempts to perform updates. This is a limitation of the drivers. If you specify a join in the SQL SELECT statement of your recordset Open() call or in the GetDefaultSQL() call and are using dynasets, you will receive the following error:
In VC++ 1.5x: Warning: multi-table recordset not updatable Warning: ODBC Success With Info, Error in row State:01S01[Microsoft][ODBC Microsoft Access 2.0 Driver] Can't update. Database or object is read-only. State:42000[Microsoft][ODBC Microsoft Access 2.0 Driver] In VC++ 2.x: Error: failure updating record. Can't update. Database or object is read-only. State:42000,Native:-1809,Origin:[Microsoft] [ODBC Microsoft Access 2.0 Driver] How to perform a join that will be updatableTo perform updatable joins based on a predefined query, you must meet the criteria laid out in the summary section of this article. Additional information about using predefined queries in MFC ODBC applications is provided below. When using a predefined query in MFC employing dynasets you can utilize two methods of opening the recordset:
REFERENCESIn the "MFC Encyclopedia" which came with Visual C++, there is an article titled "Recordset: Declaring a Class for a Predefined Query" Keywords : MfcDatabase kbprg kbprg Technology : kbMfc Version : 1.51 1.52 2.0 2.1 2.2 4.0 Platform : NT WINDOWS Issue type : kbhowto |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |