2

我正在尝试将一些数据从固定宽度的文本文件加载到视图中,插入被代替触发器拦截。

应该在触发器中从 INSERTED 输出值的调试选择没有被触发,所以我怀疑故障点是批量插入本身。

以下是详细信息:这是我正在使用的:

启动批量插入命令如下所示:

BULK INSERT Staging.vViewSolelyForInsertion
FROM 'X:\path\to\datafile.txt'
WITH
(
    FORMATFILE='X:\path\to\formatfile.xml',
    FIRE_TRIGGERS,
    ERRORFILE='X:\path\to\Error.log'
)

用于 Staging.vViewSolelyForInsertion 的 CREATE 命令:

CREATE VIEW Staging.vViewSolelyForInsertion
AS
SELECT
    CONVERT(NVARCHAR(4), NULL, 0) AS Door,
    CONVERT(NVARCHAR(9), NULL, 0) AS Chair,
    CONVERT(NVARCHAR(24), NULL, 0) AS Office,
    CONVERT(NVARCHAR(10), NULL, 0) AS Telephone,
    CONVERT(NVARCHAR(4), NULL, 0) AS Cup,
    CONVERT(NVARCHAR(1), NULL, 0) AS Headphones,
    CONVERT(NVARCHAR(20), NULL, 0) AS Mouse,
    CONVERT(NVARCHAR(12), NULL, 0) AS Keyboard

用于创建触发器的 CREATE 命令

CREATE TRIGGER Staging.ON_INSERT_Staging_vViewSolelyForInsertion
ON Staging.vRefViewSolelyForInsertion
BEGIN

    SELECT * FROM INSERTED -- debug, the error message appears before this point.

    /*
        This table exists to generate the InternalId.
    */  
    DECLARE @tbl TABLE
    (
        InternalId UNIQUEIDENTFIER NOT NULL DEFAULT NEWID(),
        Door,
        Chair,
        Office,
        Telephone,
        Cup,
        Headphones,
        Mouse,
        Keyboard
    )

    INSERT INTO @tbl
    (
        Door,
        Chair,
        Office,
        Telephone,
        Cup,
        Headphones,
        Mouse,
        Keyboard
    )
    SELECT
        i.Door,
        i.Chair,
        i.Office,
        i.Telephone,
        i.Cup,
        i.Headphones,
        i.Mouse,
        i.Keyboard
    FROM INSERTED i

    SELECT * FROM @tbl -- 2nd debug

    INSERT INTO Staging.ActualTable
    (
        InternalId,
        Door,
        Chair,
        Office,
        Telephone,
        Cup,
        Headphones,
        Mouse,
        Keyboard
    )
    SELECT
        -- Lots of processing goes on here. 
        t.InternalId,
        ProcessDoor(t.Door, t.InternalId),
        ProcessChair(t.Chair, t.InternalId),
        ProcessOffice(t.Office, t.InternalId),
        ProcessTelephone(t.Telephone, t.InternalId),
        ProcessCup(t.Cup, t.InternalId),
        ProcessHeadphones(t.Headphones, t.InternalId),
        ProcessMouse(t.Mouse, t.InternalId),
        ProcessKeyboard(t.Keyboard, t.InternalID)
    FROM @tbl t

    DELETE FROM @tbl

END

这是我正在使用的格式文件

<?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="NCharFixed"   LENGTH="4"  COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="2"     xsi:type="NCharFixed"   LENGTH="9"  COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="3"     xsi:type="NCharFixed"   LENGTH="24" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="4"     xsi:type="NCharFixed"   LENGTH="10" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="5"     xsi:type="NCharFixed"   LENGTH="4"  COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="6"     xsi:type="NCharFixed"   LENGTH="4"  COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="7"     xsi:type="NCharFixed"   LENGTH="1"  COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="8"     xsi:type="NCharFixed"   LENGTH="10" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="9"     xsi:type="NCharFixed"   LENGTH="10" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="10"    xsi:type="NCharFixed"   LENGTH="4"  COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="11"    xsi:type="NCharFixed"   LENGTH="9"  COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="12"    xsi:type="NCharFixed"   LENGTH="20" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="13"    xsi:type="NCharFixed"   LENGTH="12" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
  <FIELD ID="14"    xsi:type="CharTerm"     TERMINATOR="\r\n" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
 </RECORD>
 <ROW>
  <COLUMN SOURCE="1"    NAME="Door"         LENGTH="4"      xsi:type="SQLNVARCHAR"/>
  <COLUMN SOURCE="2"    NAME="Chair"        LENGTH="9"      xsi:type="SQLNVARCHAR"/>
  <COLUMN SOURCE="3"    NAME="Office"       LENGTH="24"     xsi:type="SQLNVARCHAR"/>
  <COLUMN SOURCE="4"    NAME="Telephone"    LENGTH="10"     xsi:type="SQLNVARCHAR"/>
  <COLUMN SOURCE="5"    NAME="Cup"          LENGTH="4"      xsi:type="SQLNVARCHAR"/>
  <COLUMN SOURCE="7"    NAME="Headphones"   LENGTH="1"      xsi:type="SQLNVARCHAR"/>
  <COLUMN SOURCE="12"   NAME="Mouse"        LENGTH="20"     xsi:type="SQLNVARCHAR"/>
  <COLUMN SOURCE="13"   NAME="Keyboard"     LENGTH="12"     xsi:type="SQLNVARCHAR"/>
 </ROW>
</BCPFORMAT>

数据文件由 \r\n 终止行组成,数据字段之间没有分隔符,并以 unicode 格式保存。每行长度为 138 个字符,加上行尾的 \r\n。

我得到的错误如下:

第 1 行第 2 列(主席)的批量加载数据转换错误(截断)。

ImportFile.xml 中的字段长度与 ImportFile.xml 中的列长度相同,与 Staging.vViewSolelyForInsertion 中的 nvarchar 长度相同。如上所述(我知道这是很久以前的事了),触发器中的第一个调试语句没有被执行,导致我认为是 BULK INSERT 失败了。

提前致谢!

4

1 回答 1

0

这一切都是徒劳的。将 xsi:type 设置为 CharFixed 而不是 NCharFixed 解决了这个问题,即使我使用的是 Unicode。现在我正在处理我的数据中的奇怪字符(在输入文件中显示为普通文本)。

于 2013-01-23T12:26:05.257 回答