代码之家  ›  专栏  ›  技术社区  ›  craigmoliver

像SQL Server 2005/2008中的当前表一样创建临时表

  •  6
  • craigmoliver  · 技术社区  · 15 年前

    如何创建与存储过程中的当前表完全相同的临时表?

    4 回复  |  直到 15 年前
        1
  •  15
  •   LukLed    15 年前
    select * into #temp_table from current_table_in_stored_procedure
    
    #temp_table - locally temp
    ##temp_table - globally temp
    
    select top 0 * into #temp_table from current_table_in_stored_procedure to have empty table
    
        2
  •  7
  •   manji    15 年前

    SELECT * INTO #t FROM table

    如果希望它为空:

    SELECT * INTO #t FROM table WHERE 1 = 2

        3
  •  1
  •   HLGEM    15 年前

    或者,可以编写现有表的脚本,并将该名称更改为临时表名称,并将创建表脚本添加到要运行的其余脚本的顶部。如果临时表与实际表的结构完全匹配(例如,当我创建一个名为inserted的假表以在测试我要放入触发器的代码时使用),通常我会这样做。

    大多数时候,尽管选择进入会得到你需要的。

        4
  •  0
  •   priyanka.sarkar    15 年前

    除了临时表外,公共表表达式或表变量还可以用于其他用途。