diff --git a/mysql-test/t/distinct.test b/mysql-test/t/distinct.test
index e517380ba9bee6d4213576f89301ae764a5d15a3..b2cc42cc0ff46a30297e0026f6f2821979b07298 100644
--- a/mysql-test/t/distinct.test
+++ b/mysql-test/t/distinct.test
@@ -437,3 +437,70 @@ EXPLAIN SELECT DISTINCT a,a FROM t1 WHERE b < 12 ORDER BY a;
 SELECT DISTINCT a,a FROM t1 WHERE b < 12 ORDER BY a;
 
 DROP TABLE t1;
+# The test case for bug#20836 should be re-enabled when bug#16861 is resolved
+# The results for the test should be the same as in 4.1.
+#
+#Bug #20836: Selecting into variables results in wrong results being returned
+#
+#--disable_warnings
+#DROP TABLE IF EXISTS t1;
+#--enable_warnings
+#
+#CREATE TABLE t1 (id INT NOT NULL, fruit_id INT NOT NULL, fruit_name varchar(20)
+#default NULL);
+#
+#INSERT INTO t1 VALUES (1,1,'ORANGE');
+#INSERT INTO t1 VALUES (2,2,'APPLE');
+#INSERT INTO t1 VALUES (3,2,'APPLE');
+#INSERT INTO t1 VALUES (4,3,'PEAR');
+#
+#SELECT DISTINCT fruit_id, fruit_name INTO @v1, @v2 FROM t1 WHERE fruit_name = 
+#'APPLE';
+#SELECT @v1, @v2;
+#
+#SELECT DISTINCT fruit_id, fruit_name INTO @v3, @v4 FROM t1 GROUP BY fruit_id, 
+#fruit_name HAVING fruit_name = 'APPLE';
+#SELECT @v3, @v4;
+#
+#SELECT DISTINCT @v5:= fruit_id, @v6:= fruit_name INTO @v7, @v8 FROM t1 WHERE 
+#fruit_name = 'APPLE';
+#SELECT @v5, @v6, @v7, @v8;
+#
+#SELECT DISTINCT @v5 + fruit_id, CONCAT(@v6, fruit_name) INTO @v9, @v10 FROM t1 
+#WHERE fruit_name = 'APPLE';
+#SELECT @v5, @v6, @v7, @v8, @v9, @v10;
+#
+#SELECT DISTINCT @v11:= @v5 + fruit_id, @v12:= CONCAT(@v6, fruit_name) INTO 
+#@v13, @v14 FROM t1 WHERE fruit_name = 'APPLE';
+#SELECT @v11, @v12, @v13, @v14;
+#
+#SELECT DISTINCT @v13, @v14 INTO @v15, @v16 FROM t1 WHERE fruit_name = 'APPLE';
+#SELECT @v15, @v16;
+#
+#SELECT DISTINCT 2 + 2, 'Bob' INTO @v17, @v18 FROM t1 WHERE fruit_name = 
+#'APPLE';
+#SELECT @v17, @v18;
+#
+#--disable_warnings
+#DROP TABLE IF EXISTS t2;
+#--enable_warnings
+#
+#CREATE TABLE t2 (fruit_id INT NOT NULL, fruit_name varchar(20)
+#default NULL);
+#
+#SELECT DISTINCT fruit_id, fruit_name INTO OUTFILE 
+#'../tmp/data1.tmp' FROM t1 WHERE fruit_name = 'APPLE';
+#LOAD DATA INFILE '../tmp/data1.tmp' INTO TABLE t2;
+#--exec rm $MYSQL_TEST_DIR/var/tmp/data1.tmp
+#
+#SELECT DISTINCT @v19:= fruit_id, @v20:= fruit_name INTO OUTFILE 
+#'../tmp/data2.tmp' FROM t1 WHERE fruit_name = 'APPLE';
+#LOAD DATA INFILE '../tmp/data2.tmp' INTO TABLE t2;
+#--exec rm $MYSQL_TEST_DIR/var/tmp/data2.tmp
+#
+#SELECT @v19, @v20;
+#SELECT * FROM t2;
+#
+#DROP TABLE t1;
+#DROP TABLE t2;
+
diff --git a/sql/sql_class.cc b/sql/sql_class.cc
index 07510c1fbb094f5832f56fd1e8e46d0b42efb257..679bbf4fcdc2eeee6467bd30d7aeb201f7958fe1 100644
--- a/sql/sql_class.cc
+++ b/sql/sql_class.cc
@@ -1613,53 +1613,21 @@ bool select_exists_subselect::send_data(List<Item> &items)
 
 int select_dumpvar::prepare(List<Item> &list, SELECT_LEX_UNIT *u)
 {
-  List_iterator_fast<Item> li(list);
-  List_iterator_fast<my_var> gl(var_list);
-  Item *item;
-
-  local_vars.empty();				// Clear list if SP
   unit= u;
-  row_count= 0;
-
+  
   if (var_list.elements != list.elements)
   {
     my_message(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT,
                ER(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT), MYF(0));
     return 1;
-  }
-  while ((item=li++))
-  {
-    my_var *mv= gl++;
-    if (mv->local)
-    {
-      Item_splocal *var= new Item_splocal(mv->s, mv->offset, mv->type);
-      (void)local_vars.push_back(var);
-#ifndef DBUG_OFF
-      var->m_sp= mv->sp;
-#endif
-    }
-    else
-    {
-      Item_func_set_user_var *var= new Item_func_set_user_var(mv->s, item);
-      /*
-        Item_func_set_user_var can't substitute something else on its place =>
-        0 can be passed as last argument (reference on item)
-        Item_func_set_user_var can't be fixed after creation, so we do not
-        check var->fixed
-      */
-      var->fix_fields(thd, 0);
-      var->fix_length_and_dec();
-      vars.push_back(var);
-    }
-  }
+  }               
   return 0;
 }
 
 
 void select_dumpvar::cleanup()
 {
-  vars.empty();
-  row_count=0;
+  row_count= 0;
 }
 
 
