Skip to content

Multiple statements in one prepared statement #1490

@notthelewis

Description

@notthelewis

Using a prepared statement, the .execute call, with multiple statements is giving me a syntax error:

async function examplePreparedMultiStmt() {
    let cnx;
    try {
        cnx = await mysql.createConnection({multipleStatements: true, user: '', host: '', port: '', password: '', database: ''});
        let [ res, fields ] = await cnx.execute(`SELECT 1 as ?; SELECT 2 as ?`, ['one', 'two'], (e, res, fields )=> {
            if (e) { throw e; } 
            return [ res, fields ];
        });
    
        return [ res, fields ];
    
    } catch (e) {
        console.log(e);
        throw e;
    }
}

Specifically:

  code: 'ER_PARSE_ERROR',
  errno: 1064,
  sql: 'SELECT 1 as one; SELECT 2 as two',
  sqlState: '42000',
  sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT 2 as two' at line 1"

But the same code, using the .query call instead of the .execute call, works perfectly fine:

async function examplePreparedMultiQuery() {
    let cnx;
    try {
        cnx = await mysql.createConnection({multipleStatements: true, user: '', host: '', port: '', password: '', database: ''});
        let [ res, fields ] = cnx.query('SELECT 1 AS ?; SELECT 2 AS ?', ['one', 'two'], (e, res, fields)=> {
            if (e) { throw e; } 
            return [ res, fields ];
        });

        return [ res, fields ];
    } catch(e) {
        console.log(e);
        throw e;
    }
}

Please could someone let me know if this is just me being stupid or if this is a genuine issue?

Is there a fundamental difference between the .execute and the .query calls to a connection if the query call can still be 'prepared' by passing in parameters? I was using the .execute as this is the example provided by the docs on both NPM and Github when regarding prepared statements.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions