# How to execute create table on fly

**URL:** <https://www.ruby-forum.com/t/how-to-execute-create-table-on-fly/91291>\
**Category:** Rails\
**Created:** [April 6, 2007, 10:19am UTC](https://www.ruby-forum.com/t/how-to-execute-create-table-on-fly/91291 "2007-04-06T10:19:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![feng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@feng](https://www.ruby-forum.com/u/feng)\
**Post date:** [April 6, 2007, 10:19am UTC](https://www.ruby-forum.com/t/how-to-execute-create-table-on-fly/91291/1 "2007-04-06T10:19:19Z")

</div>

dear all,

I’m a Rails newbie and i have some problem.

I’m using MySQL.

I want to excute some complexity query,

and I need to create some table to hold temp query result.

I want to know how to execute some SQL query such like

"CREATE TEMPORARY TABLE templist SELECT \* FROM table2 "

I’ve tried to excute find\_by\_sql("create table templist select \* …),

but it seem to return a nil object , so that rails will halt.

any tips is appreciated.

---

<div class="post-metadata">

**Author:** ![feng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@feng](https://www.ruby-forum.com/u/feng)\
**Post date:** [April 6, 2007, 11:46am UTC](https://www.ruby-forum.com/t/how-to-execute-create-table-on-fly/91291/2 "2007-04-06T11:46:00Z")

</div>

I won’t hold you back, but perhaps you should think about you solution -  
temp tables are always strange… Anyways,

[http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#M000728](http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#M000728)

ActiveRecord::Base.execute(sql, name=nil)

is your friend.

---

<div class="post-metadata">

**Author:** ![feng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@feng](https://www.ruby-forum.com/u/feng)\
**Post date:** [April 6, 2007, 3:25pm UTC](https://www.ruby-forum.com/t/how-to-execute-create-table-on-fly/91291/3 "2007-04-06T15:25:49Z")

</div>

Florian G. wrote:

> I won’t hold you back, but perhaps you should think about you solution -  
> temp tables are always strange… Anyways,
> 
> [ActiveRecord::ConnectionAdapters::DatabaseStatements](http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#M000728)
> 
> ActiveRecord::Base.execute(sql, name=nil)
> 
> is your friend.

thanks. and I find “execute” in my “Agile” book.

However, I still don’t know how to “execute” it in my controller.

if I add this line:

List.execute (“drop table if exists testtable;”)

then i got a error:  
"NoMethodError in LookupController#findcross

undefined method `execute’ for List:Class"

Any tips is appreciated.

-Feng

---

<div class="post-metadata">

**Author:** ![feng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@feng](https://www.ruby-forum.com/u/feng)\
**Post date:** [April 6, 2007, 3:38pm UTC](https://www.ruby-forum.com/t/how-to-execute-create-table-on-fly/91291/4 "2007-04-06T15:38:44Z")

</div>

feng wrote the following on 06.04.2007 15:25 :

> > 
> 
> thanks. and I find “execute” in my “Agile” book.
> 
> However, I still don’t know how to “execute” it in my controller.
> 
> if I add this line:
> 
> List.execute (“drop table if exists testtable;”)

List.connection.execute should do it.

---

<div class="post-metadata">

**Author:** ![feng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/82dd89/32.png) [@feng](https://www.ruby-forum.com/u/feng)\
**Post date:** [April 6, 2007, 3:42pm UTC](https://www.ruby-forum.com/t/how-to-execute-create-table-on-fly/91291/5 "2007-04-06T15:42:04Z")

</div>

Lionel B. wrote:

> feng wrote the following on 06.04.2007 15:25 :
> 
> > > 
> > 
> > thanks. and I find “execute” in my “Agile” book.
> > 
> > However, I still don’t know how to “execute” it in my controller.
> > 
> > if I add this line:
> > 
> > List.execute (“drop table if exists testtable;”)
> 
> List.connection.execute should do it.

it works!

thanks very very much ! 🙂
