/*
** example1.c
**
** This example illustrates how to send two queries to
** SQL Server in a command batch. It binds each set
** of results and prints the rows.
**
*/
/* These are the variables used to store the returning data. */
DBCHAR crdate[DATELEN+1];
DBINT id;
DBCHAR name[DBMAXNAME+1]; /* DBMAXNAME is defined in
* "sybdb.h" as the maximum
* length for names of database
* objects, such as tables,
* columns, and procedures.
*/
DBCHAR type[TYPELEN+1];
RETCODE result_code;
printf("Demo of SQL queries in a command batch\n\n");
fflush(stdout);
/* Initialize DB-Library. */
if (dbinit() == FAIL)
exit(ERREXIT);
/* Install the user-supplied error-handling and message-handling
* routines. They are defined at the bottom of this source file.
*/
dberrhandle((EHANDLEFUNC)err_handler);
dbmsghandle((MHANDLEFUNC)msg_handler);
/*
** Get a LOGINREC structure and fill it with the necessary
** login information.
*/
/*
** Get a DBPROCESS structure for communicating with SQL Server.
** A NULL servername defaults to the server specified by DSQUERY.
*/
dbproc = dbopen(login, NULL);
/*
** We are going to retrieve some information, from a table
** named "sysobjects", regarding names of system tables and
** stored procedures.
** We will submit two queries. The first finds all the rows
** that describe system tables. The second finds all the rows
** that describe stored procedures. The program will only look
** at the first 10 rows that describe stored procedures.
*/
/* First, put the commands into the command buffer. */
dbcmd(dbproc, "select name, type, id, crdate from sysobjects");
dbcmd(dbproc, " where type = 'S' ");
dbcmd(dbproc, "select name, type, id, crdate from sysobjects");
dbcmd(dbproc, " where type = 'P' ");
/*
** Sql Server processes the command batch in the following
** order:
**
** 1) It will check for syntax errors (i.e., "use database pubs"
** is syntactically incorrect; it should be "use pubs").
** 2) The second check is a semantic check (i.e., "select * from
** titels" will be incorrect because the spelling should be
** "titles".)
** 3) The third check occurs in the actual execution phase. This
** check involves issues like permissions or memory problems.
**
** In the execution phase, dbsqlexec() and dbresults() can return
** the value "SUCCEED", which means there are more commands in the
** batch to process and that that command was successful. A value
** of "FAIL" means that the query failed but there may be more
** commands in the batch to process. A value of "NO_MORE_RESULTS"
** means that there are no more commands in the batch to process.
** Therefore, the programmer must check the return values after
** dbsqlexec() and dbresults(), as illustrated below.
**
*/
/* Send the commands to SQL Server and start execution. */
dbsqlexec(dbproc);
/* Process each command until there are no more. */
while ((result_code = dbresults(dbproc)) != NO_MORE_RESULTS)
{
if (result_code == SUCCEED)
{
/* Bind program variables. */