Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Friday, August 17, 2012

Enable SQL stdout in Rails console


Edit or create .irbrc (or .pryrc if you're using pry) file in user home directory.  Add the following lines:
if defined?(Rails) && !Rails.env.nil?
puts '... ActiveRecord and ActiveResource Logger set to STDOUT'
logger = Logger.new(STDOUT)
ActiveRecord::Base.logger = logger
ActiveResource::Base.logger = logger
end

Then on your rails console, you should see the SQL statements being executed.







Execute SQL statement in Rails


rs = ActiveRecord::Base.connection.execute("select something from sometable")
value = rs[row_id_zero_based]['column_name']

Wednesday, December 29, 2010

Debug SQL Stored Procedure

In general, any database stored procedure can be debugged by inserting printouts within the procedure.  First, create a debug table to store the printouts.  e.g.

your_debug_table {
    id [int]
    message [string]
    timestamp [datetime]
}

Then, in your stored procedure, print messages to debug table.  e.g.

your_stored_procedure {
    ...
    insert into your_debug_table (message) values (your_message);
    ...
}

Keywords: MySQL, Oracle, PostgreSQL, MS SQL Server, Stored Procedure, Debug