public
Last active

Select table names from SQL Server from a Mac via ActiveRecord and TinyTDS driver

  • Download Gist
freetds.conf
1 2 3 4 5
# Example Microsoft SQL Server
[myTDS]
host = host
port = 1433
tds version = 8.0
ss_ar_tinytds_test.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
require 'rubygems'
gem 'tiny_tds'
gem 'activerecord'
gem 'activerecord-sqlserver-adapter'
require 'active_record'
require 'logger'
 
logger = Logger.new(STDOUT)
ActiveRecord::Base.logger = logger
 
config = {
:adapter => 'sqlserver',
:mode => 'dblib',
:dataserver => 'myTDS', # defined in: /usr/local/etc/freetds.conf
:database => 'database',
:username => 'username',
:password => 'password'
}
 
ActiveRecord::Base.establish_connection config
 
sql = "select table_name from INFORMATION_SCHEMA.TABLES"
 
table_names = ActiveRecord::Base.connection.select_all sql
table_names.each do |h|
logger.debug "Table name = #{h['table_name']}"
end

Change host in freetds.conf to your host name of the I.P. address of the host. Also, change :database, :username, and :password in ss_ar_tinytds_test.rb accordingly.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.