Search code examples
sql-serverbcp

Creating SQL Server format file to use with bulk insert


I want to bulk insert a big pile of data into SQL Server, and thus, I need a format file (I'm not inserting value into all columns).

But using This link and the bcp AdventureWorks2012.HumanResources.Department format nul -c -x -f Department-c..xml –t, -T format, I get an error, pointing at the -t, bit, saying ParentContainsErrorException saying there are missing arguments.

What's wrong with the above?


Solution

  • You need to specify your path to where you want your xml file.

    This here works for me:

    DECLARE @str VARCHAR(1000)
    SET @str = 'bcp AdventureWorks2014.HumanResources.Department format nul -c -x -f D:\Stack\Department-c.xml -t, -T'  
    EXEC xp_cmdshell @str
    GO
    

    Given the result:

    <?xml version="1.0"?>
    <BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <RECORD>
      <FIELD ID="1" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="7"/>
      <FIELD ID="2" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="100" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
      <FIELD ID="3" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="100" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
      <FIELD ID="4" xsi:type="CharTerm" TERMINATOR="\r\n" MAX_LENGTH="24"/>
     </RECORD>
     <ROW>
      <COLUMN SOURCE="1" NAME="DepartmentID" xsi:type="SQLSMALLINT"/>
      <COLUMN SOURCE="2" NAME="Name" xsi:type="SQLNVARCHAR"/>
      <COLUMN SOURCE="3" NAME="GroupName" xsi:type="SQLNVARCHAR"/>
      <COLUMN SOURCE="4" NAME="ModifiedDate" xsi:type="SQLDATETIME"/>
     </ROW>
    </BCPFORMAT>