免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2680 | 回复: 1
打印 上一主题 下一主题

发发牢骚,被ODBC接口折磨死了.... [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-07-07 18:41 |只看该作者 |倒序浏览
要得到procedure的返回值,oracle下和informix还不一样,直到看到informix下关于procudure调用的DEMO才知,原来需要BindCol才可以得到他的返回。
BindParameter,都bind无数次了,执行总说不对。不知道什么地方搞错了一些,
唉,要疯了.....

论坛徽章:
11
数据库技术版块每日发帖之星
日期:2016-06-25 06:20:00数据库技术版块每日发帖之星
日期:2016-06-24 06:20:00数据库技术版块每日发帖之星
日期:2016-05-03 06:20:00数据库技术版块每日发帖之星
日期:2016-04-21 06:20:00数据库技术版块每日发帖之星
日期:2016-01-23 06:20:00数据库技术版块每日发帖之星
日期:2015-12-03 06:20:00综合交流区版块每周发帖之星
日期:2015-12-02 15:03:53数据库技术版块每日发帖之星
日期:2015-10-19 06:20:00数据库技术版块每日发帖之星
日期:2015-08-20 06:20:002015年辞旧岁徽章
日期:2015-03-03 16:54:15数据库技术版块每日发帖之星
日期:2016-07-30 06:20:00
2 [报告]
发表于 2007-07-11 20:07 |只看该作者
/***************************************************************************
*        Licensed Materials - Property of IBM
*
*
*        "Restricted Materials of IBM"
*
*
*
*        IBM Informix Client SDK
*
*
*        (C) Copyright IBM Corporation 1997, 2004 All rights reserved.
*
*
*
*
*
*  Title:          proc.c
*
*  Description:    To execute a stored procedure returning multiple results
*
*                  The stored procedure executed is -
*                         PROCEDURE multiReturnProc (i integer)
*                      RETURNING integer;
*                        define j integer;
*                          for j in (1 to 5)
*                            return i*j with resume;
*                          end for;
*                    END PROCEDURE
*
*                  Because the Informix ODBC driver version 3.3 does not
*                  yet suport output parameters for stored procedures,
*                  the way to get return values from a procedure is to
*                  use SQLBindCol and SQLFetch. This works whether the
*                  procedure returns a single value or multiple values.
*
*
***************************************************************************
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef NO_WIN32
#include <io.h>
#include <windows.h>
#include <conio.h>
#endif /*NO_WIN32*/

#include "infxcli.h"


#define ERRMSG_LEN      200

SQLCHAR   defDsn[] = "odbc_demo";


SQLINTEGER checkError (SQLRETURN       rc,
                SQLSMALLINT     handleType,
                                SQLHANDLE       handle,
                                SQLCHAR*            errmsg)
{
    SQLRETURN       retcode = SQL_SUCCESS;

    SQLSMALLINT     errNum = 1;
        SQLCHAR                    sqlState[6];
    SQLINTEGER      nativeError;
        SQLCHAR                    errMsg[ERRMSG_LEN];
    SQLSMALLINT     textLengthPtr;


    if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
    {
        while (retcode != SQL_NO_DATA)
        {
            retcode = SQLGetDiagRec (handleType, handle, errNum, sqlState, &nativeError, errMsg, ERRMSG_LEN, &textLengthPtr);

            if (retcode == SQL_INVALID_HANDLE)
            {
                fprintf (stderr, "checkError function was called with an invalid handle!!\n");
                return 1;
            }

            if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
                fprintf (stderr, "ERROR: %d:  %s : %s \n", nativeError, sqlState, errMsg);

            errNum++;
        }

        fprintf (stderr, "%s\n", errmsg);
        return 1;   /* all errors on this handle have been reported */
    }
        else
                return 0;        /* no errors to report */
}




int main (long         argc,
          char*        argv[])
{
        /* Declare variables
    */

    /* Handles */            
        SQLHDBC     hdbc;
    SQLHENV     henv;
    SQLHSTMT    hstmt;

    /* Miscellaneous variables */

    SQLCHAR       dsn[20];   /*name of the DSN used for connecting to the database*/
    SQLRETURN   rc = 0;
        SQLINTEGER         in;


    SQLCHAR*       createProcStmt = (SQLCHAR *) "CREATE PROCEDURE multiReturnProc (i integer)\
                                  RETURNING integer;\
                                    define j integer;\
                                    for j in (1 to 5)\
                                        return i*j with resume;\
                                    end for;\
                                  END PROCEDURE;";

    SQLCHAR*       dropProcStmt = (SQLCHAR *) "DROP PROCEDURE multiReturnProc";
    SQLSMALLINT       inputParam = 2;
    SQLINTEGER         retVal = 7, cbRetVal = 0, cbInputParam = 0;
   
   
    /*  STEP 1. Get data source name from command line (or use default)
    **          Allocate the environment handle and set ODBC version
    **          Allocate the connection handle
    **          Establish the database connection
    **          Allocate the statement handle
    */


    /* If(dsn is not explicitly passed in as arg) */
    if (argc != 2)
    {
        /* Use default dsn - odbc_demo */
        fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
        strcpy ((char *)dsn, (char *)defDsn);
    }
    else
    {
        /* Use specified dsn */
        strcpy ((char *)dsn, (char *)argv[1]);
        fprintf (stdout, "\nUsing specified DSN : %s\n", dsn);
    }

   
    /* Allocate the Environment handle */
    rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
    if (rc != SQL_SUCCESS)
    {
        fprintf (stdout, "Environment Handle Allocation failed\nExiting!!");
        return (1);
    }


    /* Set the ODBC version to 3.0 */
    rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
    if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!"))
                return (1);


    /* Allocate the connection handle */
    rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
    if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!"))
                return (1);


    /* Establish the database connection */
    rc = SQLConnect (hdbc, dsn, SQL_NTS, (SQLCHAR *) "", SQL_NTS, (SQLCHAR *) "", SQL_NTS);
        if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\nExiting!!"))
                return (1);


    /* Allocate the statement handle */
    rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
    if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
                return (1);
   
   

        fprintf (stdout, "STEP 1 done...connected to database\n");




        /* STEP 2.  Create the stored procedure in the database
    */

    /* Execute the SQL statement to create the stored procedure*/
    rc = SQLExecDirect (hstmt, createProcStmt, SQL_NTS);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n"))
                goto Exit;

          
        fprintf (stdout, "STEP 2 done...stored procedure created\nExecuting procedure...\n\n");




    /* STEP 3.  Bind the input parameter
    **          Execute the procedure
    **          Bind the result set column (return values)
    **          Fetch the results
    **          Display the results
    **          Close the result set cursor
    */

    /* Bind the input parameter */
    rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &inputParam, 0, &cbInputParam);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindParameter failed\n"))
                return (1);

    /* Execute the procedure */
    rc = SQLExecDirect (hstmt, (SQLCHAR *) "{call multiReturnProc (?)}", SQL_NTS);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed\n"))
            goto Exit;

    /* Bind the result set column */
    rc = SQLBindCol (hstmt, 1, SQL_C_SLONG, (SQLPOINTER)&retVal, 0, &cbRetVal);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed\n"))
                goto Exit;

    /* Fetch and display the results */
    while (1)
    {
            rc = SQLFetch (hstmt);
               if (rc == SQL_NO_DATA_FOUND)
                break;
        else if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLFetch failed\n"))
                    goto Exit;

        /* Display the results */
        fprintf (stdout, "Procedure returned %d\n", retVal);
    }

    /* Close the result set cursor */
    rc = SQLCloseCursor (hstmt);
    if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLCloseCursor failed\n"))
                goto Exit;


        fprintf (stdout, "\nSTEP 3 done...stored procedure executed\n");



    Exit:

    /* CLEANUP: Drop the stored procedure from the database
    **          Close the statement handle
    **          Free the statement handle
    **          Disconnect from the datasource
    **          Free the connection and environment handles
    **          Exit
    */

           /* Drop the stored procedure from the database */
    SQLExecDirect (hstmt, dropProcStmt, SQL_NTS);

    /* Close the statement handle */
    SQLFreeStmt (hstmt, SQL_CLOSE);

    /* Free the statement handle */
    SQLFreeHandle (SQL_HANDLE_STMT, hstmt);

        /* Disconnect from the data source */
    SQLDisconnect (hdbc);

    /* Free the environment handle and the database connection handle */
    SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
    SQLFreeHandle (SQL_HANDLE_ENV, henv);

    fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
    in = getchar ();
    return (rc);
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP