bcp import error with xml format file and identity column

跟風遠走 提交于 2019-12-09 02:07:27

问题


I created a table in SQL server like:

CREATE TABLE [dbo].[    
    [myId] [smallint] IDENTITY(1,1) NOT NULL,
    [name] [nchar](10) NOT NULL,
    [value] [int] NOT NULL,
CONSTRAINT [PK_metadado] PRIMARY KEY CLUSTERED 
(
  [myId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

I want to import a file in my table using xml formater. I got a problem because my table had "myId". I think it's a bug in bcp because, if i don't add myId column, the importation works fine.

File:

Test      0010000290

Xml format file:

<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="CharFixed" LENGTH="10"/>
    <FIELD ID="2" xsi:type="CharFixed" LENGTH="5"/>     
    <FIELD ID="3" xsi:type="CharFixed" LENGTH="5"/> 
</RECORD>
<ROW>
    <COLUMN SOURCE="3" NAME="value" xsi:type="SQLINT" />
    <COLUMN SOURCE="1" NAME="name" xsi:type="SQLCHAR" />
</ROW>
</BCPFORMAT>

Output:

Starting copy...
SQLState = 23000, NativeError = 515
Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Cannot insert the value NULL into column 'value', table 'XXX.dbo.metadata'; column does not allow nulls. INSERT fails.
SQLState = 01000, NativeError = 3621
Warning = [Microsoft][SQL Server Native Client 10.0][SQL Server]The statement has been      terminated.
BCP copy in failed

Editing

@MatthewMartin: The "value" came null whit my first format. It works if i create this strange xml format with null column

<COLUMN SOURCE="2" NAME="null" xsi:type="SQLCHAR" />
<COLUMN SOURCE="1" NAME="name" xsi:type="SQLCHAR" />
<COLUMN SOURCE="3" NAME="value" xsi:type="SQLINT" />    

回答1:


It is extremely unlikely that you have found a bug in a well-known, intensively used and exhaustively documented tool. It is far more likely that you haven't found the correct combination of formatting instructions or are making some other mistake.

Having said that, it's still not entirely clear (to me) what you want to achieve. My best understanding is that your file has 3 fixed-length values, your table has 3 columns, and you want to copy 2 values from the file to 2 columns in the table, so that you end up with 'Test' in the name column and 100 in the value column?

That would mean you want to skip the last value in the file and the first column in the table. Note this quote from the documentation:

With an XML format file, you cannot skip a column when you are importing directly into a table by using a bcp command or a BULK INSERT statement. However, you can import into all but the last column of a table. If you have to skip any but the last column, you must create a view of the target table that contains only the columns contained in the data file. Then, you can bulk import data from that file into the view.

To use an XML format file to skip a table column by using OPENROWSET(BULK...), you have to provide explicit list of columns in the select list and also in the target table, as follows:

INSERT ... SELECT FROM OPENROWSET(BULK...)

Based on all of that background, you can either create a view and use bcp.exe or just use OPENROWSET() with the table, which I think is easier:

The table:

CREATE TABLE [dbo].metadata (
    [myId] [smallint] IDENTITY(1,1) NOT NULL,
    [name] [nchar](10) NOT NULL,
    [value] [int] NOT NULL,
    CONSTRAINT [PK_metadado] PRIMARY KEY CLUSTERED ([myId] ASC)
)

The data file (row terminated with a Windows newline, i.e. CR+LF, see example F under sample XML format files):

Test      0010000290

The format file:

<?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="CharFixed" LENGTH="10"/>
  <FIELD ID="2" xsi:type="CharFixed" LENGTH="5"/>
  <FIELD ID="3" xsi:type="CharTerm" TERMINATOR="\r\n"/>
 </RECORD>
 <ROW>
  <COLUMN SOURCE="1" NAME="name" xsi:type="SQLNCHAR"/>
  <COLUMN SOURCE="2" NAME="value" xsi:type="SQLINT"/>
 </ROW>
</BCPFORMAT>

The command:

insert into dbo.metadata ([name], [value])
select [name], [value]
from openrowset(bulk 'C:\SomeFolder\data.bcp',
    formatfile = 'C:\SomeFolder\format.xml'
    ) dt

Finally, a couple of other small points. Please always mention which version and edition of SQL Server you're using: many features are only available in specific versions/editions. You should also review your column names; I know you may have simply used them for a quick example here, but they are not very descriptive and VALUE is an ODBC reserved keyword that Microsoft recommends should not be used in SQL Server.



来源:https://stackoverflow.com/questions/8347254/bcp-import-error-with-xml-format-file-and-identity-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!