为什么 'Rows affected' 的数量在正常 SELECT 和 SELECT INTO 之间不同?

Why does the number of 'Rows affected' differ between a normal SELECT and a SELECT INTO?

我有一个如下所示的查询:

SELECT  
    OBJECT_SCHEMA_NAME(T.[object_id],DB_ID()) AS [Schema]
    ,T.[name] AS [Table], AC.[name] AS [Column]
    ,TY.[name] AS [Data Type]
    ,sep.[value] AS [Description]
    ,AC.[max_length] AS [Maximum Field Length]
    ,create_date AS [Date Created]
    ,modify_date AS [Date Modified]
    ,CASE WHEN is_identity = 1 THEN 'Yes' ELSE 'No' END AS [Primary Key]
    ,CASE WHEN AC.[is_nullable] = 1 THEN 'Yes' ELSE 'No' END AS [Allows Nulls]  
--  INTO 
--  tmp_desc
FROM sys.[tables] AS T   
    INNER JOIN sys.[all_columns] AC ON T.[object_id] = AC.[object_id]  
    INNER JOIN sys.[types] TY ON AC.[system_type_id] = TY.[system_type_id] AND AC.[user_type_id] = TY.[user_type_id]   
    left join sys.extended_properties sep on t.object_id = sep.major_id
                                         and ac.column_id = sep.minor_id
                                         and sep.name = 'MS_Description'
WHERE
    T.[is_ms_shipped] = 0

...列出我的 table 和字段中的一些属性。

当我执行以下步骤时,会发生奇怪的事情:

  1. 我 运行 SELECT,还没有创建临时 table(我 运行 滴 table 以防万一):1018 条记录 已返回
  2. I 运行 SELECT INTO(在上面的代码中取消注释):1028 条记录 受影响
  3. 我再次注释掉 INTO,不要删除温度 table,并且 SELECT 仅:1028 条记录 返回
  4. I 运行 'select * from tmp_desc': 1028 条记录 返回
  5. 我再次删除了温度 table 和 运行 SELECT:1018 条记录 返回

创建临时 table 的任何想法都会影响从查询返回的行,如果 tmp_desc 不是来自查询的一部分?

存储在tmp_desc中的数据包括自身的详细信息;显然,当您只是 运行 SELECT(没有 INTO)时,它不会。结果,在 运行 纯 SELECT 之后,INTO 多了 10 行,因为 table tmp_desc.[= 中有 10 列20=]

db<>fiddle

如果您希望查询“相同”,请使用 INTO:

从查询中排除 tmp_desc
SELECT S.[name] AS [Schema],
       T.[name] AS [Table],
       AC.[name] AS [Column],
       TY.[name] AS [Data Type],
       sep.[value] AS [Description],
       AC.[max_length] AS [Maximum Field Length],
       create_date AS [Date Created],
       modify_date AS [Date Modified],
       CASE WHEN is_identity = 1 THEN 'Yes' ELSE 'No' END AS [Primary Key],
       CASE WHEN AC.[is_nullable] = 1 THEN 'Yes' ELSE 'No' END AS [Allows Nulls]
INTO tmp_desc
FROM sys.schemas S
     INNER JOIN sys.[tables] T ON S.schema_id = T.schema_id
     INNER JOIN sys.[all_columns] AC ON T.[object_id] = AC.[object_id]
     INNER JOIN sys.[types] TY ON AC.[system_type_id] = TY.[system_type_id]
                              AND AC.[user_type_id] = TY.[user_type_id]
     LEFT JOIN sys.extended_properties sep ON T.object_id = sep.major_id
                                          AND AC.column_id = sep.minor_id
                                          AND sep.name = 'MS_Description'
WHERE T.[is_ms_shipped] = 0
  AND NOT(S.[name] = N'dbo' AND T.[name] = 'tmp_desc');