4 drop procedure if exists p1|
5 drop procedure if exists p2|
8 ######################################################################
9 # Test Dynamic SQL in stored procedures. #############################
10 ######################################################################
16 prepare stmt from "select 1";
20 deallocate prepare stmt;
27 # B. Recursion. Recusion is disabled in SP, and recursive use of PS is not
34 prepare stmt from "call p1()"|
35 # Allow SP resursion to be show that it has not influence here
36 set @SAVE_SP_RECURSION_LEVELS=@@max_sp_recursion_depth|
37 set @@max_sp_recursion_depth=100|
38 --error ER_PS_NO_RECURSION
40 --error ER_PS_NO_RECURSION
42 --error ER_PS_NO_RECURSION
44 --error ER_PS_NO_RECURSION
46 --error ER_PS_NO_RECURSION
48 --error ER_PS_NO_RECURSION
50 set @@max_sp_recursion_depth=@SAVE_SP_RECURSION_LEVELS|
51 --error ER_SP_RECURSION_LIMIT
53 --error ER_SP_RECURSION_LIMIT
55 --error ER_SP_RECURSION_LIMIT
60 # C. Create/drop a stored procedure in Dynamic SQL.
61 # One cannot create stored procedure from a stored procedure because of
62 # the way MySQL SP cache works: it's important that this limitation is not
63 # possible to circumvent by means of Dynamic SQL.
67 prepare stmt from "create procedure p2() begin select 1; end";
69 deallocate prepare stmt;
71 --error ER_UNSUPPORTED_PS
73 --error ER_UNSUPPORTED_PS
78 prepare stmt from "drop procedure p2";
80 deallocate prepare stmt;
82 --error ER_UNSUPPORTED_PS
84 --error ER_UNSUPPORTED_PS
88 # D. Create/Drop/Alter a table (a DDL that issues a commit) in Dynamic SQL.
93 prepare stmt_drop from "drop table if exists t1";
95 prepare stmt from "create table t1 (a int)";
97 insert into t1 (a) values (1);
99 prepare stmt_alter from "alter table t1 add (b int)";
101 insert into t1 (a,b) values (2,1);
102 deallocate prepare stmt_alter;
103 deallocate prepare stmt;
104 deallocate prepare stmt_drop;
110 # A more real example (a case similar to submitted by 24/7).
112 create procedure p1()
114 set @tab_name=concat("tab_", replace(curdate(), '-', '_'));
115 set @drop_sql=concat("drop table if exists ", @tab_name);
116 set @create_sql=concat("create table ", @tab_name, " (a int)");
117 set @insert_sql=concat("insert into ", @tab_name, " values (1), (2), (3)");
118 set @select_sql=concat("select * from ", @tab_name);
124 prepare stmt_drop from @drop_sql;
126 prepare stmt from @create_sql;
128 prepare stmt from @insert_sql;
130 prepare stmt from @select_sql;
133 deallocate prepare stmt;
134 deallocate prepare stmt_drop;
142 # E. Calling a stored procedure with Dynamic SQL
143 # from a stored function (currently disabled).
145 create procedure p1()
147 prepare stmt_drop from "drop table if exists t1";
149 prepare stmt from "create table t1 (a int)";
151 deallocate prepare stmt;
152 deallocate prepare stmt_drop;
155 drop function if exists f1|
157 create function f1(a int) returns int
163 # Every stored procedure that contains Dynamic SQL is marked as
164 # such. Stored procedures that contain Dynamic SQL are not
165 # allowed in a stored function or trigger, and here we get the
166 # corresponding error message.
168 --error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
170 --error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
172 --error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
173 select f1(f1(f1(0)))|
177 # F. Rollback and cleanup lists management in Dynamic SQL.
179 create procedure p1()
181 drop table if exists t1;
182 create table t1 (id integer not null primary key,
183 name varchar(20) not null);
184 insert into t1 (id, name) values (1, 'aaa'), (2, 'bbb'), (3, 'ccc');
185 prepare stmt from "select name from t1";
190 "select name from t1 where name=(select name from t1 where id=2)";
192 select name from t1 where name=(select name from t1 where id=2);
199 # H. Executing a statement prepared externally in SP.
201 prepare stmt from "select * from t1"|
202 create procedure p1()
205 deallocate prepare stmt;
208 --error ER_UNKNOWN_STMT_HANDLER
212 # I. Use of an SP variable in Dynamic SQL is not possible and
213 # this limitation is necessary for correct binary logging: prepared
214 # statements do not substitute SP variables with their values for binlog, so
215 # SP variables must be not accessible in Dynamic SQL.
217 create procedure p1()
221 set @a="mysql-variable";
222 prepare stmt from "select 'dynamic sql:', @a, a";
225 --error ER_BAD_FIELD_ERROR
227 --error ER_BAD_FIELD_ERROR
231 # J. Use of placeholders in Dynamic SQL.
233 create procedure p1()
235 prepare stmt from 'select ? as a';
236 execute stmt using @a;
243 # K. Use of continue handlers with Dynamic SQL.
245 drop table if exists t1|
246 drop table if exists t2|
247 create table t1 (id integer primary key auto_increment,
248 stmt_text char(35), status varchar(20))|
249 insert into t1 (stmt_text) values
250 ("select 1"), ("flush tables"), ("handler t1 open as ha"),
251 ("analyze table t1"), ("check table t1"), ("checksum table t1"),
252 ("check table t1"), ("optimize table t1"), ("repair table t1"),
253 ("describe extended select * from t1"),
254 ("help help"), ("show databases"), ("show tables"),
255 ("show table status"), ("show open tables"), ("show storage engines"),
256 ("insert into t1 (id) values (1)"), ("update t1 set status=''"),
257 ("delete from t1"), ("truncate t1"), ("call p1()"), ("foo bar"),
258 ("create view v1 as select 1"), ("alter view v1 as select 2"),
259 ("drop view v1"),("create table t2 (a int)"),("alter table t2 add (b int)"),
261 create procedure p1()
263 declare v_stmt_text varchar(255);
264 declare v_id integer;
265 declare done int default 0;
266 declare c cursor for select id, stmt_text from t1;
267 declare continue handler for 1295 -- ER_UNSUPPORTED_PS
268 set @status='not supported';
269 declare continue handler for 1064 -- ER_SYNTAX_ERROR
270 set @status='syntax error';
271 declare continue handler for sqlstate '02000' set done = 1;
273 prepare update_stmt from "update t1 set status=? where id=?";
277 fetch c into v_id, v_stmt_text;
278 set @id=v_id, @stmt_text=v_stmt_text;
279 set @status="supported";
280 prepare stmt from @stmt_text;
281 execute update_stmt using @status, @id;
283 until done end repeat;
284 deallocate prepare update_stmt;
291 # Bug#7115 "Prepared Statements: packet error if execution within stored
294 prepare stmt from 'select 1'|
295 create procedure p1() execute stmt|
300 # Bug#10975 "Prepared statements: crash if function deallocates"
301 # Check that a prepared statement that is currently in use
302 # can't be deallocated.
304 # a) Prepared statements and stored procedure cache:
306 # TODO: add when the corresponding bug (Bug #12093 "SP not found on second
307 # PS execution if another thread drops other SP in between") is fixed.
309 # b) attempt to deallocate a prepared statement that is being executed
310 --error ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG
311 create function f1() returns int
313 deallocate prepare stmt;
317 # b)-2 a crash (#1) spotted by Sergey Petrunia during code review
318 create procedure p1()
320 prepare stmt from 'select 1 A';
323 prepare stmt from 'call p1()'|
324 --error ER_PS_NO_RECURSION
326 --error ER_PS_NO_RECURSION
331 # Bug#10605 "Stored procedure with multiple SQL prepared statements
332 # disconnects client"
335 drop table if exists t1, t2|
337 create procedure p1 (a int) language sql deterministic
339 declare rsql varchar(100);
340 drop table if exists t1, t2;
341 set @rsql= "create table t1 (a int)";
343 prepare pst from @rsql;
346 set @rsql= "create table t2 (a int)";
348 prepare pst from @rsql;
350 drop table if exists t1, t2;
357 drop procedure if exists p1|