diff --git a/lib/active_record/connection_adapters/sqlserver/schema_statements.rb b/lib/active_record/connection_adapters/sqlserver/schema_statements.rb index 4a71f754e..f85806478 100644 --- a/lib/active_record/connection_adapters/sqlserver/schema_statements.rb +++ b/lib/active_record/connection_adapters/sqlserver/schema_statements.rb @@ -673,6 +673,8 @@ def get_table_name(sql) # Parses the raw table name that is used in the SQL. Table name could include database/schema/etc. def get_raw_table_name(sql) + return if sql.blank? + s = sql.gsub(/^\s*EXEC sp_executesql N'/i, "") if s.match?(/^\s*INSERT INTO.*/i) diff --git a/test/cases/schema_test_sqlserver.rb b/test/cases/schema_test_sqlserver.rb index 1751af622..255c58711 100644 --- a/test/cases/schema_test_sqlserver.rb +++ b/test/cases/schema_test_sqlserver.rb @@ -66,6 +66,10 @@ class SchemaTestSQLServer < ActiveRecord::TestCase it do assert_equal "[WITH - SPACES$DOLLAR]", connection.send(:get_raw_table_name, "SELECT id FROM [WITH - SPACES$DOLLAR]") end + + it do + assert_nil connection.send(:get_raw_table_name, nil) + end end describe 'INSERT statements' do