exec_single

NAME

exec_single(): Executes a given query.

TYPE

Procedure

DESCRIPTION

Given a query, this procedure executes it. Essentially, is uses dynamic SQL to invoke the query.

Query may be of any valid type:

  • DML (e.g. INSERT, UPDATE, ...)
  • DDL (e.g. CREATE, ALTER, ...)
  • Other (e.g. KILL, SHOW, ...)
Refer to the MySQL Manual for complete listing of valid statements.

The procedure is used as the underlying execution mechanism for other common_schema routines. It's main advantage is that it accepts input configuration (see following). Users will often not use this routine directly.

Invoker of this procedure must have the privileges required for execution of given query.

SYNOPSIS

exec_single(IN execute_query TEXT CHARSET utf8) 
  MODIFIES SQL DATA

Input:

  • execute_query: a single query to execute. This can be DML, DDL or any other valid MySQL command. The procedure will not execute nor change anything when this input is empty (blank space). The query may be terminated by a semicolon, but does not have to. A semicolon may appear within query (e.g. in quoted text).
Input config:
  • @common_schema_dryrun: when 1, query is not executed, but rather printed.
  • @common_schema_verbose: when 1, query is verbosed.
Output:
  • Whatever output the query may have.
  • @common_schema_rowcount: number of rows affected by execution.

EXAMPLES

Use exec_single() to create a table:

mysql> SHOW TABLES FROM world;
+-----------------+
| Tables_in_world |
+-----------------+
| City            |
| Country         |
| CountryLanguage |
+-----------------+

mysql> CALL exec_single('CREATE TABLE world.Region (id INT)');

mysql> SHOW TABLES FROM world;
+-----------------+
| Tables_in_world |
+-----------------+
| City            |
| Country         |
| CountryLanguage |
| Region          |
+-----------------+

Do an insert, get resulting number of affected rows:

mysql> CALL exec_single('INSERT INTO world.Region VALUES (1),(2),(3),(4),(5)');
	
mysql> SELECT @common_schema_rowcount;
+-------------------------+
| @common_schema_rowcount |
+-------------------------+
|                       5 |
+-------------------------+

Do a dry run: do not actually execute statement, just print out your intentions:

mysql> SET @common_schema_dryrun := 1;
	
mysql> CALL exec_single('DELETE FROM world.Region WHERE id < 3');
+---------------------------------------+
| exec_single: @common_schema_dryrun    |
+---------------------------------------+
| DELETE FROM world.Region WHERE id < 3 |
+---------------------------------------+

mysql> SELECT COUNT(*) FROM world.Region;
+----------+
| COUNT(*) |
+----------+
|        5 |
+----------+

ENVIRONMENT

MySQL 5.1 or newer

SEE ALSO

eval(), exec(), foreach(), repeat_exec()

AUTHOR

Shlomi Noach
 
common_schema documentation