I am using the mysql2 library for deno/node to try and run a CREATE query for a TEMPORARY table but results only contain the ResultSetHeader showing a serverStatus of 2. After some research it appears this status code means SERVER_STATUS_AUTOCOMMIT flag is enabled.
The table is never created and I never get a real error..
Here is the code:
// deno run --allow-env --allow-net z.ts
import * as mysql2 from "https://deno.land/x/mysql2/mod.ts";
const pool = mysql2.createPool({
host: "db.ondigitalocean.com",
port: 12345,
user: "devuser",
password: "letmein",
database: "devdb",
connectionLimit: 4,
timezone: "+00:00" // Sync app timezone with MySQL server UTC timezone
});
/* CREATE temp table tblTodos in database memory */
const sqlCREATE1 =
`CREATE TEMPORARY TABLE IF NOT EXISTS tblTodos (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
userId BIGINT UNSIGNED DEFAULT NULL,
CHECK (userId >= 0),
todoCreated TIMESTAMP DEFAULT NULL,
todoModified TIMESTAMP DEFAULT NULL,
todoStatus VARCHAR(16) DEFAULT NULL,
todoTitle VARCHAR(128) DEFAULT NULL,
todoBody VARCHAR(1024) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE = INNODB
AUTO_INCREMENT=2001
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci;`;
const create1Result = await pool.execute(sqlCREATE1);
if (create1Result) {
console.log(create1Result);
}
await pool.end();
This is what create1Results
contains even though I’m not certain this is an error.
[
ResultSetHeader {
fieldCount: 0,
infileName: undefined,
affectedRows: 0,
insertId: 0,
info: "",
serverStatus: 2,
warningStatus: 0,
stateChanges: undefined,
changedRows: undefined
},
undefined
]
2
Answers
Example of desired outcome based on Bill's answer here for completion.
Console Output:
Code:
Temporary tables exist only in the same session where they were created, and the table is automatically removed when the session ends.
I’m not a Node.js developer, but I notice in the documented examples:
So your temporary table is dropped almost immediately after it is created.
You need to get a connection and use that connection for creating the temporary table, and run all subsequent queries that reference that temporary table.
Again, an example from the documentation: