sql - 临时表列大小在多少行之后确定?

标签 sql sql-server sql-server-2008 t-sql

在多少行之后确定临时表列大小?

例如,假设以下查询返回 10,000 行:

select Name, LastName, Resume into #temp from Employee

表 Employee 中的所有三列都是 varchar,但 Employee.Resume (varchar(500)) 的范围可以从 20 个字符到 400 个字符。如果我在没有 INTO 子句的情况下运行该查询,我将看到较长的 Employee.Resume 值几乎位于 10,000 行的末尾。

将结果转储到临时表时,INTO 子句是否考虑整个结果集?或者它是否使用前 X 行来确定大小?或者它只是“继承”原始表的列大小?

自从我查询 tempdb.sys.columns 以来,我几乎可以肯定它会复制原始列的大小,但我只是想检查一下。

谢谢。

最佳答案

它复制原始列的数据类型。它不会尝试对数据长度或精度进行任何平均。不过,有一些与 FILESTREAM(转换为 VARBINARY(MAX) )和 IDENTITY 列(有时不保留)相关的异常(exception)情况

来自MSDN :

The format of new_table is determined by evaluating the expressions in the select list. The columns in new_table are created in the order specified by the select list. Each column in new_table has the same name, data type, nullability, and value as the corresponding expression in the select list. The IDENTITY property of a column is transferred except under the conditions defined in "Working with Identity Columns" in the Remarks section.

...

Data Types

The FILESTREAM attribute does not transfer to the new table. FILESTREAM BLOBs are copied and stored in the new table as varbinary(max) BLOBs. Without the FILESTREAM attribute, the varbinary(max) data type has a limitation of 2 GB. If a FILESTREAM BLOB exceeds this value, error 7119 is raised and the statement is stopped.

When an existing identity column is selected into a new table, the new column inherits the IDENTITY property, unless one of the following conditions is true:

  • The SELECT statement contains a join.

  • Multiple SELECT statements are joined by using UNION.

  • The identity column is listed more than one time in the select list.

  • The identity column is part of an expression.

  • The identity column is from a remote data source.

If any one of these conditions is true, the column is created NOT NULL instead of inheriting the IDENTITY property. If an identity column is required in the new table but such a column is not available, or you want a seed or increment value that is different than the source identity column, define the column in the select list using the IDENTITY function. See "Creating an identity column using the IDENTITY function" in the Examples section below.

关于sql - 临时表列大小在多少行之后确定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35490885/

相关文章:

sql - 为数据库创建同义词/更改数据库 View 指向

json - 在 SSIS 中读取包含 JSON 列的 csv 文件?

java - 如何仅使用 jdbc 和标准 sql 进行分页?

sql - 数据库的事务日志已满

php - 使用 PHP 检查数据库的时间间隔

SQL 列合并和聚合函数

sql - 删除 Postgres 文本文件中指定表的行

SQL 组合列

sql - ORDER BY 索引列仍然很慢

sql - 仅当所有条件都为 true 时才返回行