# ActiveRecord: table name with spaces?

**URL:** <https://www.ruby-forum.com/t/activerecord-table-name-with-spaces/52573>\
**Category:** Rails\
**Created:** [January 16, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/activerecord-table-name-with-spaces/52573 "2006-01-16T18:04:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![afewtips](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/76d3ee/32.png) [@afewtips](https://www.ruby-forum.com/u/afewtips)\
**Post date:** [January 16, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/activerecord-table-name-with-spaces/52573/1 "2006-01-16T18:04:41Z")

</div>

Howdy.

I’m trying to work with a legacy SQL Server database where some of the  
table names have spaces in them.

I’ve done set\_table\_name “tablename with spaces”, but I can’t get  
find(:all) to work. I get the following error (because apparently  
it’s not putting [] around the name as is required for SQL Server for  
names with spaces.

* * *

## c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/connection\_ adapters/abstract\_adapter.rb:88:in `log': DBI::DatabaseError: Execute (ActiveRec ord::StatementInvalid) OLE error code:80040E14 in Microsoft OLE DB Provider for SQL Server Incorrect syntax near the keyword 'all'. HRESULT error code:0x80020009 Exception occurred.: SELECT * FROM all wells with apis from c:/ruby/lib /ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/sq lserver_adapter.rb:467:in`select’ from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_recor d/connection\_adapters/sqlserver\_adapter.rb:227:in `select_all' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_recor d/base.rb:431:in`find\_by\_sql’

I see from the exception method that it hasn’t delimited the table  
name. There may be more than one way to do this in T-SQL, but what  
I’m familiar with is putting [] around the table name.

If I do my own manual find\_by\_sql and put “select \* from [all wells  
with apis]” it works fine.

Thanks for any suggestions.

---

<div class="post-metadata">

**Author:** ![afewtips](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/76d3ee/32.png) [@afewtips](https://www.ruby-forum.com/u/afewtips)\
**Post date:** [January 16, 2006, 7:59pm UTC](https://www.ruby-forum.com/t/activerecord-table-name-with-spaces/52573/2 "2006-01-16T19:59:19Z")

</div>

Michael T. wrote:

> If I do my own manual find\_by\_sql and put “select \* from [all wells  
> with apis]” it works fine.

Could you just create a view on the table without spaces in the name and  
select off of that? “SELECT \* FROM all\_wells\_with\_apis\_view;”

–  
Alan G.  
Cronosys, LLC [http://www.cronosys.com](http://www.cronosys.com)  
Phone: 216-221-4600 ext 308

---

<div class="post-metadata">

**Author:** ![afewtips](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/76d3ee/32.png) [@afewtips](https://www.ruby-forum.com/u/afewtips)\
**Post date:** [January 17, 2006, 2:01am UTC](https://www.ruby-forum.com/t/activerecord-table-name-with-spaces/52573/3 "2006-01-17T02:01:12Z")

</div>

Ok, without other suggestions that’s what I’ll do (and thanks for that  
suggestion).

But I am curious what one would do if modifying the DB were not an  
option? Are we reduced to doing all \_by\_sql() calls?

MT
