Skip to content

Commit 6f9c242

Browse files
authored
Fix retrieval of temporary table's column information (#1308)
1 parent f1fea82 commit 6f9c242

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Unreleased
22

3+
#### Fixed
4+
5+
- [#1308](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1308) Fix retrieval of temporary table's column information.
6+
37
#### Added
48

59
- [#1301](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1301) Add support for INDEX INCLUDE.

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

+16-6
Original file line numberDiff line numberDiff line change
@@ -631,11 +631,21 @@ def column_type(ci:)
631631
end
632632

633633
def column_definitions_sql(database, identifier)
634-
object_name = prepared_statements ? "@0" : quote(identifier.object)
635-
schema_name = if identifier.schema.blank?
636-
"schema_name()"
634+
schema_name = "schema_name()"
635+
636+
if prepared_statements
637+
object_name = "@0"
638+
schema_name = "@1" if identifier.schema.present?
637639
else
638-
prepared_statements ? "@1" : quote(identifier.schema)
640+
object_name = quote(identifier.object)
641+
schema_name = quote(identifier.schema) if identifier.schema.present?
642+
end
643+
644+
object_id_arg = identifier.schema.present? ? "CONCAT(#{schema_name},'.',#{object_name})" : object_name
645+
646+
if identifier.temporary_table?
647+
database = "TEMPDB"
648+
object_id_arg = "CONCAT('#{database}','..',#{object_name})"
639649
end
640650

641651
%{
@@ -691,7 +701,7 @@ def column_definitions_sql(database, identifier)
691701
AND k.unique_index_id = ic.index_id
692702
AND c.column_id = ic.column_id
693703
WHERE
694-
o.name = #{object_name}
704+
o.Object_ID = Object_ID(#{object_id_arg})
695705
AND s.name = #{schema_name}
696706
ORDER BY
697707
c.column_id
@@ -713,7 +723,7 @@ def remove_check_constraints(table_name, column_name)
713723
end
714724

715725
def remove_default_constraint(table_name, column_name)
716-
# If their are foreign keys in this table, we could still get back a 2D array, so flatten just in case.
726+
# If there are foreign keys in this table, we could still get back a 2D array, so flatten just in case.
717727
execute_procedure(:sp_helpconstraint, table_name, "nomsg").flatten.select do |row|
718728
row["constraint_type"] == "DEFAULT on column #{column_name}"
719729
end.each do |row|

lib/active_record/connection_adapters/sqlserver/utils.rb

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ def hash
8181
parts.hash
8282
end
8383

84+
def temporary_table?
85+
object.start_with?("#")
86+
end
87+
8488
protected
8589

8690
def parse_raw_name
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
require "cases/helper_sqlserver"
4+
5+
class TemporaryTableSQLServer < ActiveRecord::TestCase
6+
def test_insert_into_temporary_table
7+
ActiveRecord::Base.with_connection do |conn|
8+
conn.exec_query("CREATE TABLE #temp_users (id INT IDENTITY(1,1), name NVARCHAR(100))")
9+
10+
result = conn.exec_query("SELECT * FROM #temp_users")
11+
assert_equal 0, result.count
12+
13+
conn.exec_query("INSERT INTO #temp_users (name) VALUES ('John'), ('Doe')")
14+
15+
result = conn.exec_query("SELECT * FROM #temp_users")
16+
assert_equal 2, result.count
17+
end
18+
end
19+
end

0 commit comments

Comments
 (0)