Skip to content

Commit 7fcf20d

Browse files
authored
Allow INSERT statements with SELECT notation (#1244)
1 parent 13349d9 commit 7fcf20d

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Unreleased
2+
3+
#### Fixed
4+
5+
- [#1244](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1244) Allow INSERT statements with SELECT notation
6+
17
## v7.2.1
28

39
#### Fixed

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ def get_raw_table_name(sql)
681681
s.split(/INSERT INTO/i)[1]
682682
.split(/OUTPUT INSERTED/i)[0]
683683
.split(/(DEFAULT)?\s+VALUES/i)[0]
684+
.split(/\bSELECT\b(?![^\[]*\])/i)[0]
684685
.match(/\s*([^(]*)/i)[0]
685686
elsif s.match?(/^\s*UPDATE\s+.*/i)
686687
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)