Outdated Version

You are viewing an older version of this section. View current production version.


Executes a stored procedure and returns a set of rows as a result.


ECHO { procedure_name( [argument_list] ) 
       | database_name.procedure_name( [argument_list] ) }; 



The name of the stored procedure to execute.


A list of optional arguments to pass as input parameters to the stored procedure.


You cannot use ECHO to execute a stored procedure that does not return a value, or a stored procedure that returns an ARRAY or RECORD value. If the results of a stored procedure is a scalar value, the result of executing that stored procedure with ECHO will be a single row with the column name RESULT.

delimiter //
create procedure return_scalar() returns int as
    return 2;
end //
delimiter ;

memsql> echo return_scalar();
|      2 |
1 row in set

The behavior is different when the stored procedure returns a query type variable. Consider the following table and stored procedure:

create table t(a int, b varchar(30));
insert t values(1, "red"), (2, "green"), (3, "blue");

delimiter //
create procedure p(x int) returns query(a int, b varchar(30)) as
  q query(a int, b varchar(30)) =
    select a, b
    from t
    where a >= x;
  return q;
end //
delimiter ;

You can use the ECHO command to call this procedure, evaluate the query it returns, and output the resulting rows, as follows:

memsql> echo p(2);
| a    | b     |
|    3 | blue  |
|    2 | green |


Stored procedures use a late binding approach when returning sets of rows. That is, a query object with parameter bindings is returned by a stored procedure, not a materialized row set. To get the set of rows for the query returned by a stored procedure, you must run the procedure using ECHO.

You can consume the results of an ECHO statement from a client application program just like you consume the results of a SELECT statement.

Consume Output of Echo in a Python Application

A stored procedure may optionally return a QUERY value which evaluates to a set of rows. You can call a stored procedure from an application program and consume the set of rows produced by the query using the ECHO statement to run the procedure.


As an example, first run the following SQL statements:

-- Setup for Python ECHO example.
use information_schema;
drop database if exists test;
create database test;
use test;

create table if not exists tbl(id int, value varchar(30));

insert into tbl values(1, "red");
insert into tbl values(2, "green");
insert into tbl values(3, "blue");

delimiter //
create or replace procedure p(x int) returns query(id int, value varchar(30)) as
    r query(id int, value varchar(30)) = 
          select id, value from tbl 
          where id >= x;
    return r;
delimiter ;

Python Application

Then, make sure the MemSQL Python library is installed (https://github.com/memsql/memsql-python).

Now, run:

from memsql.common import database

# Specify connection information for MemSQL
HOST = ""
PORT = 3306
USER = "root"

# Specify which database to work with.
DATABASE = "test"

def get_connection(db=DATABASE):
    """ Returns a new connection to the database. """
    return database.connect(
        host=HOST, port=PORT, user=USER, password=PASSWORD, database=db)

def test_echo(x):
    with get_connection(db="test") as conn:
        query = 'echo p(%s)' % (x);
        for r in conn.query(query):
            print "id = %s, value = %s" % (r.id, r.value)

Now, test_echo can be called from the Python command line to display rows retrieved using ECHO. For example:


The following output is produced:

>>> test_echo(2);
id = 2, value = green
id = 3, value = blue

Related Topics

The CALL command is similar to ECHO but does not produce a set of rows as a result. Any query value returned is ignored when using CALL.