使用文件名批量插入

时间:2018-07-16 15:50:40

标签: sql sql-server

我正在将csv文件批量插入该表中,

CREATE TABLE [dbo].[XMLData]
(
    [id] [BIGINT] IDENTITY(1,1) NOT NULL,
    [col1] [BIGINT] NOT NULL,
    [col2] [NCHAR](24) NOT NULL,
    [col3] [BIGINT] NOT NULL,
    [col4] [XML] NULL,
    [fname] [NVARCHAR](50) NULL,
    [importedDate] [DATETIME] DEFAULT (GETDATE()) NOT NULL,

    CONSTRAINT [XMLData_pk] 
        PRIMARY KEY CLUSTERED ([id] ASC)
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

批量格式为

13.0
4
1       SQLCHAR            0       42      ","       2     col1 ""
2       SQLCHAR            0       48      ","       3     col2 SQL_Latin1_General_CP1_CI_AS
3       SQLCHAR            0       42      ","       4     col3 ""
4       SQLCHAR            0       0       "\n"       5     col4 ""

[id]是标识列,[fname]是批量插入表中的文件名,我用刚插入到批量之后的文件名来更新[fname]插入,如下所示

DECLARE c1 CURSOR FOR 
     SELECT WHICHPATH, WHICHFILE 
     FROM @fileList 
     WHERE WHICHFILE LIKE '%.csv%'

OPEN c1

FETCH NEXT FROM c1 INTO @path, @filename

WHILE @@fetch_status <> -1
BEGIN
    BEGIN TRY
         BEGIN TRANSACTION 
             SET @sqlc = N'BULK INSERT [dbo].[XMLDATA] FROM ''' + @path + @filename + ''' '
             + '  WITH (FORMATFILE = ''' + @path + 'format.fmt''' + ',TABLOCK )';

             EXEC (@sqlc)

             UPDATE [dbo].[XMLData] 
             SET fname = @filename 
             WHERE fname IS NULL

             COMMIT
    END TRY 
    BEGIN CATCH
        RAISERROR(N'The batch %s did not import!',-1,-1, @filename);   
    END CATCH

    FETCH NEXT FROM c1 INTO @path, @filename
END

CLOSE c1
DEALLOCATE c1

我正在实现大容量插入比插入自身更长的时间之后的UPDATE。
无论如何,是否会将文件名和文件名同时插入[fname]列? 我的第二个问题是,将大容量插入交易中会降低性能吗?

1 个答案:

答案 0 :(得分:1)

使用OPENROWSET代替BULK INSERT,然后可以使用INSERT…SELECT加载表,并添加列。