@@ -1968,13 +1936,10 @@ Statement_map::~Statement_map()
 
 bool select_dumpvar::send_data(List<Item> &items)
 {
-  List_iterator_fast<Item_func_set_user_var> li(vars);
-  List_iterator_fast<Item_splocal> var_li(local_vars);
-  List_iterator_fast<my_var> my_li(var_list);
+  List_iterator_fast<my_var> var_li(var_list);
   List_iterator<Item> it(items);
-  Item_func_set_user_var *xx;
-  Item_splocal *yy;
-  my_var *zz;
+  Item *item;
+  my_var *mv;
   DBUG_ENTER("select_dumpvar::send_data");
 
   if (unit->offset_limit_cnt)
@@ -1987,24 +1952,27 @@ bool select_dumpvar::send_data(List<Item> &items)
     my_message(ER_TOO_MANY_ROWS, ER(ER_TOO_MANY_ROWS), MYF(0));
     DBUG_RETURN(1);
   }
-  while ((zz=my_li++) && (it++))
+  while ((mv= var_li++) && (item= it++))
   {
-    if (zz->local)
+    if (mv->local)
     {
-      if ((yy=var_li++)) 
-      {
-	if (thd->spcont->set_variable(current_thd, yy->get_var_idx(),
-                                      it.ref()))
-	  DBUG_RETURN(1);
-      }
+      if (thd->spcont->set_variable(thd, mv->offset, &item))
+	    DBUG_RETURN(1);
     }
     else
     {
-      if ((xx=li++))
-      {
-        xx->check(0);
-	xx->update();
-      }
+      Item_func_set_user_var *suv= new Item_func_set_user_var(mv->s, item);
+
+      /*
+        Item_func_set_user_var can't substitute something else on its
+        place => NULL may be passed as last argument (reference on
+        item) Item_func_set_user_var can't be fixed after creation, so
+        we do not check var->fixed
+      */
+
+      suv->fix_fields(thd, 0);
+      suv->check(0);
+      suv->update();
     }
   }
   DBUG_RETURN(0);
diff --git a/sql/sql_class.h b/sql/sql_class.h
index 218c56959a379c7394e615c88daaf41c86353f50..20f1f680fb2609853afd2865eda3c83cc5b06d43 100644
--- a/sql/sql_class.h
+++ b/sql/sql_class.h
@@ -2181,9 +2181,7 @@ class select_dumpvar :public select_result_interceptor {
   ha_rows row_count;
 public:
   List<my_var> var_list;
-  List<Item_func_set_user_var> vars;
-  List<Item_splocal> local_vars;
-  select_dumpvar(void)  { var_list.empty(); local_vars.empty(); vars.empty(); row_count=0;}
+  select_dumpvar()  { var_list.empty(); row_count= 0;}
   ~select_dumpvar() {}
   int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
   bool send_data(List<Item> &items);
diff --git a/storage/myisam/myisampack.c b/storage/myisam/myisampack.c
index 98121cc6d905191cab481f216749e2dc0839fb51..a5c52ed7642b51d377d96c4a9d446028ec7bb10e 100644
--- a/storage/myisam/myisampack.c
+++ b/storage/myisam/myisampack.c
@@ -2923,6 +2923,8 @@ static void flush_bits(void)
     bits-= 8;
     *file_buffer.pos++= (uchar) (bit_buffer >> bits);
   }
+  if (file_buffer.pos >= file_buffer.end)
+    VOID(flush_buffer(~ (ulong) 0));
   file_buffer.bits= BITS_SAVED;
   file_buffer.bitbucket= 0;
 }