/****************************************************************************
** (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: getmessage.sqc 
**
** SAMPLE : How to get error message in the required locale with token
**          replacement. The tokens can be programatically obtained by
**          invoking Sqlaintp using JNI.
**
** SQL STATEMENTS USED:
**          DECLARE CURSOR 
**          OPEN
**          FETCH
**          CLOSE
**          CONNECT
**          SELECT 
**
**                           
**
** For more information about the sample programs, see the README file.
**
** For more information about SQL, see the "SQL Reference".
**
** For more information on DB2 APIs, see the Administrative API Reference.
**
** For the latest information on programming, compiling, and running DB2 
** applications, refer to the DB2 Information Center at 
**     http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
****************************************************************************/
#include <sqlenv.h>
#include <stdio.h>
#include <stdlib.h>
#include <sqlutil.h>

main()
{
  EXEC SQL INCLUDE SQLCA;

  EXEC SQL BEGIN DECLARE SECTION;
    char message[128];
  EXEC SQL END DECLARE SECTION;

  printf("How to get error message in the required locale with token\n");
  printf("  replacement. The tokens can be programatically obtained\n");
  printf("  by onvoking Sqlaintp API.\n\n");

  EXEC SQL CONNECT TO SAMPLE;

  printf("Executing\n"); 
  printf("     SELECT SYSPROC.SQLERRM('sql551',\n" );
  printf("                            'USERA;UPDATE;SYSCAT.TABLES',\n");
  printf("                            ';',\n"); 
  printf("                            'en_US',\n"); 
  printf("                            1)\n"); 
  printf("       FROM SYSIBM.SYSDUMMY1;\n");

  /* Suppose:
       'sql551' is sqlcode 
       'USERA', 'UPDATE', 'SYSCAT.TABLES' are tokens 
       ';' is the delimiter for tokens. 
       'en_US' is the locale 
     If the above information is passed to the scalar function SQLERRM,
     a message is returned in the specified LOCALE */

  /* declare cursor */
  EXEC SQL DECLARE c0 CURSOR FOR 
    SELECT SYSPROC.SQLERRM('sql551',
                           'USERA;UPDATE;SYSCAT.TABLES', 
                           ';', 
                           'en_US', 
                           1) 
      FROM SYSIBM.SYSDUMMY1;   

  /* open cursor */
  EXEC SQL OPEN c0;

  /* fetch cursor */
  EXEC SQL FETCH c0 INTO :message;

  /* displaying the message */
  printf("\nThe Message is \n%s\n", message); 

  /* close cursor */
  EXEC SQL CLOSE c0;
} /* main */