SQL Server 2014批量插入无法正常工作

时间:2015-12-17 21:36:11

标签: sql-server

请原谅新手问题,但我有一个问题我无法解决。尝试将.csv批量插入到SQL Server 2014表中,最后2列为空。无论我尝试过什么,我都无法获取任何数据(不断受到影响)。

文件位于下方,表格的DDL也是如此。尝试使用和不使用格式文件,以及BULK INSERT的众多版本,包括使用和t作为字段终止符,并且r作为行终止符。非常感谢任何和所有帮助,这让我完全陷入困境。

尝试过只使用格式文件,并拼出所有内容(多种变体)。

提前致谢!!

表DDL:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Expireds_New]
(
    [Address] [nvarchar](50) NOT NULL,
    [City] [nvarchar](50) NULL,
    [State] [text] NULL,
    [Zip] [nvarchar](15) NOT NULL,
    [MLS#] [nchar](10) NULL,
    [Status] [varchar](20) NULL,
    [OMD] [date] NULL,
    [Followup_Date] [date] NULL,
    [First_Owner] [varchar](50) NULL,
    [Second_Owner] [varchar](50) NULL,
    [Phone1] [nvarchar](15) NULL,
    [Phone2] [nvarchar](15) NULL,
    [Phone3] [nvarchar](15) NULL,
    [Email] [varchar](50) NULL,
    [Remarks] [nvarchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

SET ANSI_PADDING OFF
GO

FMT文件:

12
15
1       SQLCHAR             0       100     "\t"     1     Address                        SQL_Latin1_General_CP1_CI_AS
2       SQLCHAR             0       100     "\t"     2     City                           SQL_Latin1_General_CP1_CI_AS
3       SQLCHAR             0       0       "\t"     3     State                          SQL_Latin1_General_CP1_CI_AS
4       SQLCHAR             0       30      "\t"     4     Zip                            SQL_Latin1_General_CP1_CI_AS
5       SQLCHAR             0       20      "\t"     5     MLS#                           SQL_Latin1_General_CP1_CI_AS
6       SQLCHAR             0       20      "\t"     6     Status                         SQL_Latin1_General_CP1_CI_AS
7       SQLCHAR             0       11      "\t"     7     OMD                            ""
8       SQLCHAR             0       11      "\t"     8     Followup_Date                  ""
9       SQLCHAR             0       50      "\t"     9     First_Owner                    SQL_Latin1_General_CP1_CI_AS
10      SQLCHAR             0       50      "\t"     10    Second_Owner                   SQL_Latin1_General_CP1_CI_AS
11      SQLCHAR             0       30      "\t"     11    Phone1                         SQL_Latin1_General_CP1_CI_AS
12      SQLCHAR             0       30      "\t"     12    Phone2                         SQL_Latin1_General_CP1_CI_AS
13      SQLCHAR             0       30      "\t"     13    Phone3                         SQL_Latin1_General_CP1_CI_AS
14      SQLCHAR             0       50      "\t"     14    Email                          SQL_Latin1_General_CP1_CI_AS
15      SQLCHAR             0       0       "\r\n"   15    Remarks                        SQL_Latin1_General_CP1_CI_AS

文件格式(csv的前3行):

Address1    City    State   Postal Code Gender  Extension   Fax Web Name    Company Phone Number 1  Phone Number 2  Phone Number 3  Email   Address2
139 27th St Del Mar CA  92014   150045904   Expired 11/2/2015       139 27th Street Llc     16195551212 16195551212 
1021 Costa Pacifica Way  2  Oceanside   CA  92054   150056483   Expired 11/1/2015       2000 Family Trust Nack      16195551212 16195551212 16195551212

使用格式文件和参数批量插入(我使用了此语句的多种变体,包括和不包含格式文件):

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_load_expireds_new] 
AS
BEGIN
    BULK INSERT Prospects.dbo.Expireds_New
    FROM 'C:\prospects\expireds_new_contacts.csv'
    WITH
        (KEEPNULLS,
         FORMATFILE = 'C:\Commands\expireds.FMT',
         FIRSTROW = 2,
         FIELDTERMINATOR = '\t',
         ROWTERMINATOR = '\r\n'
         --ERRORFILE ='C:\Commands\err.txt'
        )
END

仅使用格式文件批量插入:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_load_expireds_new] 
AS
BEGIN
    BULK INSERT Prospects.dbo.Expireds_New
    FROM 'C:\prospects\expireds_new_contacts.csv'
    WITH
        (FORMATFILE = 'C:\Commands\expireds.FMT'
         --ERRORFILE ='C:\Commands\err.txt'
        )
END

没有生成错误,这两个语句都成功完成。但是,当我执行任一过程时,不会导入任何数据。不会对错误文件或SQL Server Management Studio生成错误。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

要指定换行符,而不是使用ROWTERMINATOR ='\ r \ n',请尝试使用ROWTERMINATOR = '0x0A'