/****************************************************************************
** (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: tbinfo.sqc
**
** SAMPLE: How to get information at the table level
**
** SQL STATEMENTS USED:
** SELECT
** DECLARE CURSOR
** OPEN
** FETCH
** CLOSE
**
**
*****************************************************************************
**
** For more information on the sample programs, see the README file.
**
** For information on developing C applications, see the Application
** Development Guide.
**
** For information on using SQL statements, see the SQL Reference.
**
** For the latest information on programming, building, and running DB2
** applications, visit the DB2 Information Center:
** http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlenv.h>
#include <sqlutil.h>
#include "utilemb.h"
int TbSchemaNameGet(void);
int TbColumnInfoGet(void);
EXEC SQL BEGIN DECLARE SECTION;
char tableName[128];
char schemaName[128];
char columnName[20];
char dataType[14];
sqlint32 dataLength;
short dataScale;
EXEC SQL END DECLARE SECTION;
int main(int argc, char *argv[])
{
int rc = 0;
char dbAlias[SQL_ALIAS_SZ + 1];
char user[USERID_SZ + 1];
char pswd[PSWD_SZ + 1];
/* check the command line arguments */
rc = CmdLineArgsCheck1(argc, argv, dbAlias, user, pswd);
if (rc != 0)
{
return rc;
}
printf("\nTHIS SAMPLE SHOWS HOW TO GET INFORMATION AT THE TABLE LEVEL.\n");
/* connect to the database */
rc = DbConn(dbAlias, user, pswd);
if (rc != 0)
{
return rc;
}
strcpy(tableName, "STAFF");
rc = TbSchemaNameGet();
rc = TbColumnInfoGet();
/* disconnect from the database */
rc = DbDisconn(dbAlias);
if (rc != 0)
{
return rc;
}
return 0;
} /* end main */
int TbSchemaNameGet(void)
{
struct sqlca sqlca;
printf("\n-----------------------------------------------------------");
printf("\nUSE THE SQL STATEMENT:\n");
printf(" SELECT INTO\n");
printf("TO GET A TABLE SCHEMA NAME.\n");
/* get the table schema name */
printf("\n Execute the statement\n");
printf(" SELECT tabschema INTO :schemaName\n");
printf(" FROM syscat.tables\n");
printf(" WHERE tabname = :tableName\n");
printf(" for\n");
printf(" tableName = '%s'.\n", tableName);
EXEC SQL SELECT tabschema INTO :schemaName
FROM syscat.tables
WHERE tabname = :tableName;
EMB_SQL_CHECK("Table schema name -- Get");
/* get rid of spaces from the end of schemaName */
strtok(schemaName, " ");
printf("\n Table schema name is: %s\n", schemaName);
return 0;
} /* TbSchemaNameGet */
int TbColumnInfoGet(void)
{
struct sqlca sqlca;
printf("\n-----------------------------------------------------------");
printf("\nUSE THE SQL STATEMENTS:\n");
printf(" DECLARE CURSOR\n");
printf(" OPEN\n");
printf(" FETCH\n");
printf(" CLOSE\n");
printf("TO GET TABLE COLUMN INFO.\n");
/* get info for table columns */
printf("\n Get info for '%s.%s' table columns:\n",
schemaName, tableName);
printf("\n column name data type data size\n");
printf(" -------------------- -------------- ----------\n");
EXEC SQL DECLARE c1 CURSOR FOR
SELECT colname, typename, length, scale
FROM syscat.columns
WHERE tabschema = :schemaName AND tabname = :tableName;
EXEC SQL OPEN c1;
EMB_SQL_CHECK("Cursor -- Open");
EXEC SQL FETCH c1 INTO :columnName, :dataType, :dataLength, :dataScale;
EMB_SQL_CHECK("Cursor -- Fetch");
if (sqlca.sqlcode == 100)
{
printf("\n Data not found.\n");
}
while (sqlca.sqlcode != 100)
{
printf(" %-20.20s %-14.14s %d", columnName, dataType, dataLength);
if (dataScale != 0)
{
printf(",%d\n", dataScale);
}
else
{
printf("\n");
}
EXEC SQL FETCH c1 INTO :columnName, :dataType, :dataLength, :dataScale;
EMB_SQL_CHECK("Cursor -- Fetch");
}
EXEC SQL CLOSE c1;
EMB_SQL_CHECK("Cursor -- Close");
return 0;
} /* TbColumnInfoGet */