如何使用列的动态值批量插入

时间:2017-06-08 15:40:46

标签: php sql-server database sql-server-2005 bulkinsert

情况如下:

我有200个带有不同名称的txt文件,例如 601776.txt 每个文件的名称实际上是一个ID_foo,它包含一些这样的数据(2列):

04004 Albánchez
04006 Albox
04008 Alcóntar
04009 Alcudia de Monteagud
.
.
.

现在我想 BULK INSERT 这些TXT文件到一个SQL Server表中,其中有3列,其中一列应该是txt文件的名称。我正在使用PHP脚本,所以我做了一个循环来获取文件名然后是什么?

BULK INSERT Employee_Table
    FROM '../home/601776.txt'  
    WITH (  
         FIELDTERMINATOR ='\t',  
         ROWTERMINATOR = ''\n'' 
        )  

如何在每个循环中使用$ file_name变量批量插入时设置第三列?

如果可以通过逐行读取txt文件来插入表格,您认为这是一个更好的主意吗?怎么样?

由于

1 个答案:

答案 0 :(得分:0)

这是光标在SQL Server中实际理想的少数几次之一。这是一种方式。一旦看到PRINT语句并且满意,就可以将其注释掉并取消注释掉它下面的两行。我添加了一些逻辑来添加文件名和通常需要的处理日期,但是您的表定义需要这些列。它应该得到这个想法。

---------------------------------------------------------------------------------------------------------------
--Set some variables
---------------------------------------------------------------------------------------------------------------

DECLARE @dt VARCHAR(10)                                                         --date variable but stored as VARCHAR for formatting of file name
DECLARE @fileLocation VARCHAR(128) = 'E:\DATA_TRANSFERS\'                       --production location which is \\issqlstd01 but the xp_dirtree didn't like this
DECLARE @sql NVARCHAR(4000)                                                     --dynamic sql variable
DECLARE @fileName VARCHAR(128)                                                  --full file name variable


---------------------------------------------------------------------------------------------------------------
--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





---------------------------------------------------------------------------------------------------------------
--Create a cursor to fetch the file names
---------------------------------------------------------------------------------------------------------------

DECLARE c CURSOR FOR
select subdirectory from #FileNames where isfile = 1

OPEN c
FETCH NEXT FROM c INTO @fileName

---------------------------------------------------------------------------------------------------------------
--For each file, bulk insert 
---------------------------------------------------------------------------------------------------------------


WHILE @@FETCH_STATUS = 0
    BEGIN

        SET @sql = 'BULK INSERT Employee_Table FROM '''+ @fileLocation + @fileName +''' WITH (FIELDTERMINATOR = ''\t'',KEEPNULLS,ROWTERMINATOR = ''0x0a'')'


        --Try the bulk insert, if error is thrown log the error 
        --Also update the Table Columns which aren't a part of the original file (load date and original file name)
        BEGIN TRY
            PRINT(@sql)
            --EXEC(@sql)
            --UPDATE Employee_Table SET OrigFile = @fileName, LoadDate = GETDATE() WHERE OrigFile IS NULL
        END TRY
        BEGIN CATCH
            SELECT ERROR_MESSAGE()
        END CATCH

        FETCH NEXT FROM c INTO @fileName
    END

CLOSE c
DEALLOCATE c

GO