--- cdr/cdr_odbc.c	2007-12-27 11:46:08.000000000 -0600
+++ cdr/cdr_odbc.add	2007-12-27 11:27:00.000000000 -0600
@@ -77,11 +77,34 @@
 
 static int odbc_do_query(void);
 static int odbc_init(void);
+static void extract_error(char *, SQLSMALLINT, SQLHANDLE);
 
 static SQLHENV	ODBC_env = SQL_NULL_HANDLE;	/* global ODBC Environment */
 static SQLHDBC	ODBC_con;			/* global ODBC Connection Handle */
 static SQLHSTMT	ODBC_stmt;			/* global ODBC Statement Handle */
 
+static void extract_error(
+      char *fn,
+      SQLSMALLINT type,
+      SQLHANDLE handle)
+  {
+	SQLINTEGER i = 0;
+	SQLINTEGER native;
+	SQLCHAR state[ 7 ];
+	SQLCHAR text[256];
+	SQLSMALLINT len;
+	SQLRETURN ret;
+
+	do
+	{
+		ret = SQLGetDiagRec(type, handle, ++i, state, &native, text,
+		sizeof(text), &len );
+		if (SQL_SUCCEEDED(ret))
+		ast_log(LOG_WARNING,"cdr_odbc:%s: %s:%ld:%ld:%s\n", fn, state, i, native, text);
+	}
+	while( ret == SQL_SUCCESS );
+  }
+
 static void odbc_disconnect(void)
 {
 	SQLDisconnect(ODBC_con);
@@ -129,8 +152,10 @@
 	ODBC_res = SQLAllocHandle(SQL_HANDLE_STMT, ODBC_con, &ODBC_stmt);
 
 	if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
-		if (option_verbose > 10)
+		if (option_verbose > 10){
 			ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Failure in AllocStatement %d\n", ODBC_res);
+			extract_error( "odbc_log", SQL_HANDLE_STMT, ODBC_stmt);
+		}
 		SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
 		odbc_disconnect();
 		ast_mutex_unlock(&odbc_lock);
@@ -144,8 +169,10 @@
 	ODBC_res = SQLPrepare(ODBC_stmt, (unsigned char *)sqlcmd, SQL_NTS);
 	
 	if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
-		if (option_verbose > 10)
+		if (option_verbose > 10){
 			ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error in PREPARE %d\n", ODBC_res);
+			extract_error( "odbc_log", SQL_HANDLE_STMT, ODBC_stmt);
+		}
 		SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
 		odbc_disconnect();
 		ast_mutex_unlock(&odbc_lock);
@@ -378,8 +405,10 @@
 	ODBC_res = SQLExecute(ODBC_stmt);
 	
 	if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
-		if (option_verbose > 10)
-			ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error in Query %d\n", ODBC_res);
+		if (option_verbose > 10){
+			ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error result from Query is %d\n", ODBC_res);
+			extract_error( "odbc_do_query", SQL_HANDLE_STMT, ODBC_stmt);
+		}
 		SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
 		odbc_disconnect();
 		return -1;
@@ -398,8 +427,10 @@
 	if (ODBC_env == SQL_NULL_HANDLE || connected == 0) {
 		ODBC_res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &ODBC_env);
 		if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
-			if (option_verbose > 10)
+			if (option_verbose > 10){
 				ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error AllocHandle\n");
+				extract_error( "odbc_init", SQL_HANDLE_ENV, ODBC_env);
+			}
 			connected = 0;
 			return -1;
 		}
@@ -407,8 +438,10 @@
 		ODBC_res = SQLSetEnvAttr(ODBC_env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
 
 		if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
-			if (option_verbose > 10)
+			if (option_verbose > 10){
 				ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error SetEnv\n");
+				extract_error( "odbc_init", SQL_HANDLE_ENV, ODBC_env);
+			}
 			SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
 			connected = 0;
 			return -1;
@@ -417,8 +450,10 @@
 		ODBC_res = SQLAllocHandle(SQL_HANDLE_DBC, ODBC_env, &ODBC_con);
 
 		if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
-			if (option_verbose > 10)
+			if (option_verbose > 10){
 				ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error AllocHDB %d\n", ODBC_res);
+				extract_error( "odbc_init", SQL_HANDLE_DBC, ODBC_con);
+			}
 			SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
 			connected = 0;
 			return -1;
@@ -431,8 +466,10 @@
 	ODBC_res = SQLConnect(ODBC_con, (SQLCHAR*)dsn, SQL_NTS, (SQLCHAR*)username, SQL_NTS, (SQLCHAR*)password, SQL_NTS);
 
 	if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
-		if (option_verbose > 10)
+		if (option_verbose > 10){
 			ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Error SQLConnect %d\n", ODBC_res);
+			extract_error( "odbc_init", SQL_HANDLE_DBC, ODBC_con);
+		}
 		SQLFreeHandle(SQL_HANDLE_DBC, ODBC_con);
 		SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
 		connected = 0;
