Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wm3r0-00042L-EU for pgsql-odbc@arkaria.postgresql.org; Sun, 18 May 2014 16:22:26 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Wm3qy-0007qG-63 for pgsql-odbc@arkaria.postgresql.org; Sun, 18 May 2014 16:22:24 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wm3qx-0007q9-BI for pgsql-odbc@postgresql.org; Sun, 18 May 2014 16:22:23 +0000 Received: from dub0-omc4-s33.dub0.hotmail.com ([157.55.2.108]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wm3qq-0004kJ-5h for pgsql-odbc@postgresql.org; Sun, 18 May 2014 16:22:22 +0000 Received: from DUB121-W27 ([157.55.2.71]) by dub0-omc4-s33.dub0.hotmail.com with Microsoft SMTPSVC(6.0.3790.4675); Sun, 18 May 2014 09:22:15 -0700 X-TMN: [+Rb+F5Yub3Ked7X1T+V98RLjOzeAsRyo] X-Originating-Email: [nukemd@hotmail.com] Message-ID: From: John Smith To: "pgsql-odbc@postgresql.org" Subject: Problems using SQLFetch on prepared INSERT with RETURNING clause Date: Sun, 18 May 2014 18:22:15 +0200 Importance: Normal Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginalArrivalTime: 18 May 2014 16:22:15.0471 (UTC) FILETIME=[54D1B3F0:01CF72B5] X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-odbc Precedence: bulk Sender: pgsql-odbc-owner@postgresql.org Hi, I am having some difficulties with a prepared INSERT statement with a retur= ning clause. I made a small example that reproduces the problem: One database:=A0CREATE DATABASE my_test_db One table:=A0CREATE TABLE my_test_table(mycol integer) The query I am attempting to use:=A0INSERT INTO my_test_table VALUES(?) RET= URNING xmin::text::bigint First i use SQLPrepare to prepare my insert statement. After this, I use SQ= LBindCol to bind a 64-bit integer to the result that would be xmin. Then I = use SQLBindParam to bind a 32 bit integer variable for the input to the col= umn. Then I call SQLExecute and SQLFetch. The first time it works fine, and xmin= is returned into the bound variable. Then I call SQLMoreResults until all = results are consumed. After this I call SQLExecute and SQLFetch again, and here I would expect an= other row to be inserted, and the xmin for that row to be returned on SQLFe= tch. The new row is indeed inserted, but SQLFetch fails with SQLSTATE 07005, nat= ive error 15 and message "Null statement result in PGAPI_ExtendedFetch." I am using the PostgreSQL Unicode(x64) driver version on Windows 7. I tried= versions 9.03.02.10 and the just released 9.03.03.00. Any ideas? I'll paste my complete test program (C++, uses boost unit test) = below. //--------------------------------------------------------------------- #include #include namespace { =A0 =A0SQLHENV henv =3D NULL; =A0 =A0SQLHDBC hdbc =3D NULL; =A0 =A0SQLHANDLE hstmt =3D NULL; =A0 =A0SQLWCHAR sqlstate[25]; =A0 =A0SQLWCHAR message[255]; =A0 =A0SQLINTEGER native_error; =A0 =A0//This test program will create a database named my_test_db with one= table, my_test_table, containing a single integer column named mycol. =A0 =A0SQLWCHAR crete_db_sql[] =3D L"CREATE DATABASE my_test_db"; =A0 =A0SQLWCHAR create_table_sql[] =3D L"CREATE TABLE my_test_table(mycol i= nteger)"; =A0 =A0//It will attempt to prepare a query that inserts an integer, and re= turns the xmin value as a bigint. =A0 =A0//This will work fine the first time, but subsequent calls to SQLExe= c will fail on the SQLFetch that should fetch xmin. =A0 =A0SQLWCHAR insert_sql[] =3D L"INSERT INTO my_test_table VALUES(?) RETU= RNING xmin::text::bigint"; =A0 =A0SQLINTEGER input =3D 0; =A0 =A0SQLBIGINT xmin_output =3D 0; =A0 =A0std::wstring get_connection_string(const std::wstring& dbname) =A0 =A0{ =A0 =A0 =A0 return L"Driver=3D{PostgreSQL Unicode(x64)};Server=3D127.0.0.1;= Port=3D5432;Database=3D" + dbname + L";Uid=3Dpostgres;Pwd=3Dpassword;"; =A0 =A0} } #define ODBC_CHECK(x) if (!SQL_SUCCEEDED(x)) { \ =A0 =A0BOOST_CHECK_IMPL(false, BOOST_TEST_STRINGIZE(x), CHECK, CHECK_PRED);= \ =A0 =A0SQLGetDiagRec(SQL_HANDLE_STMT, hstmt, 1, sqlstate, &native_error, me= ssage, 255, nullptr); \ =A0 =A0std::wcerr << L"SQLSTATE: " << sqlstate << L" NATIVE ERROR: " << nat= ive_error << std::endl << L"MESSAGE: " << message << std::endl; } BOOST_AUTO_TEST_CASE( aaa_postgres_odbc ) { =A0 =A0//------Boring setup code that creates the database etc. =A0 =A0BOOST_CHECK(SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HA= NDLE, &henv))); =A0 =A0BOOST_CHECK(SQL_SUCCEEDED(SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, =A0 =A0 =A0 reinterpret_cast(SQL_OV_ODBC3), 0))); =A0 =A0BOOST_CHECK(SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc= ))); =A0 =A0BOOST_CHECK(SQL_SUCCEEDED(SQLDriverConnect(hdbc, NULL, const_cast(get_connection_string(L"postgres").c_str()), =A0 =A0 =A0 SQL_NTS, nullptr, 0, nullptr, SQL_DRIVER_NOPROMPT))); =A0 =A0BOOST_CHECK(SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hst= mt))); =A0 =A0ODBC_CHECK(SQLExecDirect(hstmt, crete_db_sql, SQL_NTS)); =A0 =A0BOOST_CHECK(SQL_SUCCEEDED(SQLFreeHandle(SQL_HANDLE_STMT, hstmt))); =A0 =A0BOOST_CHECK(SQL_SUCCEEDED(SQLDisconnect(hdbc))); =A0 =A0//------Database should now be set up, some more boring code to crea= te our table goes here =A0 =A0BOOST_CHECK(SQL_SUCCEEDED(SQLDriverConnect(hdbc, NULL, const_cast(get_connection_string(L"my_test_db").c_str()), =A0 =A0 =A0 SQL_NTS, nullptr, 0, nullptr, SQL_DRIVER_NOPROMPT))); =A0 =A0BOOST_CHECK(SQL_SUCCEEDED(SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hst= mt))); =A0 =A0ODBC_CHECK(SQLExecDirect(hstmt, create_table_sql, SQL_NTS)); =A0 =A0ODBC_CHECK(SQLPrepare(hstmt, insert_sql, SQL_NTS)); =A0 =A0 =A0 =A0ODBC_CHECK(SQLBindCol(hstmt, 1, SQL_C_SBIGINT, &xmin_output, 0, null= ptr)); =A0 =A0ODBC_CHECK(SQLBindParam(hstmt, 1, SQL_C_SLONG, SQL_PARAM_INPUT, 0, 0= , &input, nullptr)); =A0 =A0//Execute first insert =A0 =A0++input; =A0 =A0ODBC_CHECK(SQLExecute(hstmt)); =A0 =A0ODBC_CHECK(SQLFetch(hstmt)); =A0 =A0BOOST_CHECK(xmin_output> 0); =A0 =A0while (SQL_SUCCEEDED(SQLMoreResults(hstmt))); =A0 =A0//Execute second insert =A0 =A0++input; =A0 =A0ODBC_CHECK(SQLExecute(hstmt)); =A0 =A0ODBC_CHECK(SQLFetch(hstmt)); =A0//This is where it fails =A0 =A0BOOST_CHECK(xmin_output> 0); =A0 =A0BOOST_CHECK(SQL_SUCCEEDED(SQLFreeHandle(SQL_HANDLE_STMT, hstmt))); =A0 =A0BOOST_CHECK(SQL_SUCCEEDED(SQLDisconnect(hdbc))); =A0 =A0BOOST_CHECK(SQL_SUCCEEDED(SQLFreeHandle(SQL_HANDLE_ENV, henv))); }=20=09=09=20=09=20=20=20=09=09=20=20 --=20 Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc