gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:28:38 25 4
gpt4 key购买 nike

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

例如,假设以下查询返回 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 insteadof 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com