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

Software Engineering 1957 views

Your /usr/local/etc/freetds.conf file should look something like this:

# Example Microsoft SQL Server
    host = host
    port = 1433
    tds version = 8.0

Change host in freetds.conf to your host name of the I.P. address of the host.

Here's a simple test:

require 'rubygems'
gem 'tiny_tds'
gem 'activerecord'
gem 'activerecord-sqlserver-adapter'
require 'active_record'
require 'logger'

logger = 
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']}"

Also, change :database, :username, and :password in ss_ar_tinytds_test.rb accordingly.

See Also

Create Ruby apps using SQL Server on macOS