Thanks to visit codestin.com
Credit goes to github.com

Skip to content

error when statement.execute(true) against temp table on mssql #1263

@solarobot

Description

@solarobot

HI,
I created a temp table on mssql and want to execute insert on this table,

// Step 1: Create a temporary table based on the database type
if (m_dbType == DBType::MSSQL) {
	// SQL Server: Create a local temporary table
	*m_sqlSession << "CREATE TABLE #TempOids (oid uniqueidentifier PRIMARY KEY)";
}
else if (m_dbType == DBType::ORACLE) {
	// Oracle: Create a global temporary table (if not already created)
	// Note: This table should be created once during schema setup, not in every call
	// For testing, we'll drop and recreate it, but in production, this should be pre-created
	try {
		*m_sqlSession << "DROP TABLE TempOids CASCADE CONSTRAINTS";
	}
	catch (const soci::soci_error& e) {
		// Ignore if table doesn't exist
	}
	*m_sqlSession << "CREATE GLOBAL TEMPORARY TABLE TempOids (oid VARCHAR2(50) PRIMARY KEY) ON COMMIT DELETE ROWS";
}

// Step 2: Batch insert oids into the temporary table
std::string insertQuery;
if (m_dbType == DBType::MSSQL) {
	insertQuery = "INSERT INTO #TempOids (oid) VALUES (:oid)";
}
else { // Oracle
	insertQuery = "INSERT INTO TempOids (oid) VALUES (:oid)";
}

soci::statement insertStmt = (m_sqlSession->prepare << insertQuery, soci::use(oids));
insertStmt.execute(true);

While insertStmt.execute(true) throws exception,

Image

Any guide is greatly appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions