Insert into … values ( SELECT … FROM … )

前端 未结 26 2502
我在风中等你
我在风中等你 2020-11-21 05:40

I am trying to INSERT INTO a table using the input from another table. Although this is entirely feasible for many database engines, I always seem to struggle t

26条回答
  •  醉梦人生
    2020-11-21 05:56

    For Microsoft SQL Server, I will recommend learning to interpret the SYNTAX provided on MSDN. With Google it's easier than ever, to look for syntax.

    For this particular case, try

    Google: insert site:microsoft.com

    The first result will be http://msdn.microsoft.com/en-us/library/ms174335.aspx

    scroll down to the example ("Using the SELECT and EXECUTE options to insert data from other tables") if you find it difficult to interpret the syntax given at the top of the page.

    [ WITH  [ ,...n ] ]
    INSERT 
    {
            [ TOP ( expression ) [ PERCENT ] ] 
            [ INTO ] 
            {  | rowset_function_limited 
              [ WITH (  [ ...n ] ) ]
            }
        {
            [ ( column_list ) ] 
            [  ]
            { VALUES ( { DEFAULT | NULL | expression } [ ,...n ] ) [ ,...n     ] 
            | derived_table       <<<<------- Look here ------------------------
            | execute_statement   <<<<------- Look here ------------------------
            |   <<<<------- Look here ------------------------
            | DEFAULT VALUES 
            }
        }
    }
    [;]
    
    
    

    This should be applicable for any other RDBMS available there. There is no point in remembering all the syntax for all products IMO.

    提交回复
    热议问题