Embedded SQL for C Examples
The examples, 1 through 8, describe Embedded SQL for C (ESQL/C) character-mode programs written using plain C, standard C libraries, and Embedded SQL statements. Each program connects to the specified server running Microsoft® SQL Server™ and executes a series of Embedded SQL statements.
Each program described in the following examples uses the appropriate Examplen.sqc, Gcutil.c, Gcutil.h, and Quickwin.def files. Some of these programs use the author2 table, which you can create by using the Author2.sql file.
You can build an example program for the following environments by using the associated commands. n is the number of the example program.
Environment |
Command |
Microsoft Windows NT® and Microsoft Windows® 95/98 |
nmake -f "makefile." APP="examplen" |
MS-DOS |
nmake -f "makefile." APP="examplen" ENV="dos" |
QuickWin |
nmake -f "makefile." APP="examplen" ENV="qwin" |
Example 1
- Declares and opens a standard cursor by using a prepared SQL statement.
- Fetches rows from the sysobjects table in the specified database.
Example 2
- Declares and opens a browse cursor.
- Fetches rows from the sales table in the pubs database into a C structure.
Example 3
- Illustrates error handling.
- Attempts to insert a row into the authors table that violates the constraint on the au_id column.
- Reports the CHECK constraint violation.
Example 4
- Connects to two servers running Microsoft® SQL Server™, and declares and opens two standard cursors by using a prepared SQL statement.
- Fetches rows from the author2 table in the pubs database of each SQL Server.
- Allows you to issue positioned updates of each author’s last name as the rows are fetched.
Example 5
- Issues a singleton SELECT statement to retrieve a row from the titles table in the pubs database into a C structure or into the individual fields of a C structure.
- Uses browse cursors with a SELECT statement and the sp_who stored procedure.
- Issues static and dynamic INSERT statements.
- Issues a SELECT statement of various date and time values.
Example 6
- Issues multiple static SQL statements within a transaction in a Transact-SQL batch.
- Executes the sp_addtype and sp_droptype stored procedures.
- Uses a browse cursor with the sp_who stored procedure.
- Issues a SELECT statement into a temporary table and retrieves a row from that temporary table by using the EXECUTE IMMEDIATE statement.
- Executes a static Transact-SQL batch that uses control-of-flow language.
Example 7
- Opens a standard cursor by using HOLDLOCK within a transaction; inserts the cursor.
- Updates and deletes rows from the authors table in the pubs database.
- Inserts rows into the authors table within a transaction and then rolls back that transaction.
- Demonstrates that triggers are enforced by attempting to delete a row from the titles table that fires the deltitle trigger.
- Uses a browse cursor with the sp_who stored procedure.
- Executes the sp_addtype and sp_droptype stored procedures.
Example 8
- Demonstrates behavior that is similar to that of isql.
(c) 1988-98 Microsoft Corporation. All Rights Reserved.