Skip to content


Untangle SQL Util Script

Have you ever wanted to check the values being inserted into a table?

Here’s a quick little script which parses an SQL insert statement and prints out the column name and corresponding value:

sql = <<-EOSQL
PASTE YOUR SQL HERE!
EOSQL
 
sql_matches = sql.match(/[a-z `]\((.*)\) values \((.*)\)/i)
columns = sql_matches[1].split(/, ?/)
values = sql_matches[2].split(/, ?/)
 
columns.each_with_index do |column_name, i|
  puts "#{column_name}: #{values[i]}"
end

Note: Consider the use of this script a code smell. If your table has so many columns this becomes necessary, consider normalizing your data.

Posted in Ruby, Tips and Tricks.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.