node-mysql2 icon indicating copy to clipboard operation
node-mysql2 copied to clipboard

Calling procedures with OUT parameters

Open ssatorres opened this issue 11 years ago • 2 comments

When I try to call a procedure with prepared statement

    var query = 'CALL sp_signup(:user_name, :user_email, :environment_name, :sector_id, :language, @p_status, @p_msg);';
    query += 'SELECT @p_status, @p_msg;';
    var param = {user_name: 'John Doe',
                         user_email: '[email protected]',
                         environment_name: 'Test',
                         sector_id: '1',
                         language: 'pt-BR' };
    connection.config.namedPlaceholders = true;
    connection.execute(query, param, function(err, res) {
    ...

This is de return message:

{ [Error: 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 @p_status, @p_msg' at line 1] code: 'ER_PARSE_ERROR', sqlState: '#42000' }

Why can I do?

ssatorres avatar Feb 07 '15 01:02 ssatorres

Can you try to do two calls using two execute commands? E.e

var query = 'CALL sp_signup(:user_name, :user_email, :environment_name, :sector_id, :language, @p_status, @p_msg)';
    var param = {user_name: 'John Doe',
                         user_email: '[email protected]',
                         environment_name: 'Test',
                         sector_id: '1',
                         language: 'pt-BR' };
    connection.config.namedPlaceholders = true;
    connection.execute(query, param, function(err, res) {
       connection.execute('SELECT @p_status, @p_msg', function() { ... });
    })

prepared statements don't support multiple statements (but can have multiple results in response). There are some bugs there yet I'm currently fixing ( #27, #37 ) I plan to release it as part of https://github.com/sidorares/node-mysql2/milestones/0.15.1 Until released I would not recommend using prepared statements with stored procedures - just use text protocol (e.i query instead of execute )

There might be also some bug in your query or named placeholders replacement. Can you please add console.log(options.sql, options.values) after https://github.com/sidorares/node-mysql2/blob/master/lib/connection.js#L358

sidorares avatar Feb 07 '15 02:02 sidorares

should be closed by #157

sidorares avatar Feb 27 '15 06:02 sidorares