I’m calling a stored procedure that returns input parameters but when I have a SQL_VARCHAR that is over 31 characters I get junk characters past the 31st character. Sometimes it’s actually variables values that exist other places in my program which leads me to believe the bind isn’t allocating the proper memory space for the input parameters returned by the stored procedure. The odd thing is that if in my bind I use SQL_CHAR then the correct value displays except for 1 extra junk character at the very end. However, when I do a SQLDescribeCol on the stored procedure the DataTypePtr says it’s SQL_VARCHAR. Has anyone had problems with the SQLBindCol not allocating the appropriate memory space?
Below is the pertinent lines of my code and attached is a screenshot from testing the proc in the test tool.
In the screenshot the address is supposed to be:
219 7th Avenue, SE_ WWWWWWWWWWWWWWWWWWWW
set returnCode [odbc SQLAllocHandle SQL_HANDLE_ENV SQL_NULL_HANDLE henv]
echo “Create env handle: $returnCode”
set returnCode [odbc SQLSetEnvAttr $henv SQL_ATTR_ODBC_VERSION SQL_OV_ODBC3 0]
echo “Set environment: $returnCode”
set returnCode [odbc SQLAllocHandle SQL_HANDLE_DBC $henv hdbc]
echo “Create db handle: $returnCode”
set returnCode [odbc SQLConnect $hdbc DATAS_NAME SQL_NTS USERN SQL_NTS passw SQL_NTS]
echo “Connect to DB: $returnCode”
#set returnCode [odbc SQLConnect $hdbc DATAS_NAME SQL_NTS]
#echo “Connect to DB: $returnCode”
set returnCode [odbc SQLAllocHandle SQL_HANDLE_STMT $hdbc hstmt]
echo “Create stored procedure handle: $returnCode”
set rCode [odbc SQLBindCol $hstmt 18 SQL_INTEGER pat_age_in_years 2 pcbValue1]
echo “bind=$rCode”
set rcode [odbc SQLBindCol $hstmt 19 SQL_VARCHAR pat_addr_line 40 pcbValue19]
set rcode [odbc SQLBindCol $hstmt 20 SQL_VARCHAR pat_addr_line2 40 pcbValue20]
set storedproc “exec crdsp_get_patient_demog_r @pat_nbr=”12345678″, @pat_ref_code=”MC””
echo $storedproc
set returnCode [odbc SQLPrepare $hstmt $storedproc SQL_NTS]
echo “Preparing stored procedure: $returnCode”
set returnCode [odbc SQLExecute $hstmt]
echo “Executing stored procedure: $returnCode”
set x 0
###Fetch the columns that you bound to variables using SQLBindCol
### From only the first row of the result set
set returnCode [odbc SQLFetch $hstmt]
set rcode [odbc SQLDescribeCol $hstmt 19 address 40 NameLengthPtr DataTypePtr ColumnSizePtr DecimalDigitsPtr NullablePtr]
echo $NameLengthPtr
echo $DataTypePtr
echo $ColumnSizePtr
###Loop through the rest of the rows of the result set
while {$returnCode == “SQL_SUCCESS” || $returnCode == “SQL_SUCCESS_WITH_INFO”} {
echo “pat_age_in_years: $pat_age_in_years”
echo “pat_addr_line1: $pat_addr_line”
echo “pat_addr_line2: $pat_addr_line2”
echo “============ End Record ==============”
set returnCode [odbc SQLFetch $hstmt]
incr x
}
echo “Returned $x rows”
###Clean up statement handle
odbc SQLFreeHandle SQL_HANDLE_STMT $hstmt
###Disconnect from the database
odbc SQLDisconnect $hdbc
###Clean up the database, and environment handles
odbc SQLFreeHandle SQL_HANDLE_DBC $hdbc
odbc SQLFreeHandle SQL_HANDLE_ENV $henv