Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Matt Scilipoti

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

ActiveRecord and SQL Server

From http://www.softiesonrails.com/articles/2006/06/28/activerecord-with-sqlserver-without-rails:

Ensure you done the initial setup for SQL Server and ActiveRecord (ADO.rb).

   1  
   2  require 'rubygems'
   3  require_gem 'activerecord'
   4  
   5  ActiveRecord::Base.establish_connection(
   6    :adapter  => "sqlserver",
   7    :dsn => "instance_name_goes_here",
   8    :host => "machine_name\\instance_name",
   9    :database =>  "db_name",
  10    :username =>  "my_username",
  11    :password =>  "my_password"
  12  )
  13  
  14  class MsSqlTable < ActiveRecord::Base
  15    def method_missing(method, *args)
  16      respond_to?(method) ? super : send(method.to_s.camelize, *args)
  17    end
  18  end
  19  
  20  class Item < MsSqlTable
  21    set_primary_key "sku"
  22    set_table_name "item"
  23  end


I'll walk through it real quick:

1. The :dsn needs to be your SQL Server instance name
2. The :host needs to a path like "myserver\myinstance", even though you've already specified the instance name in the :dsn parameter
3. The rest of the settings should be obvious. If your password is blank, don't specify :password at all.
4. Our legacy column names are things like OldDescription. So I created an intermediate base class that would provide ruby-like aliases. Now I can use item.old_description instead if I want to.
5. Finally, I declare one class for each table I want to access. I have to set the primary key and table name manually, since our tables don't correspond to any Rails conventions.

I saved this script as legacy.rb, and then fired up irb from that directory:
   1  
   2  irb> load 'legacy.rb'
   3  => true
   4  irb> Item.find("191191").old_description
   5  => "Table In A Bag"
   6  irb>

Force recompile of Sql Server stored procs

This force a recompile of all stored procs, views, tables, triggers, and functions in the current database.
   1  
   2  CHECKPOINT 
   3  GO
   4  
   5  DBCC DROPCLEANBUFFERS
   6  GO
   7  
   8  DBCC FREEPROCCACHE
   9  GO
  10  
  11  DECLARE @intDBID INTEGER 
  12  SET @intDBID = DB_ID()
  13  
  14  DBCC FLUSHPROCINDB (@intDBID)
  15  GO
  16  
  17  
  18  declare @procNames Table (procName varchar(255))
  19  insert into @procNames
  20  select name from sysObjects where xtype in ('V','P','U','FN','TF','TR' ) and status > 0
  21  
  22  --select * from @procNames
  23  
  24  set nocount off
  25  --3. Run each command
  26  -- =============================================
  27  -- Declare and using a READ_ONLY cursor
  28  -- =============================================
  29  DECLARE RecompilableItemsCursor CURSOR
  30  READ_ONLY
  31  FOR select procName from @procNames
  32  
  33  DECLARE @RecompilableItem varchar(255)
  34  OPEN RecompilableItemsCursor
  35  
  36  
  37  FETCH NEXT FROM RecompilableItemsCursor INTO @RecompilableItem
  38  WHILE (@@fetch_status <> -1)
  39  BEGIN
  40  	IF (@@fetch_status <> -2)
  41  	BEGIN
  42  		DECLARE @sql varchar(300)
  43  		select @sql = 'Exec sp_recompile ' + @RecompilableItem
  44  		print @sql
  45  		exec (@sql)
  46  	END
  47  	FETCH NEXT FROM RecompilableItemsCursor INTO @RecompilableItem
  48  END
  49  
  50  CLOSE RecompilableItemsCursor
  51  DEALLOCATE RecompilableItemsCursor
  52  
  53  
  54  GO
  55  

Setup for Performance Testing: Clear cache, buffers (MSSQL)

For Microsoft SQL (MSSQL).
Use this to clear the cache and buffers to ensure comparison are accurate.

   1  
   2  dbcc freeproccache
   3  go
   4  dbcc dropcleanbuffers
   5  go
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS