vendredi 30 juillet 2021

Issue reading inserted value from BIGINT column in Microsoft SQL server database from c++ application

I am trying to get the inserted ID in a BIGINT column from a query I execute from c++ application, the target database is Microsoft SQL server, and when I try to get the output ID value all I get is a weird value (2714419332062) and when check in the database the inserted values are '1', '2'... etc.

SQLHANDLE sqlStmtHandle = ReadFromDB(L"INSERT INTO [MyTable] ([Value])
                                       OUTPUT INSERTED.[ID]
                                       VALUES('TEST')");

SQLBIGINT insertID;
SQLLEN lenInsertID = 0;
SQLRETURN returnData;

SQLBindCol(sqlStmtHandle, 1, SQL_C_LONG, &insertID, 2, &lenInsertID);
returnData = SQLFetch(sqlStmtHandle);
if (returnData == SQL_SUCCESS || returnData == SQL_SUCCESS_WITH_INFO)
    do
    {
        cout << insertID << endl;
    } while (SQLFetch(sqlStmtHandle) != SQL_NO_DATA);

SQLFreeHandle(SQL_HANDLE_STMT, sqlStmtHandle);

Schema in SQL would be:

CREATE TABLE [MyTable] (
    [ID]        BIGINT  NOT NULL IDENTITY(1,1)
    ,[Value]    VARCHAR(512) NOT NULL)
GO

As a side note this code works if column ID is INT and I use SQLINTEGER for insertID. Also, if there is any small bug in the code (this is a VERY simplified version of the original code)

Aucun commentaire:

Enregistrer un commentaire