Skip to content

Commit 4a66e85

Browse files
committed
Fix retrieval of temporary table's column information
1 parent 527ba54 commit 4a66e85

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Unreleased
2+
3+
#### Fixed
4+
5+
- [#1308](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1308) Fix retrieval of temporary table's column information.
6+
17
## v7.2.4
28

39
#### Fixed

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

+18-8
Original file line numberDiff line numberDiff line change
@@ -571,12 +571,22 @@ def column_definitions(table_name)
571571
end
572572

573573
def column_definitions_sql(database, identifier)
574-
object_name = prepared_statements ? "@0" : quote(identifier.object)
575-
schema_name = if identifier.schema.blank?
576-
"schema_name()"
577-
else
578-
prepared_statements ? "@1" : quote(identifier.schema)
579-
end
574+
schema_name = "schema_name()"
575+
576+
if prepared_statements
577+
object_name = "@0"
578+
schema_name = "@1" if identifier.schema.present?
579+
else
580+
object_name = quote(identifier.object)
581+
schema_name = quote(identifier.schema) if identifier.schema.present?
582+
end
583+
584+
object_id_arg = identifier.schema.present? ? "CONCAT(#{schema_name},'.',#{object_name})" : object_name
585+
586+
if identifier.temporary_table?
587+
database = "TEMPDB"
588+
object_id_arg = "CONCAT('#{database}','..',#{object_name})"
589+
end
580590

581591
%{
582592
SELECT
@@ -631,7 +641,7 @@ def column_definitions_sql(database, identifier)
631641
AND k.unique_index_id = ic.index_id
632642
AND c.column_id = ic.column_id
633643
WHERE
634-
o.name = #{object_name}
644+
o.Object_ID = Object_ID(#{object_id_arg})
635645
AND s.name = #{schema_name}
636646
ORDER BY
637647
c.column_id
@@ -653,7 +663,7 @@ def remove_check_constraints(table_name, column_name)
653663
end
654664

655665
def remove_default_constraint(table_name, column_name)
656-
# If their are foreign keys in this table, we could still get back a 2D array, so flatten just in case.
666+
# If there are foreign keys in this table, we could still get back a 2D array, so flatten just in case.
657667
execute_procedure(:sp_helpconstraint, table_name, "nomsg").flatten.select do |row|
658668
row["constraint_type"] == "DEFAULT on column #{column_name}"
659669
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)