ibm-information-center/dist/eclipse/plugins/i5OS.ic.cli_5.4.0.1/rzadpexembeddedsql.htm

232 lines
8.9 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="Example: Embedded SQL and the equivalent DB2 UDB CLI function calls" />
<meta name="DC.subject" content="embedded SQL" />
<meta name="keywords" content="embedded SQL" />
<meta name="DC.Relation" scheme="URI" content="rzadphdxmp.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 1999, 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1999, 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="rzadpexembeddedsql" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Example: Embedded SQL and the equivalent DB2 UDB CLI function calls</title>
</head>
<body id="rzadpexembeddedsql"><a name="rzadpexembeddedsql"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Embedded SQL and the equivalent DB2 UDB CLI function calls</h1>
<div><div class="section"><p>This example shows embedded statements in comments, and the equivalent DB2<sup>®</sup> UDB
CLI function calls. </p>
</div>
<div class="section"><div class="note"><span class="notetitle">Note:</span> By using the code examples, you agree to the terms
of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
</div>
<div class="section"><div class="p"> <pre>/*************************************************************************
** file = embedded.c
**
** Example of executing an SQL statement using CLI.
** The equivalent embedded SQL statements are shown in comments.
**
** Functions used:
**
** SQLAllocConnect SQLFreeConnect
** SQLAllocEnv SQLFreeEnv
** SQLAllocStmt SQLFreeStmt
** SQLConnect SQLDisconnect
**
** SQLBindCol SQLFetch
** SQLSetParam SQLTransact
** SQLError SQLExecDirect
**
**************************************************************************/
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include "sqlcli.h"
#ifndef NULL
#define NULL 0
#endif
int print_err (SQLHDBC hdbc,
SQLHSTMT hstmt);
int main ()
{
SQLHENV henv;
SQLHDBC hdbc;
SQLHSTMT hstmt;
SQLCHAR server[] = "sample";
SQLCHAR uid[30];
SQLCHAR pwd[30];
SQLINTEGER id;
SQLCHAR name[51];
SQLINTEGER namelen, intlen;
SQLSMALLINT scale;
scale = 0;
/* EXEC SQL CONNECT TO :server USER :uid USING :authentication_string; */
SQLAllocEnv (&amp;henv); /* allocate an environment handle */
SQLAllocConnect (henv, &amp;hdbc); /* allocate a connection handle */
/* Connect to database indicated by "server" variable with */
/* authorization-name given in "uid", authentication-string given */
/* in "pwd". Note server, uid, and pwd contain null-terminated */
/* strings, as indicated by the 3 input lengths set to SQL_NTS */
if (SQLConnect (hdbc, server, SQL_NTS, NULL, SQL_NTS, NULL, SQL_NTS)
!= SQL_SUCCESS)
return (print_err (hdbc, SQL_NULL_HSTMT));
SQLAllocStmt (hdbc, &amp;hstmt); /* allocate a statement handle */
/* EXEC SQL CREATE TABLE NAMEID (ID integer, NAME varchar(50)); */
{
SQLCHAR create[] = "CREATE TABLE NAMEID (ID integer, NAME varchar(50))";
/* execute the sql statement */
if (SQLExecDirect (hstmt, create, SQL_NTS) != SQL_SUCCESS)
return (print_err (hdbc, hstmt));
}
/* EXEC SQL COMMIT WORK; */
SQLTransact (henv, hdbc, SQL_COMMIT); /* commit create table */
/* EXEC SQL INSERT INTO NAMEID VALUES ( :id, :name */
{
SQLCHAR insert[] = "INSERT INTO NAMEID VALUES (?, ?)";
/* show the use of SQLPrepare/SQLExecute method */
/* prepare the insert */
if (SQLPrepare (hstmt, insert, SQL_NTS) != SQL_SUCCESS)
return (print_err (hdbc, hstmt));
/* Set up the first input parameter "id" */
intlen = sizeof (SQLINTEGER);
SQLSetParam (hstmt, 1,
SQL_C_LONG, SQL_INTEGER,
(SQLINTEGER) sizeof (SQLINTEGER),
scale, (SQLPOINTER) &amp;id,
(SQLINTEGER *) &amp;intlen);
namelen = SQL_NTS;
/* Set up the second input parameter "name" */
SQLSetParam (hstmt, 2,
SQL_C_CHAR, SQL_VARCHAR,
50,
scale, (SQLPOINTER) name,
(SQLINTEGER *) &amp;namelen);
/* now assign parameter values and execute the insert */
id=500;
strcpy (name, "Babbage");
if (SQLExecute (hstmt) != SQL_SUCCESS)
return (print_err (hdbc, hstmt));
}
/* EXEC SQL COMMIT WORK; */
SQLTransact (henv, hdbc, SQL_COMMIT); /* commit inserts */
/* EXEC SQL DECLARE c1 CURSOR FOR SELECT ID, NAME FROM NAMEID; */
/* EXEC SQL OPEN c1; */
/* The application doesn't specify "declare c1 cursor for" */
{
SQLCHAR select[] = "select ID, NAME from NAMEID";
if (SQLExecDirect (hstmt, select, SQL_NTS) != SQL_SUCCESS)
return (print_err (hdbc, hstmt));
}
/* EXEC SQL FETCH c1 INTO :id, :name; */
/* Binding first column to output variable "id" */
SQLBindCol (hstmt, 1,
SQL_C_LONG, (SQLPOINTER) &amp;id,
(SQLINTEGER) sizeof (SQLINTEGER),
(SQLINTEGER *) &amp;intlen);
/* Binding second column to output variable "name" */
SQLBindCol (hstmt, 2,
SQL_C_CHAR, (SQLPOINTER) name,
(SQLINTEGER) sizeof (name),
&amp;namelen);
SQLFetch (hstmt); /* now execute the fetch */
printf("Result of Select: id = %ld name = %s\n", id, name);
/* finally, we should commit, discard hstmt, disconnect */
/* EXEC SQL COMMIT WORK; */
SQLTransact (henv, hdbc, SQL_COMMIT); /* commit the transaction */
/* EXEC SQL CLOSE c1; */
SQLFreeStmt (hstmt, SQL_DROP); /* free the statement handle */
/* EXEC SQL DISCONNECT; */
SQLDisconnect (hdbc); /* disconnect from the database */
SQLFreeConnect (hdbc); /* free the connection handle */
SQLFreeEnv (henv); /* free the environment handle */
return (0);
}
int print_err (SQLHDBC hdbc,
SQLHSTMT hstmt)
{
SQLCHAR buffer[SQL_MAX_MESSAGE_LENGTH + 1];
SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1];
SQLINTEGER sqlcode;
SQLSMALLINT length;
while ( SQLError(SQL_NULL_HENV, hdbc, hstmt,
sqlstate,
&amp;sqlcode,
buffer,
SQL_MAX_MESSAGE_LENGTH + 1,
&amp;length) == SQL_SUCCESS )
{
printf("SQLSTATE: %s Native Error Code: %ld\n",
sqlstate, sqlcode);
printf("%s \n", buffer);
printf("----------------------------- \n");
};
return(SQL_ERROR);
}
</pre>
</div>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rzadphdxmp.htm" title="This topic provides complete examples of DB2 UDB CLI applications.">Examples: DB2 UDB CLI applications</a></div>
</div>
</div>
</body>
</html>