Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server MSSQL-如何在批量插入中设置默认值?_Sql Server_Sql Server 2005 - Fatal编程技术网

Sql server MSSQL-如何在批量插入中设置默认值?

Sql server MSSQL-如何在批量插入中设置默认值?,sql-server,sql-server-2005,Sql Server,Sql Server 2005,我试图从csv文件中获取包含以下数据的数据 Employee Name: Employee Id : Employee Type : Joining Date Henry : 000123 : Permanent : 01/01/2011 Mathew : 111524 : Contract : 05/04/2011 Steven : 002544 : Permanent : 07/12/2010 Sophia : 015474 :

我试图从csv文件中获取包含以下数据的数据

Employee Name: Employee Id : Employee Type : Joining Date 
Henry   : 000123   : Permanent   : 01/01/2011  
Mathew  : 111524   : Contract    : 05/04/2011  
Steven  : 002544   : Permanent   : 07/12/2010  
Sophia  : 015474   :             : 29/02/2011
Rooney  : 111303   :             : 11/11/2010
现在我要开始了

我有2个cvs文件和2个格式文件,其中1个用于永久类型,1个用于合同类型。现在我想在没有提供数据的情况下设置表中的值。例如,“员工类型”列。如何设置默认值,其中当我运行永久员工cvs时为“employee Type=permanent”,当我运行合同cvs时为“employee Type=contract”

BULK INSERT Employee_Table
    FROM 'C:\Employee.csv'  
    WITH (  
         FIELDTERMINATOR ='';'',  
         FIRSTROW = 2,
         ROWTERMINATOR = ''\n'' 
        )  

使用OPENROWSET以便可以默认字段:

INSERT [dbo].[Employee_Table]
  ([Employee Name], [Employee Id], [Employee Type], [Joining Date])
SELECT [Employee Name]
     , [Employee Id]
     , [Employee Type] = 'Permanent'
     , [Joining Date]
 FROM  OPENROWSET (BULK 'C:\Employee.csv',FORMATFILE='C:\formatfile.xml') as BulkLoadFile

与@Brian建议的方法不同,您可以将数据加载到暂存表,然后使用t-sql将数据转换为所需的方式。如果需要执行比格式化文件更复杂的操作,您可以这样做。您也可以在SSIS包中进行转换,而不是使用批量插入。

您好,Brian,谢谢您的回复。这对我帮助很大,解决了大部分问题,但还有一个问题。“我的平面文件”的最后一行包含平面文件中的数据数及其动态值。如何设置跳过最后一行。?请给我一些建议。谢谢,据我所知不可能。您可以尝试进行计数(*)以获取记录数,然后使用top子句限制导入的记录。