字符串截断,ANSI_WARNINGS OFF

时间:2011-03-28 21:19:03

标签: sql string sql-server-2008 error-handling warnings

使用SQL Server 2008(R2)。 我在不同的数据库中有两个表(具有相同的列数和数据类型 - 但是大小不同) 。我正在将值从一个插入另一个,但问题是源表说:nvarchar(200),目标表有一个类型为nvarhchar(100)的字段。 并且源表中的数据包含大于100个字符的字段,因此错误:

*String or binary data would be truncated is thrown.*

我尝试使用

-- SourceServer is passed in at command prompt (batch)
    SET ANSI_WARNINGS OFF
    GO

    INSERT INTO DestinationTable(col1, col2,...) 
    SELECT col1, col2, ... 
    FROM $SourceServer.dbo.SourceTable 

    SET ANSI_WARNINGS ON
    GO

然而,这会抛出一个如下所示的错误: “ INSERT failed because the following SET options have incorrect settings: 'ANSI_WARNINGS'. Verify that 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 index operations.

关于如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:3)

您可以自己截断数据:

insert into desttable(destcolumn)
select left(sourcecolumn, 100) from sourcetable