Dynamic T-SQL stored procedure to insert in differents tables

后端 未结 3 627
终归单人心
终归单人心 2021-01-27 16:20

What is the best way to achieve this

INSERT INTO @TableName (@ColumnNames)
    EXEC sp_executesql @SQLResult;

Where @TableName,

3条回答
  •  囚心锁ツ
    2021-01-27 17:04

    First of all I appreciate all your comments. And I agree that SQL dynamic is a pain to debug (Thanks God, management studio has this possibility). And, of course there are hundreds of different solutions

    I solved it in this way finally, more or less I try to explain why this solution of SQL dynamic. The client uses xlsx spreadsheets to enter certain data, so I read the spreadsheets and I insert the (data depends on the spreadsheet to insert into the proper table). Later the data in the tables are exported to XML to send a third party sofware.

    SET @SEL = N'';
    DECLARE sel_cursor CURSOR
    FOR (SELECT  sc.name as field
          FROM sys.objects so INNER JOIN sys.columns sc ON so.[object_id]=sc.[object_id]
          WHERE so.name= @TableName and sc.name not in ('InitDate', 'EndDate', 'Version', 'Status'));
    
    SET @SEL = ''; set @i = 0;
    OPEN sel_cursor
    FETCH NEXT FROM sel_cursor INTO @field
    WHILE @@FETCH_STATUS = 0
    BEGIN
        set @sel = @sel + ', '+ @field 
        set @i   = 1;
        FETCH NEXT FROM sel_cursor INTO @field
    END
    CLOSE sel_cursor;
    DEALLOCATE sel_cursor;
    
    
    SET @SQL = N''
    SET @SQL = @SQL + N'SELECT * INTO XLImport FROM OPENROWSET'
    SET @SQL = @SQL + N'('
    SET @SQL = @SQL + N'''Microsoft.ACE.OLEDB.12.0'''+','
    SET @SQL = @SQL + N'''Excel 12.0 Xml; HDR=YES;'
    SET @SQL = @SQL + N'Database='+@file +''''+ ','
    SET @SQL = @SQL + N'''select * from ['+ @SheetName + '$]'''+');'
    
    EXEC sp_executesql @SQL
    
    SET @SQL = N'';
    SET @SQL = @SQL + N'
    SELECT '+''''+CAST(@initDate AS VARCHAR(10))+'''' +', '+ ''''+CAST(@endDate AS VARCHAR(10))+'''' 
          + ', '+ CAST(@version AS VARCHAR(2)) +', ' +''''+@status+''''
          + @SEL 
     +'    FROM DBO.XLImport '
    
    
    DECLARE cols_cursor CURSOR
       FOR (Select COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS where table_name = @tableName);
    SET @SEL = ''; set @i = 0;
    OPEN cols_cursor
    FETCH NEXT FROM cols_cursor INTO @field
    WHILE @@FETCH_STATUS = 0
    BEGIN
       set @sel = @sel + @field + ', '
       set @i   = 1;
       FETCH NEXT FROM cols_cursor INTO @field
    END
    CLOSE cols_cursor;
    DEALLOCATE cols_cursor;
    
    SET @SEL = LEFT(@SEL, LEN(@SEL) - 1) -- remove last ,
    
    
    SET @SQL = N''
    SET @SQL = @SQL + N'SELECT * INTO XLImport FROM OPENROWSET'
    SET @SQL = @SQL + N'('
    SET @SQL = @SQL + N'''Microsoft.ACE.OLEDB.12.0'''+','
    SET @SQL = @SQL + N'''Excel 12.0 Xml; HDR=YES;'
    SET @SQL = @SQL + N'Database='+@file +''''+ ','
    SET @SQL = @SQL + N'''select * from ['+ @SheetName + '$]'''+');'
    
    EXEC sp_executesql @SQL
    
    SET @SQLString =
    N'INSERT INTO '+ @TableName + '('+ @SEL +') ' + @SQL;
    
    EXEC sp_executesql @SQLString
    

提交回复
热议问题