/****************************************************************************
** (c) Copyright IBM Corp. 2007 All rights reserved.
**
** The following sample of source code ("Sample") is owned by International
** Business Machines Corporation or one of its subsidiaries ("IBM") and is
** copyrighted and licensed, not sold. You may use, copy, modify, and
** distribute the Sample in any form without payment to IBM, for the purpose of
** assisting you in the development of your applications.
**
** The Sample code is provided to you on an "AS IS" basis, without warranty of
** any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR
** IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Some jurisdictions do
** not allow for the exclusion or limitation of implied warranties, so the above
** limitations or exclusions may not apply to you. IBM shall not be liable for
** any damages you suffer as a result of using, copying, modifying or
** distributing the Sample, even if IBM has been advised of the possibility of
** such damages.
*****************************************************************************
**
** SOURCE FILE NAME: fnuse.sqC
**
** SAMPLE: How to use built-in SQL functions
**
** SQL FUNCTION USED:
** IDENTITY_VAL_LOCAL
**
** SQL STATEMENTS USED:
** CREATE TABLE
** COMMIT
** EXECUTE IMMEDIATE
** VALUES
** PREPARE
** DECLARE CURSOR
** OPEN
** FETCH
** CLOSE
** DROP
**
**
*****************************************************************************
**
** For more information on the sample programs, see the README file.
**
** For information on developing embedded SQL applications see the Developing Embedded SQL Applications book.
**
** For information on using SQL statements, see the SQL Reference.
**
** For the latest information on programming, compiling, and running DB2
** applications, visit the DB2 Information Center at
** http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
****************************************************************************/
#include <string.h>
#include <sqlenv.h>
#include <sqlcodes.h>
#include <sqlutil.h>
#include "utilemb.h"
#if ((__cplusplus >= 199711L) && !defined DB2HP && !defined DB2AIX) || \
(DB2LINUX && (__LP64__ || (__GNUC__ >= 3)) )
#include <iostream>
using namespace std;
#else
#include <iostream.h>
#endif
EXEC SQL BEGIN DECLARE SECTION;
short empno;
char name[15];
char strStmt[257];
short lastIdentityValue;
short lastIdentityValueInd;
EXEC SQL END DECLARE SECTION;
class FnUse
{
public:
int ScalarFn_IDENTITY_VAL_LOCAL_Use();
};
int FnUse::ScalarFn_IDENTITY_VAL_LOCAL_Use()
{
int rc = 0;
struct sqlca sqlca;
cout << "\n-----------------------------------------------------------";
cout << "\nUSE THE SQL STATEMENTS:" << endl;
cout << " CREATE TABLE" << endl;
cout << " COMMIT" << endl;
cout << " INSERT" << endl;
cout << " PREPARE" << endl;
cout << " DECLARE CURSOR" << endl;
cout << " OPEN" << endl;
cout << " FETCH" << endl;
cout << " CLOSE" << endl;
cout << " VALUES" << endl;
cout << " DROP" << endl;
cout << "TO WORK WITH THE SCALAR FUNCTION 'IDENTITY_VAL_LOCAL'." << endl;
cout << "\n CREATE TABLE empl(empno INTEGER GENERATED BY DEFAULT"
<< "\n AS IDENTITY(START WITH 30,"
<< "\n INCREMENT BY 10),"
<< "\n name VARCHAR(10))" << endl;
EXEC SQL CREATE TABLE empl(empno INTEGER GENERATED BY DEFAULT
AS IDENTITY(START WITH 30,
INCREMENT BY 10),
name VARCHAR(10));
EMB_SQL_CHECK("table -- create");
cout << " COMMIT" << endl;
EXEC SQL COMMIT;
EMB_SQL_CHECK("transaction -- commit");
cout << "\n INSERT INTO empl(name) VALUES('Sanders')" << endl;
strcpy(strStmt, "INSERT INTO empl(name) VALUES('Sanders') ");
EXEC SQL EXECUTE IMMEDIATE :strStmt;
EMB_SQL_CHECK("table -- insert");
cout << " VALUES IDENTITY_VAL_LOCAL() returns: ";
EXEC SQL VALUES IDENTITY_VAL_LOCAL()
INTO :lastIdentityValue:lastIdentityValueInd;
EMB_SQL_CHECK("IDENTITY_VAL_LOCAL -- use");
if (lastIdentityValueInd >= 0)
{
cout << lastIdentityValue;
}
else
{
cout << "NULL";
}
cout << endl;
cout << " INSERT INTO empl(name) VALUES('Davis')" << endl;
strcpy(strStmt, "INSERT INTO empl(name) VALUES('Davis') ");
EXEC SQL EXECUTE IMMEDIATE :strStmt;
EMB_SQL_CHECK("table -- insert");
cout << " VALUES IDENTITY_VAL_LOCAL() returns: ";
EXEC SQL VALUES IDENTITY_VAL_LOCAL()
INTO :lastIdentityValue:lastIdentityValueInd;
EMB_SQL_CHECK("IDENTITY_VAL_LOCAL -- use");
if (lastIdentityValueInd >= 0)
{
cout << lastIdentityValue;
}
else
{
cout << "NULL";
}
cout << endl;
cout << " INSERT INTO empl(name) VALUES('Yamaguchi')" << endl;
strcpy(strStmt, "INSERT INTO empl(name) VALUES('Yamaguchi') ");
EXEC SQL EXECUTE IMMEDIATE :strStmt;
EMB_SQL_CHECK("table -- insert");
cout << " VALUES IDENTITY_VAL_LOCAL() returns: ";
EXEC SQL VALUES IDENTITY_VAL_LOCAL()
INTO :lastIdentityValue:lastIdentityValueInd;
EMB_SQL_CHECK("IDENTITY_VAL_LOCAL -- use");
if (lastIdentityValueInd >= 0)
{
cout << lastIdentityValue;
}
else
{
cout << "NULL";
}
cout << endl;
cout << " COMMIT" << endl;
EXEC SQL COMMIT;
EMB_SQL_CHECK("transaction -- commit");
cout << "\n SELECT empno, name FROM empl" << endl;
cout << "\n EMPNO NAME" << endl;
cout << " ----- ----------" << endl;
strcpy(strStmt, "SELECT empno, name FROM empl");
EXEC SQL PREPARE stmt1 FROM :strStmt;
EMB_SQL_CHECK("statement -- prepare");
EXEC SQL DECLARE c1 CURSOR FOR stmt1;
EXEC SQL OPEN c1;
EMB_SQL_CHECK("cursor -- open");
EXEC SQL FETCH c1 INTO :empno, :name;
EMB_SQL_CHECK("cursor -- fetch");
while (sqlca.sqlcode != 100)
{
cout.setf(ios::right, ios::adjustfield);
cout.width(9);
cout << empno << " ";
cout.setf(ios::left, ios::adjustfield);
cout.width(10);
cout << name << endl;
EXEC SQL FETCH c1 INTO :empno, :name;
EMB_SQL_CHECK("cursor -- fetch");
}
cout << endl;
EXEC SQL CLOSE c1;
EMB_SQL_CHECK("cursor -- close");
cout << " DROP TABLE empl" << endl;
EXEC SQL DROP TABLE empl;
EMB_SQL_CHECK("table -- drop");
cout << " COMMIT" << endl;
EXEC SQL COMMIT;
EMB_SQL_CHECK("transaction -- commit");
return 0;
} //FnUse::ScalarFn_IDENTITY_VAL_LOCAL_Use
int main(int argc, char *argv[])
{
int rc = 0;
CmdLineArgs check;
FnUse use;
DbEmb db;
// check the command line arguments
rc = check.CmdLineArgsCheck1(argc, argv, db);
if (rc != 0)
{
return rc;
}
cout << "\nTHIS SAMPLE SHOWS HOW TO USE FUNCTIONS." << endl;
// database -- connect to
rc = db.Connect();
if (rc != 0)
{
return rc;
}
rc = use.ScalarFn_IDENTITY_VAL_LOCAL_Use();
// disconnect from the database
rc = db.Disconnect();
if (rc != 0)
{
return rc;
}
return 0;
} //main