Skip to content

Commit 68d1c0f

Browse files
authored
Allow INSERT statements with SELECT notation (#1238)
1 parent ee8eba9 commit 68d1c0f

File tree

3 files changed

+6
-0
lines changed

3 files changed

+6
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515

1616
- [#1215](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1215) Fix mismatched foreign key errors
1717
- [#1228](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1228) Enable identity insert on view's base table
18+
- [#1238](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1238) Allow INSERT statements with SELECT notation
1819

1920
Please check [7-1-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/7-1-stable/CHANGELOG.md) for previous changes.

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ def get_raw_table_name(sql)
702702
s.split(/INSERT INTO/i)[1]
703703
.split(/OUTPUT INSERTED/i)[0]
704704
.split(/(DEFAULT)?\s+VALUES/i)[0]
705+
.split(/\bSELECT\b(?![^\[]*\])/i)[0]
705706
.match(/\s*([^(]*)/i)[0]
706707
elsif s.match?(/^\s*UPDATE\s+.*/i)
707708
s.match(/UPDATE\s+([^\(\s]+)\s*/i)[1]

test/cases/schema_test_sqlserver.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ class SchemaTestSQLServer < ActiveRecord::TestCase
9696
it do
9797
assert_equal "[test].[aliens]", connection.send(:get_raw_table_name, "EXEC sp_executesql N'INSERT INTO [test].[aliens] ([name]) OUTPUT INSERTED.[id] VALUES (@0)', N'@0 varchar(255)', @0 = 'Trisolarans'")
9898
end
99+
100+
it do
101+
assert_equal "[with].[select notation]", connection.send(:get_raw_table_name, "INSERT INTO [with].[select notation] SELECT * FROM [table_name]")
102+
end
99103
end
100104
end
101105
end

0 commit comments

Comments
 (0)