Embedded SQL for C Examples

Embedded SQL for C and SQL Server

Embedded SQL for C and SQL Server

Embedded SQL for C Examples

The examples, from 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 a specified instance of Microsoft® SQL Server™ 2000 and executes a series of Embedded SQL statements.

Default Location

x:\Program Files\Microsoft SQL Server\80\Tools\Devtools\Samples\Esqlc

Running the Sample

You can build an example program for the following environments using the associated commands. N is the number of the example program.

Environment Command
Microsoft Windows® 2000,
Microsoft Windows NT® 4.0,
Microsoft Windows 95, Microsoft Windows 98
nmake -f "makefile." APP="examplen"
MS-DOS nmake -f "makefile." APP="examplen" ENV="dos"
QuickWin nmake -f "makefile." APP="examplen" ENV="qwin"

Remarks

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.

Example 1
  • Declares and opens a standard cursor 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 instances of Microsoft® SQL Server™ 2000, and declares and opens two standard cursors 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 single 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 similar to that of isql.

See Also

Samples