【问题标题】:Dynamic bulkinsert of mutiple csv files from different location folders动态批量插入来自不同位置文件夹的多个 csv 文件
【发布时间】:2018-10-27 06:38:28
【问题描述】:

我在不同的位置文件夹中有多个 csv 文件。我想动态地在 SQL 服务器中进行批量插入,这将在单个表中进行批量插入。 我是为一个 CSV 文件做的。有人可以帮帮我吗?

【问题讨论】:

    标签: sql-server sql-server-2008 sql-server-2005


    【解决方案1】:

    这里有一些东西可以帮助您入门。您可以阅读 xp_dirtreecursors 以了解它们是如何工作的。如果您的文件分布在不同的父文件夹或不同的驱动器中,您将需要一个额外的光标来获取它们...

    ---------------------------------------------------------------------------------------------------------------
    --Set some variables
    ---------------------------------------------------------------------------------------------------------------
    
    DECLARE @fileLocation VARCHAR(128) = '\\server\e$\data\'                        --location of files (parent folder)
    DECLARE @sql NVARCHAR(4000)                                                     --dynamic sql variable
    DECLARE @fileName VARCHAR(128)                                                  --full file name variable if you want to use this
    
    
    ---------------------------------------------------------------------------------------------------------------
    --Get a list of all the file names in the directory
    ---------------------------------------------------------------------------------------------------------------
    
    IF OBJECT_ID('tempdb..#FileNames') IS NOT NULL DROP TABLE #FileNames
    CREATE TABLE #FileNames (
        id int IDENTITY(1,1)
        ,subdirectory nvarchar(512)
        ,depth int
        ,isfile bit)
    INSERT #FileNames (subdirectory,depth,isfile)
    EXEC xp_dirtree @fileLocation, 1, 1
    
    
    --Here's all the files and folders. Note isFile field.
    select * from #FileNames
    
    
    ---------------------------------------------------------------------------------------------------------------
    --Create a cursor to fetch the file names
    ---------------------------------------------------------------------------------------------------------------
    
    DECLARE c CURSOR FOR
    select name from #FileNames where isfile = 1
    
    OPEN c
    FETCH NEXT FROM c INTO @fileName
    
    ---------------------------------------------------------------------------------------------------------------
    --For each file, bulk insert to the proper view, update the proper table, update the log, etc...
    ---------------------------------------------------------------------------------------------------------------
    
    
    WHILE @@FETCH_STATUS = 0
        BEGIN
            --do your bulk insert work
        FETCH NEXT FROM c INTO @fileName
        END
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 2018-04-29
      相关资源
      最近更新 更多