【问题标题】:Insert values from another Table in sql server 2008在 sql server 2008 中插入另一个表中的值
【发布时间】:2012-03-24 22:32:35
【问题描述】:

我必须将所有列数据复制到另一个表。我创建了一个新的空白表。如何在其中插入值。我避免手动编写列名,因为它包含 35 个列名。序列和名称两个表中的列数相同..?

【问题讨论】:

    标签: sql sql-server-2008


    【解决方案1】:

    如果表有相同的列和类型,就这样做;

    INSERT INTO table2 SELECT * FROM table1;
    

    演示here.

    【讨论】:

      【解决方案2】:

      使用以下脚本:

      INSERT INTO "table1" ("column1", "column2", ...)
             SELECT "column3", "column4", ...
                FROM "table2"
      

      有关更多信息,请参阅: http://www.1keydata.com/sql/sqlinsert.html

      【讨论】:

        【解决方案3】:

        为每列创建包含列和数据类型的 table2。如果两个表上的列完全匹配,则从 table1 插入到 table2 中

        Create table table2(
        column1 datatype, 
        column2 datatype,
        column3 datatype,
        column35 datatype
        }
        
        INSERT INTO table2
        SELECT * from table1
        

        【讨论】:

          【解决方案4】:
          create table2 
          
          insert into table2
          select * from table1
          

          【讨论】:

            【解决方案5】:

            请找到我的版本。我在两个表中都有相同的列名

                INSERT INTO first_table 
                        (column_1, 
                         column_2, 
                         column_3, 
                         column_etc)
            
            SELECT tab2.column_1 AS column_1, 
                   10            AS column_2, 
                   Getdate()     AS column_3, 
                   'some_text'   AS column_etc
            
            FROM   second_table tab2 (nolock) 
            

            【讨论】:

              【解决方案6】:
              insert into dbo.FolderStatus
              (  
                 [FolderStatusId],
                 [code],
                 [title],
                 [last_modified]
              )
              select
              [code],
              [code],
              [title],
              [last_modified]
              from dbo.f_file_stat
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多