diff --git a/mysql-test/r/sp.result b/mysql-test/r/sp.result
index 1fd519bc7291294121706e51127ab403f8ade8b4..5640057bf35969c376f42108ee6a00e518a22305 100644
--- a/mysql-test/r/sp.result
+++ b/mysql-test/r/sp.result
@@ -2321,3 +2321,16 @@ select `foo` ();
 `foo` ()
 5
 drop function `foo`;
+drop procedure if exists p1;
+create table t1(id int);
+insert into t1 values(1);
+create procedure p1()
+begin
+declare i int;
+select max(id)+1 into i from t1;
+end
+//
+call p1()//
+call p1()//
+drop procedure p1;
+drop table t1;
diff --git a/mysql-test/t/sp.test b/mysql-test/t/sp.test
index 4f556e34d514e5586220ff0b1d294f056ff0e34f..4c5665aae6248f0ab724cb881124c3b586327a71 100644
--- a/mysql-test/t/sp.test
+++ b/mysql-test/t/sp.test
@@ -2739,3 +2739,24 @@ drop function if exists foo;
 create function `foo` () returns int return 5;
 select `foo` ();
 drop function `foo`;
+
+#
+# rolling back temporary Item tree changes in SP
+#
+--disable_warnings
+drop procedure if exists p1;
+--enable_warnings
+create table t1(id int);
+insert into t1 values(1);
+delimiter //;
+create procedure p1()
+begin
+  declare i int;
+  select max(id)+1 into i from t1;
+end
+//
+call p1()//
+call p1()//
+delimiter ;//
+drop procedure p1;
+drop table t1;
diff --git a/sql/sp_head.cc b/sql/sp_head.cc
index 3f2969768c549d670f1b922eecf63a212c40cfa6..dc3b2857993ab5bc68e96c36d4ba85679692d475 100644
--- a/sql/sp_head.cc
+++ b/sql/sp_head.cc
@@ -482,6 +482,7 @@ sp_head::execute(THD *thd)
       break;
     DBUG_PRINT("execute", ("Instruction %u", ip));
     ret= i->execute(thd, &ip);
+    thd->rollback_item_tree_changes();
     if (i->free_list)
       cleanup_items(i->free_list);
     // Check if an exception has occurred and a handler has been found