SQL Server: Bulk Insert into table with computed column

萝らか妹 提交于 2020-01-24 22:23:29

问题


I try to insert data from a textfile into a SQL Server 2016 table with a computed column with bcp.

My bcp command:

bcp Test.dbo.myFirstImport IN D:\myFirstImport.txt -f D:\myFirstImport.xml –T

My table:

CREATE TABLE [dbo].[MyFirstImport](
       [PersonID] [smallint] NULL,
       [FirstName] [varchar](25) NULL,
       [LastName] [varchar](30) NULL,
       [BirthDate] [date] NOT NULL,
       [YearMonthCom]  AS (datepart(year,[BirthDate])*(100)+datepart(month,[BirthDate])) PERSISTED
) ON [PRIMARY]

My Data (tab separated):

1   Anthony   Grosse      1980-02-23
2   Alica     Fatnowna    1963-11-14
3   Stella    Rosenhain   1992-03-02

My 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="CharTerm" TERMINATOR="\t" MAX_LENGTH="7"/>
  <FIELD ID="2" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="25" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="3" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="30" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="4" xsi:type="CharTerm" TERMINATOR="\r\n" MAX_LENGTH="11"/>
  <!--
  <FIELD ID="4" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="11"/>
  <FIELD ID="5" xsi:type="CharTerm" TERMINATOR="\r\n" MAX_LENGTH="12"/>
  -->
 </RECORD>
 <ROW>
  <COLUMN SOURCE="1" NAME="PersonID" xsi:type="SQLSMALLINT"/>
  <COLUMN SOURCE="2" NAME="FirstName" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="3" NAME="LastName" xsi:type="SQLVARYCHAR"/>
  <COLUMN SOURCE="4" NAME="BirthDate" xsi:type="SQLDATE"/>
  <!--
  <COLUMN SOURCE="5" NAME="YearMonthCom" xsi:type="SQLINT"/>
  -->
 </ROW>
</BCPFORMAT>

My error

SQLState = 37000, NativeError = 1934 Error = [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Fehler bei INSERT, da die folgenden SET-Optionen falsche Einstellungen aufweisen: "QUOTED_IDENTIFIER". Überprüfen Sie, ob die SET-Optionen für die Verwendung mit indizierte Sicht en und/oder Indizes für berechnete Spalten und/oder gefilterte Indizes und/oder Abfragebenachrichtigungen und/oder XML-Datentypmethoden und/oder Vorgänge für rä umliche Indizes richtig sind. BCP copy in failed

And in English

INSERT failed because the following SET options have incorrect settings: "QUOTED_IDENTIFIER". Verify that the SET options are correct for use with indexed views and / or indexes on computed columns and / or filtered indexes and / or query notifications and / or XML data type methods and / or spatial indices operations


回答1:


I created another table without that computed column and bcp worked, just to be sure it is an issue with the computed column. Then I recreated the table (with computed column) with QUOTED_IDENTIFIER set to ON - see Jacob's comment - and it still did not work. But when I started bcp with -q it worked. So thank you, Jacob!



来源:https://stackoverflow.com/questions/47436706/sql-server-bulk-insert-into-table-with-computed-column

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