我要从选择列表中插入总共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 )
答案 0 :(得分:1)
为帮助解决此问题,您可能希望在每个列上运行LEN
函数并将其与架构进行比较,以确定导致问题的列:
无需在您的问题中重新发布整个SQL语句......
SELECT LEN(PK1.STRING_VALUE), LEN(PK2.STRING_VALUE), LEN(PK3.STRING_VALUE)
-- ...and so on
这绝对是SQL Server生成的最无用的错误消息之一,因为它几乎无法启动。
答案 1 :(得分:1)
您不仅需要为列使用LEN,还需要使用DATALENGTH。 LEN会在DATALENGTH不会......
的列上附加修剪至于隔离无效的行 - 如果您可以在架构中创建一个单独的表来使用,您可以随时尝试INSERT TOP 100000 ....
到新表中,看看是否可以找出它在哪里中断。一旦你找出行号,就会给你一个开始寻找的好地方。