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

后端 未结 7 2524
悲&欢浪女
悲&欢浪女 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:26

    In answer to a related question (SELECT * EXCEPT), I point out the truly relational language Tutorial D allows projection to be expressed in terms of the attributes to be removed instead of the ones to be kept e.g.

    my_relvar { ALL BUT description }
    

    However its INSERT syntax requires tuple value constructors to include attribute name / value pairs e.g.

    INSERT P
       RELATION 
       {
          TUPLE { PNO PNO ( 'P1' ) , PNAME CHARACTER ( 'Nut' ) }, 
          TUPLE { PNO PNO ( 'P2' ) , PNAME CHARACTER ( 'Bolt' ) }
       };
    

    Of course, using this syntax there is no column ordering (because it is truly relational!) e.g. this is semantically equivalent:

    INSERT P
       RELATION 
       {
          TUPLE { PNO PNO ( 'P1' ) , PNAME CHARACTER ( 'Nut' ) }, 
          TUPLE { PNAME CHARACTER ( 'Bolt' ) , PNO PNO ( 'P2' ) }
       };
    

    The alternative would be to rely fully on attribute ordering, which SQL does partially e.g. this is a close SQL equivalent to the the above:

    INSERT INTO P ( PNO , PNAME ) 
       VALUES        
          ( PNO ( 'P1' ) , CAST ( 'Nut'  AS VARCHAR ( 20 ) ) ) , 
          ( PNO ( 'P2' ) , CAST ( 'Bolt' AS VARCHAR ( 20 ) ) );
    

    Once the commalist of columns has been specified the VALUES row constructors have the maintain this order, which is not ideal. But at least the order is specified: your proposal would rely on some default order which may be possibly non-deterministic.

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