Not exactly. Why?
- Setting up MS Sql connection from rails application is a serious pain in the a** and I have to do days to research to get it right. I have shared my findings in following two sections.
Understanding the process to connect Mssql from Ruby on Rails
The communication protocol used by client software to communicate with MS SQL Server is called Tabular Data Stream(TDS). Brian Bruns started an open source project called FreeTDS to provide an implementation of the TDS protocol for anyone who wants to connect to Mssql. FreeTDS comes with several interfaces to TDS including ODBC.
The rails SQL Server database adapter has two modes: ADO and ODBC. ADO mode allows Rails applications running on Windows to access SQL Server. ODBC mode also allows Rails applications running on non-Windows platforms to access SQL Server. In ODBC mode, the adapter connects to SQL Server through an ODBC driver.
In ODBC mode, the SQL Server adapter requires the Ruby/DBI ODBC driver, which depends on the Ruby ODBC module. The Ruby/DBI module provides a database independent interface for accessing relational databases from within Ruby. Ruby/DBI has a two layer architecture. The database interface (DBI) layer provides a set of common access methods that are used the same way regardless of the underlying database. The database driver (DBD) layer is database dependent. Each driver provides access to a particular database, translating between the DBI layer and the database. The Ruby/DBI ODBC driver provides access to databases for which an ODBC driver is available.
In short, the connection to the Mssql from Rails app would be something like this:
Ruby/Rails => ActiveRecord SQL Server Adapter => DBI, DBD-ODBC Gem => Ruby ODBC Gem => unixODBC => FreeTDS => SQL Server.Setting up the Mssql connection
We have done the setup on CentOS, but it should pretty much work with any unix flavor. We start with installing the required dependencies.
These two will provide a Mssql client "tsql" to connect to sql server from command line, which is very much like "mysql" command for mysql. Please refer to the table at the bottom of this post for queries to easily get started with tsql.yum install unixODBC unixODBC-devel yum install freetds freetds-devel
So, we are done with installing the required gem packages. Lets configure the database connection.gem install dbi gem install dbd-odbc gem install activerecord-sqlserver-adapter gem install ruby-odbc
- TDS Driver Config file: /etc/odbcinst.ini
unixODBC needs to know about all ODBC drivers you intend to use, installed drivers are specified in this file. Add the following lines in the end
You may want to check if the file exists at given location. If not, the right path will be /usr/lib/libtdsodbc.so[FreeTDS] Description = ODBC for SqlServer Driver = /usr/lib64/libtdsodbc.so Setup = /usr/lib64/libtdsS.so FileUsage = 1
- Sql Server Definition Config file: /etc/freetds.conf
For each of the Sql Server, define a entry, something like:
Give a useful name for "my_config_server" because it will be used further in config files[my_config_server] host = sql.example.com port = 1433 tds version = 8.0
- DSN Config file: /etc/odbc.ini
ODBC client applications will typically work with ODBC Data Source Names (DSN). In layman terms, for each database, we need to define a connection like
The config unique identifier my_dev_server_dsn, will be used in database.yml config file.[my_dev_server_dsn] Driver=FreeTDS Description=My Server Connection Servername=my_config_server Server=my_config_server Port=1433 Database=SampleDatabase APP=ConnectionString
- Rails App Config File: RAILS_ROOT/config/database.yml
development: adapter: sqlserver mode: ODBC username: sample password: sample database: SampleDatabase dsn: my_dev_server_dsn
And, yes we are done! ./script/console should work like a charm connecting Mssql :-)
- TDS Driver Config file: /etc/odbcinst.ini
- Mysql and Mssql have different sql query format. So, in few cases you may have to fix your find queries:
- Specifying "join on columns" in a join query is mandatory with Mssql, which is optional with Mysql and can be done in where clause.
- "Order by RAND()" does not works in Mssql, because RAND() is only a Mysql function. Moreover, rails will not raise an error if u r using this function in your queries :(
The Mssql method is NEWID()
- ActiveRecord/Migrations is not 100% Mssql complaint.
- The "date" logical type of rails, which maps to "date" datatype in mysql becomes "datetime" datatype in Mssql. Beware of this, if you are doing any date related calculations.
- The boolean logical type of rails maps to "boolean" datatype in mysql, becomes "bit(1)" datatype in Mssql. Keep in mind, code snippets like these doesn't works and neither they raise an exception :(
Such queries are to be rewritten to:1 2
Product.find(:first, :conditions => {:valid => true})
1 2
Product.find(:first, :conditions => {:valid => 1})
- "float" logical type of rails maps to "float" data type in mysql, becomes "decimal" datatype in Mssql. This doesn't have direct impact anywhere in your code. But, if you were trying to import your Mysql data dump into Mssql, then you might not get correct values imported. In my case, values like 2.33 became 2.33e+19 etc.
Running Mssql queries from Tsql
| Purpose | Mysql server | Ms Sql Server |
|---|---|---|
| Connection To Server | mysql -uusername -ppassword -hhostname | tsql -S hostname -U username -P password |
| Use Database | mysql> use databasename; | 1> use databasename 2> go |
| show tables | mysql> show tables | 1> select name from databasename..sysobjects where xtype='U'; 2> go |
| show some rows from a table | mysql> select * from tablename limit 10; | 1> select top 10 * from tablename 2> go |

Thanks for your post, it is not an easy solution.
I hope I can improve through learning this respect. But overall, it’s very nice. Thank you for your share!
The Acronym List is a searchable database of over 8 million acronyms, abbreviations and meanings. Covers: business, international, chat, organizations, common acronyms, computers, science, technology, government, telecommunications, and military acronyms.
Thanks for your post, it is not an easy solution.
this is really informative post and a goldmine for me
I would be confusion if the deal with this. but with this post is, of course I will be able to follow step by step, as you point out
This is not easy, let alone someone like me who have not studied sql
sql always makes me dizzy, but your post looks very easy sql
Thanks for your guide.
Good article, thank you so much to share :)
Juicy Couture Shop the latest styles juicy couture handbags, juicy couture tracksuit.
Shop the latest styles juicy couture handbags, juicy couture tracksuit. Juicy Couture
This is great site! Thanks! Awesome! Two thumbs up!
autoradio navigation, car dvd gps navigation, sat navigation stereo, OEM Factory headunit for all car makes
autoradio navigation, car dvd gps navigation, sat navigation stereo, OEM Factory headunit for all car makes Higher quality car electronics from Qualir Car DVD Player
autoradio navigation, car dvd gps navigation, sat navigation stereo, OEM Factory headunit for all car makes Higher quality car electronics from Qualir Car DVD Player
It sounds good! Thanks for sharing.
Shop the latest styles juicy couture handbags, juicy couture tracksuit. Juicy Couture
If you set all links on your comments to use rel=”nofollow” then you wouldn’t get so much spam.
Thanks very much! This has helped me out on another topic i wasnt able to solve since ive read this blog entry! Its so easy i dont understand why i did it another way wasting so much time…
thanks ruby !!
Thanks for your guide.
Hope all goes well with your new database server! Good luck!
thanks for this great information. i hope coming more!
thanks for this great information. i hope coming more!
thanks for this great information. i hope coming more!
thanks for this great information. i hope coming more!
Well worth the read. Thanks for sharing this information. I got a chance to know about this.
Good post, thanks for share.I like to visit more often, is there a place to get tutorials on this game, Your post is amazing.
Welcome to fashion goods online store, http://www.coach-outlet.co.uk coach bags http://www.coach-outlet.co.uk Coach Outlet http://www.coach-outlet.co.uk coach outlet store online http://www.coach-outlet.co.uk coach handbags
mason0507 thank you
maosn05407
About Moving from Mysql to Microsoft SQL Server in Ruby on Rails 31 ,you give a wonderful solution,but this is not easy, let alone someone like me who have not studied sql.
I would like to explain the misunderstanding for women and men. That is, we often think that men like women to wear high heels. Especially the women just fall in love, in order to perform better on her former boyfriend can easily lead to misunderstanding of some error. Such an interesting story: a couple just contact a week, early morning when the men want to see her lovely girlfriend, came to his girlfriend’s doorstep. But when he saw his girlfriend wearing a fine pair of boots, the man immediately lost her good opinion. Because men want to look like his girlfriend just woke up. Most men like to wear high heels in addition to women, women will love to wear sportswear. Perhaps this is a man to a woman from his
http://www.coach-outlet.co.uk coach bags http://www.coach-outlet.co.uk Coach Outlet http://www.coach-outlet.co.uk coach outlet store online http://www.coach-outlet.co.uk coach handbags
Moving database to MySQL offers many benefits but not always is the best solution for your application. here are many applications out there that use a combination of Microsoft Access and JET to manage data. These applications are only used by a single user, and often are used in situations where the data file is simply copied to a new machine when it needs to be moved.
Beautiful and attractive site. برامج
thnx 4 sharing الجياش
Defenitly been the best soloution for our business, great article
Not an easy solution and not always the best. Thanks for the post, much easier after reading it.
Ahh moving databases is a tricky thing to do. I had to go through that when I changed hosts….what a nightmare it was!
Rate My Muscles
Nice post.Thank you for taking the time to publish this information very useful!I’m still waiting for some interesting thoughts from your side in your next post thanks
Tough stuff. This is not easy, let alone someone like me who did not study sql, but I’ll give it a try.
I ordered these because I thoughtUGG Australia is a brand that is all about luxury and comfort for everyday life.
Thanks for the post, much easier after reading it. Not an easy solution and not always the best.
Hello!!It is my horour to see you blog.Iam agree with you,Ithink friendship is very important,so we must have a lot friend,History repeated proof: friend, health than leadership than performance than IQ, eq, holiday greetings than usual, asss an important than. Space than the ground is good, to visit friends, no tickets.
I want to Moving from Mysql to Microsoft SQL Server in Ruby. thanks for sharing this.And i believe you have solved the problem.
Moving database to MySQL offers many benefits but not always is the best solution for your application. Our website has a back office built in MySQL and i sware it’s a nigthmare to operate . Lunettes Ray Ban
I’ve had a general idea of moving from MySQL to MSSQL, but your detailed solution now makes all things clear =)
This article is interesting, but unfortunately, it seems quarkruby doesn’t live any more…
My company use Ruby, so it’s very interesting
This doesn’t have direct impact anywhere in your code. But, if you were trying to import your Mysql data dump into Mssql, then you might not get correct values imported.
this guide is helpful, thx!
This is exactly what I’m looking for. Really nice tutorial this.
I`ll be back to read your site , lets hope that your future news will be as good as this ones are. I`m glad i found this site! What you`ve wrote here is very true and can be very usefull for the readers of this site. Have good luck with your site and i`ll be back to read your new informations.
Really nice, im doing a new website for my new printing business and this will help me set up my new database of printing prices.
really great i have a printing site too, it’s been really good for me.
really nice blog,I MBT Changa Shoes think most people would agree with your article. I am MBT Changa Shoes going to bookmark this web site,I will check this site ofen.
What arestorm…Gossip: what kind of dressing girls have treasure?Together and see it!
This will really help us with website devlopment
xiaoru
xiaoru
xiaoru
For any C, C++, Java, ASP.net, PHP, JSP, Fortran or any other Programming and IT Security Assignment help you need, please visit our site at http://www.helpwiththesis.com. We help College and University students at a minimal price.
Solid tutorial and explanatory. Congratulations. It was exactly what I was looking for. Thank you.
For any C, C++, Java, ASP.net, PHP, JSP, Fortran or any other Programming and IT Security Assignment help you need, please visit our site at http://www.helpwiththesis.com. We help College and University students at a minimal price.
For any C, C++, Java, ASP.net, PHP, JSP, Fortran or any other Programming and IT Security Assignment help you need, please visit our site at http://www.helpwiththesis.com. We help College and University students at a minimal price. take me home tonight download watch jane eyre online jane eyre download watch red riding hood online
Hello,
I have a strong sql base but am looking for some courses that can help me learn the structure and ins and outs of sql. I need a class in Houston, or even online may work. Has anyone had experience with a good training class for sql in Houston? Thanks!
Regards, Benix
Outstanding tuto. Thanks for this.
tyujhyujhgjhgjme courses that can help me learn the structure and ins and outs of sql. I need a class in Houston, or even online may work. Has anyone had experience with a good training class for sql in Houst
What an excellent blog! Thanks very much! This has helped me out on another topic i wasnt able to solve since ive read this blog entry! Its so easy i dont understand why i did it another way wasting so much time…
This tutorial is exactly what I was looking for. Step by step. Thanks for sharing
je vous remercie de partager, très gentil de votre part
Great Blog, i’m new to SQL but it’s becoming more apparent as I start understanding the concept. We are in the process of redeveloping out site in aspx and more information the better.
I forgot to mention does anyone know where I can find a good tutorial on profile building?
Is there any open source tools available for migration.
Thank you for submiting this entry. Keep it up!!
how to create the same Shoutbox without using Flash?
Thank you for submiting this entry. Keep it up!! fast five download
The comments must be somehow moderated. This is the only option if you want to remove a part of these spam comments.
This is a relative issue. It’s about what you want at a time. Switching can be done, but you can not please you.
I got a chance to know about this. Thanks for sharing this information.
très bon site et j’aime beaucoup.
I had a real problem with my DNS config, after a while I realized that the ODBC Data was not pointing to the right names.
While I was running the TDS driver config, I had this idea of reverting some of my code to a better approach with MySQL. Thanks to you
Thanks for the post !
While Setting up the Mssql connection I am getting error installing the required dependencies. yum install unixODBC unixODBC-devel yum install freetds freetds-devel
InterestingI layout on your blog. I really enjoyed reading it and also I will be back to read more in the future.
InterestingI layout on your blog. I really enjoyed reading it and also I will be back to read more in the future.
I really hate those nofollow stuff. Not because the link does nit counts, more because it sucks to give someone content for nothing. Here in germany some “webmaster” are really sick! Very often for example bookmarking services grab as many content they can get, but after a while they got nofollow. Thats sick! Sorry…
yml should make it an easy solution. I have shared my findings in following two sections. Brian Bruns started an open source project called FreeTDS to provide an implementation of the TDS protocol for anyone who wants to connect to Mssql. FreeTDS comes with several interfaces to TDS including ODBC. ADO mode allows Rails applications running on Windows to access SQL Server. We start with installing the required dependencies. Please refer to the table at the bottom of this post for queries to easily get started with tsql. Lets configure the database connection. Thanks for sharing this information. I got a chance to know about this. Because men want to look like his girlfriend just woke up. here are many applications out there that use a combination of Microsoft Access and JET to manage data.And i believe you have solved the problem. We help College and University students at a minimal price. It was exactly what I was looking for. We are in the process of redeveloping out site in aspx and more information the better. This is the only option if you want to remove a part of these spam comments. I really enjoyed reading it and also I will be back to read more in the future. They are devoted to web technology boutique.com or can be hired for consulting at Downcase Consulting. I really hate those nofollow stuff. Not because the link does nit counts, more because it sucks to give someone content for nothing. Here in germany some “webmaster” are really sick! Very often for example bookmarking services grab as many content they can get, but after a while they got nofollow. Thats sick! Sorry…
This is a difficult thing. Seriously, I’ve experienced it myself. If it’s possible, I would avoid moving server.