SSMS: How to import (Copy/Paste) data from excel

前端 未结 5 1681
轻奢々
轻奢々 2020-12-22 07:17

How to Copy/Paste following delimited data (by default delimited with tab) from excel:

declare @t_values   nvarchar(max)   =                   
N\'                   


        
5条回答
  •  借酒劲吻你
    2020-12-22 07:39

    Query for select into temporary table:

    -- PURPOSE: create temp table with values pasted from Excel (or any other CSV source)
    -- NB! Script do NOT write any ifo to any table
    -- to perform insert please copy/paste script output SelectInto column and execute
    -- NB! Current script works only for tables with RowNumbers < 1000
    
    declare @tmp_tbl_name   nvarchar(max)   = N'#t';    -- Destination Table Name 
    declare @tmp_clm_name   nvarchar(max)   = NULL;     -- comma separated COLUMN Names; if NOT defined (NULL) - takes from 1st data row
    declare @dlm            nvarchar(128)   = char(9);  -- Delimiter: HT = char(9) = HorizontalTab
    declare @eol            nvarchar( 2 )   = N'
    ';                                                  -- EndOfLine
    declare @tbl            nvarchar(max)   =           -- Paste Table Values here (from excel for instance)
    N'CopyPasteExcelTableHere'
    ;
    
    ------------------------------------------------------------------------------------------------------------
    declare @FirstRow  nvarchar(max); 
    declare @LastRow   nvarchar(max); 
    
    SET @tbl =     REPLACE               -- replace EndOfLine by LF  (CR - Carriage Return, LF - Line Feed)
                    ( @tbl                  -- Paste Table Values here (from excel for instance)
                    , @eol                  -- EndOfLine
                    , char(10)              -- LF
                    )
    ;
    
    IF @tmp_clm_name is NULL    --> 1st row contains column names
        BEGIN
            SET @tmp_clm_name = left(@tbl, charindex(char(10), @tbl)-1);
            SET @tbl          = stuff(@tbl, 1, len(@tmp_clm_name)+1, '');
            SET @tmp_clm_name = '[' + REPLACE(@tmp_clm_name, @dlm, '],[') + ']';
        END
    ;
    
    SET @FirstRow  = N'IF OBJECT_ID(''tempdb..'+@tmp_tbl_name+''') IS NOT NULL DROP TABLE '+ @tmp_tbl_name + ';'
            + @eol + N'-- select '+ @tmp_clm_name+' from #t;'
            + @eol + N'SELECT * INTO ' +  @tmp_tbl_name + N' FROM '
            + @eol + N'(VALUES '                            
    ;
    
    SET @LastRow    = N') t('+ @tmp_clm_name +');'        
    ;
    
    ;with v as
    (  select  --row_number()  over(order by insertquery),            
                 ',(' 
                + replace               -- replace 'NULL' by NULL
                  ( '''' 
                     + replace          -- surround values by quotes 'value1','value2'...
                       (    value
                       ,    @dlm        -- Delimiter: HT = char(9) = HorizontalTab
                       ,    ''','''
                       ) 
                     + ''''
                  , '''NULL'''  
                  , 'NULL'      
                  )                                              
                + ')' as insertvalues
        --into #t 
        from  string_split(@tbl, char(10))   -- insert Line per Row into table
        where len(value)>0                   -- skip empty rows
    ),
    rv as -- adding RowID column (have to identify 1st row to remove 1st character)
    (   select (ROW_NUMBER() OVER(ORDER BY insertvalues)) as RowID, *  
        from v
    )
              select 1 as RowID                  , @FirstRow as SelectInto 
    union all select 1 +  RowID                  , case when RowID=1 then stuff(insertvalues,1,1,' ') else insertvalues end from rv
    union all select 2 +(select count(*) from rv), @LastRow 
    order by RowID
    

提交回复
热议问题