1878.5.4
by Brian Aker
Update documentation. |
1 |
Dynamic SQL
|
1900.2.4
by Stewart Smith
fix more docs warnings: underline/overline too short |
2 |
===========
|
1878.5.4
by Brian Aker
Update documentation. |
3 |
|
1994.4.1
by Marisa Plumb
doc modifications |
4 |
Dynamic SQL enables you to build SQL statements dynamically at runtime. It can be generated within an application or from the system tables and then executed against the database to manipulate the data. You can create more general purpose, flexible applications by using dynamic SQL since the full text of a SQL statement may be unknown at the time of compilation. |
5 |
||
6 |
Dynamic SQL allows you to execute DDL statements (and other SQL statements) that are not supported in purely static SQL programs. |
|
7 |
||
1994.4.119
by Marisa Plumb
doc fixes and additions |
8 |
EXECUTE
|
9 |
--------
|
|
10 |
||
11 |
EXECUTE() is used to generate SQL in a dynamic manner on the server. It can select values inside of the database as variables, and then execute them from statements that are already stored as variables. |
|
12 |
||
13 |
EXECUTE can be CONCURRENT and with NO RETURN. |
|
14 |
||
1945.3.5
by Marisa Plumb
more sql doc modifications |
15 |
In Drizzle you can use the EXECUTE command along with :doc:'user defined variables <variables>' |
1994.4.119
by Marisa Plumb
doc fixes and additions |
16 |
to create SQL in a dynamic manner on the server. An example of this is: |
2165.2.5
by Andrew Hutchings
Make fixes to temporal docs |
17 |
|
18 |
.. code-block:: mysql |
|
1994.4.1
by Marisa Plumb
doc modifications |
19 |
|
20 |
SET @var= "SELECT 1"; |
|
21 |
EXECUTE @var; |
|
22 |
||
2165.2.5
by Andrew Hutchings
Make fixes to temporal docs |
23 |
You can also omit the variable and just insert the SQL directly: |
24 |
||
25 |
.. code-block:: mysql |
|
1994.4.1
by Marisa Plumb
doc modifications |
26 |
|
27 |
EXECUTE "SELECT 1"; |
|
28 |
||
29 |
By adding WITH NO RETURN you can have EXECUTE and no errors will be |
|
1921.4.1
by Brian Aker
Adding in support for EXECUTE to have WITH NO RETURN. |
30 |
generated and no data will be returned by the execution of the statement. |
1921.4.2
by Brian Aker
Adding in concurrent execute support. |
31 |
|
32 |
If you want to launch the query in a separate session, you can do that with |
|
2165.2.5
by Andrew Hutchings
Make fixes to temporal docs |
33 |
the following: |
34 |
||
35 |
.. code-block:: mysql |
|
1921.4.2
by Brian Aker
Adding in concurrent execute support. |
36 |
|
1994.4.1
by Marisa Plumb
doc modifications |
37 |
EXECUTE "SELECT 1" CONCURRENT; |
1958.1.1
by Brian Aker
Fix clash in parser with wait(). |
38 |
|
1921.4.2
by Brian Aker
Adding in concurrent execute support. |
39 |
The query will run in a new session and will execute as the user that |
40 |
launched it. It can be killed via KILL and the system limit on total number |
|
41 |
of sessions will be enforced. |
|
1994.5.29
by Stewart Smith
add FIXME for EXECUTE doing things inside explicit txn |
42 |
|
1994.4.119
by Marisa Plumb
doc fixes and additions |
43 |
EXECUTE also executes the statements inside an explicit transaction. |
44 |
||
45 |
The EXECUTE class has a run() method, which takes a std::string with SQL (to execute). Here is a simple example from the slave plugin code: |
|
46 |
||
47 |
.. code-block:: mysql |
|
48 |
||
49 |
Execute execute(*(_session.get()), true); |
|
50 |
string sql("SELECT `transaction_id` FROM `sys-replication`.`queue`" |
|
51 |
" WHERE `commit_order` IS NOT NULL ORDER BY `commit_order` ASC"); |
|
52 |
sql::ResultSet result_set(1); |
|
53 |
execute.run(sql, result_set); |
|
54 |
||
55 |
In essence, EXECUTE wraps SQL (single or multiple statements) in a transaction and executes it. |