SQL: Making Direct SQL Calls (ODBC)

OverviewHow Do IFAQSampleODBC Driver List

This article explains:

  • When to use direct SQL calls.

  • How you make direct SQL calls to the data source.

****Note   ****This information applies to the MFC ODBC classes.  If you’re working with the MFC DAO classes, see the topic Comparison of Microsoft Jet Database Engine SQL and ANSI SQL in DAO Help.

When to Call SQL Directly

To create new tables, drop (delete) tables, alter existing tables, create indexes, and perform other SQL functions that change the data source schema, you must issue an SQL statement directly to the data source using Database Definition Language (DDL). When you use ClassWizard to create a recordset for a table — at design time — you can choose which columns of the table to represent in the recordset. This doesn’t allow for columns you or another user of the data source add to the table later, after your program has been compiled. The database classes don’t support DDL directly, but you can still write code to bind a new column to your recordset dynamically, at run time. For information on how to do this binding, see the article Recordset: Dynamically Binding Data Columns (ODBC).

You can use the DBMS itself to alter the schema, or another tool that lets you perform DDL functions.

You can also use ODBC function calls for sending SQL statements, such as calling a predefined query (stored procedure) that doesn’t return records.

Making Direct SQL Function Calls

You can directly execute an SQL call using a object. Set up your SQL statement string (usually in a CString) and pass it to the member function of your CDatabase object. If you use ODBC function calls to send an SQL statement that normally returns records, the records are ignored.