Home » Developer & Programmer » Precompilers, OCI & OCCI » Returning value from a select count() statement
Returning value from a select count() statement [message #231322] Mon, 16 April 2007 09:08 Go to next message
clancypc
Messages: 35
Registered: December 2006
Member
I am trying to find the number of rows that will be returned by a query, so I have tried the code below, however the value is always returned as zero, when I know that there is a record in the database. Any ideas on what I am doing wrong?

    
strcpy(QueryString, "SELECT count(*) FROM ACC_INF WHERE acc_number = ':AccNum'\0");

    /* Attach sql statement to statement handle */
    TRACE(("1102: Preparing statement handle\n"));
    OCIStmtPrepare( OraStmntHandlePtr,
            OraErrorHandlePtr,
            &QueryString,
            strlen(QueryString),
            OCI_NTV_SYNTAX,
            OCI_DEFAULT );

    /* Bind the input variable */
    TRACE(("1103: Binding the input variable\n"));
    RtrnValue = OCIBindByName( OraStmntHandlePtr,
        &OraBindAccNumPtr,
        OraErrorHandlePtr,
        ":AccNum",
        -1,
        AccRec->AccNum,
        sizeof(AccRec->AccNum),
        SQLT_STR,
        NULL, NULL, NULL, NULL, NULL,
        OCI_DEFAULT);

    /* Define output variable */
    RtrnValue = OCIDefineByPos( OraStmntHandlePtr,
            &OraDefPtr[0],
            OraErrorHandlePtr,
            1,
            &NumRecords,        /* destination variable */
            sizeof(NumRecords),
            SQLT_INT,
            (dvoid *) 0, (ub2 *) 0, (ub2 *) 0, OCI_DEFAULT);
    if ( RtrnValue != 0 )
        checkerr( OraErrorHandlePtr, RtrnValue);

    /* Execute statement */
    TRACE(("1104: Executing the statement\n"));
    RtrnValue = OCIStmtExecute(OraServiceContextHandlePtr,
            OraStmntHandlePtr,
            OraErrorHandlePtr,
            1,    /* fetch first row */
            0,
            NULL,
            NULL,
            OCI_DEFAULT);
    if ( RtrnValue != 0 )
        checkerr( OraErrorHandlePtr, RtrnValue);


Thanks,
Peter
Re: Returning value from a select count() statement [message #231470 is a reply to message #231322] Tue, 17 April 2007 03:49 Go to previous message
clancypc
Messages: 35
Registered: December 2006
Member
OK I can update the issue slightly, I have reduced the select statement to a simple "select count(*) from acc_inf" and that returns 19 into NumRecords, so it must be something in the format of the where statement that is causing it to fail.

Previous Topic: How to Describe Neste Package in OCI Oracle
Next Topic: Evaluate if query successful and row count
Goto Forum:
  


Current Time: Sun Dec 04 18:44:09 CST 2016

Total time taken to generate the page: 0.13016 seconds