-2

我共有 22025 条记录要从选择列表中插入。当我执行以下脚本时,错误提示“字符串或二进制数据将被截断”。而如果我在选择列表中添加一些 where 子句以减少一次插入的记录数,例如第一次插入 10000 条记录,第二次插入 12025 条记录,则没有错误提示。我检查了每个字段的长度,不超过目标列的长度。请指教。谢谢!

INSERT INTO [BWX] 
SELECT PK1.STRING_VALUE,PK2.STRING_VALUE,PK3.STRING_VALUE,PK4.STRING_VALUE,
       2000010, BW_ROW.ID AS BW_ROW_ID, BW_ROW.SEQUENCE_NUMBER, 
       0 AS UPDATE_FLAG, 0 AS DELETE_FLAG, BW_ROW.IS_ACTIVE 
FROM BW_CELL AS PK1, BW_COLUMN AS PK1COL, BW_CELL AS PK2, BW_COLUMN AS PK2COL, 
     BW_CELL AS PK3, BW_COLUMN AS PK3COL, BW_CELL AS PK4, BW_COLUMN AS PK4COL, 
     BW_COLUMN AS PK9COL, BW_ROW 
WHERE PK1.BW_ROW_ID = BW_ROW.ID AND PK1.PTN_KEY = 100 
      AND PK1.BW_COLUMN_ID = PK1COL.ID AND PK1COL.NAME = 'key_1' 
      AND PK2.BW_ROW_ID = BW_ROW.ID AND PK2.PTN_KEY = 100 
      AND PK2.BW_COLUMN_ID = PK2COL.ID AND PK2COL.NAME = 'key_2' 
      AND PK3.BW_ROW_ID = BW_ROW.ID AND PK3.PTN_KEY = 100 
      AND PK3.BW_COLUMN_ID = PK3COL.ID AND PK3COL.NAME = 'key_3' 
      AND PK4.BW_ROW_ID = BW_ROW.ID AND PK4.PTN_KEY = 100 
      AND PK4.BW_COLUMN_ID = PK4COL.ID AND PK4COL.NAME = 'key_4' 
      AND BW_ROW.BW_TBL_ID = 2000010 AND BW_ROW.IS_ACTIVE = 1 AND BW_ROW.TX_ID > -1 
      AND NOT EXISTS ( SELECT [BWX_ROW@DEVICE_FORECAST].BW_ROW_ID 
                       FROM [BWX_ROW@DEVICE_FORECAST] 
                       WHERE BW_ROW.ID = [BWX_ROW@DEVICE_FORECAST].BW_ROW_ID ) 
4

2 回答 2

1

您不仅需要对列使用 LEN,还需要使用 DATALENGTH。LEN 将在DATALENGTH不会的列上附加修剪...

至于隔离无效的行——如果你可以在你的模式中创建一个单独的表来使用,你总是可以尝试INSERT TOP 100000 ....进入新表,看看你是否能找出它在哪里中断。一旦你找出了行号,这给了你一个开始寻找的好地方。

于 2013-08-14T03:33:36.247 回答
1

为了帮助解决此问题,您可能希望LEN在每个列上运行该函数并将其与架构进行比较,以找出导致问题的原因:

无需在您的问题中重新发布整个 SQL 语句...

SELECT LEN(PK1.STRING_VALUE), LEN(PK2.STRING_VALUE), LEN(PK3.STRING_VALUE)
-- ...and so on

这绝对是 SQL Server 生成的最无用的错误消息之一,因为它让您几乎无处可去。

于 2013-08-14T02:31:06.337 回答