T-SQL Insert into table without having to specify every column

后端 未结 7 2496
悲&欢浪女
悲&欢浪女 2021-02-18 13:25

In our db there is a table that has a little over 80 columns. It has a primary key and Identity insert is turned on. I\'m looking for a way to insert into this table every colum

相关标签:
7条回答
  • 2021-02-18 14:02

    You can do this quite easily actually:

    -- Select everything into temp table
    Select * Into 
        #tmpBigTable
        From [YourBigTable]
    
    -- Drop the Primary Key Column from the temp table  
    Alter Table #tmpBigTable Drop Column [PrimaryKeyColumn]
    
    -- Insert that into your other big table
    Insert Into [YourOtherBigTable]
        Select * From #tmpBigTable
    
    -- Drop the temp table you created
    Drop Table #tmpBigTable
    

    Provided you have Identity Insert On in "YourOtherBigTable" and columns are absolutely identical you will be okay.

    0 讨论(0)
  • 2021-02-18 14:05

    No, that's not possible. You could be tempted to use

    INSERT INTO MyLargeTable SELECT * FROM OtherTable
    

    But that would not work, because your identity column would be included in the *.

    You could use

    SET IDENTITY_INSERT MyLargeTable ON
    INSERT INTO MyLargeTable SELECT * FROM OtherTable
    SET IDENTITY_INSERT MyLargeTable OFF
    

    first you enable inserting identity values, than you copy the records, then you enable the identity column again.

    But this won't work neither. SQL server won't accept the * in this case. You have to explicitly include the Id in the script, like :

    SET IDENTITY_INSERT MyLargeTable ON
    INSERT INTO MyLargeTable (Id, co1, col2, ...., col80) SELECT Id, co1, col2, ...., col80 FROM OtherTable
    SET IDENTITY_INSERT MyLargeTable OFF
    

    So we're back from where we started.

    The easiest way is to right click the table in Management Studio, let it generate the INSERT and SELECT scripts, and edit them a little to let them work together.

    0 讨论(0)
  • 2021-02-18 14:10

    Why not just create a VIEW of the original data, removing the unwanted fields? Then 'Select * into' your hearts desire.

    • Localized control within a single view
    • No need to modify SPROC
    • Add/change/delete fields easy
    • No need to query meta-data
    • No temporary tables
    0 讨论(0)
  • 2021-02-18 14:10

    Really, honestly it takes ten seconds or less to pull all of the columns over from the object browser and then delete the identity column from the list. It is a bad idea to use select * for anything but quick ad hoc query.

    0 讨论(0)
  • 2021-02-18 14:14

    You could query Information_Schema to get a list of all the columns and programatically generate the column names for your query. If you're doing this all in t-sql it would be cumbersome, but it could be done. If you're using some other client language, like C# to do the operation, it would be a little less cumbersome.

    0 讨论(0)
  • 2021-02-18 14:16
    CREATE TABLE Tests
    (
        TestID int IDENTITY PRIMARY KEY,
        A int,
        B int,
        C int
    )
    
    INSERT INTO dbo.Tests
    VALUES (1,2,3)
    
    SELECT * FROM Tests
    

    This works in SQL2012

    0 讨论(0)
提交回复
热议问题