如何解决此错误:转换nvarchar值时转换失败' String或二进制数据将被截断。'到数据类型int

时间:2014-11-24 06:50:42

标签: sql-server

我的表: -

ID  int Unchecked
ColName varchar(255)    Unchecked
Title   varchar(255)    Unchecked
DataSource  varchar(255)    Checked
ControlID   int Unchecked
MaxLength   int Unchecked
Width   int Unchecked
Height  int Unchecked
IsMandatory bit Unchecked
CommandArea char(1) Checked
CreatedBy   int Checked
CreatedOn   datetime    Checked
UpdatedOn   datetime    Checked
Status  bit Checked
UpdatedBy   int Checked
IPAddress   varchar(1)  Checked

我的存储过程:

ALTER PROCEDURE [dbo].[Spaddvoucherreachcols] @xml   XML,
                                              @error VARCHAR(1000) output
AS
  BEGIN
      BEGIN TRY
          INSERT INTO VoucherReachCols
                      (ColName,
                       Title,
                       DataSource,
                       ControlID,
                       MaxLength,
                       Width,
                       Height,
                       IsMandatory,
                       CreatedBy,
                       CreatedOn,
                       UpdatedOn,
                       Status,
                       UpdatedBy,
                       IPAddress)
          SELECT N.value('(Title)[1]', 'Varchar(MAX)'),
                 N.value('(ColName)[1]', 'Varchar(MAX)'),
                 N.value('(DataSource)[1]', 'Varchar(MAX)'),
                 N.value('(ControlID)[1]', 'NVarchar(MAX)'),
                 N.value('(MaxLength)[1]', 'NVarchar(MAX)'),
                 N.value('(Width)[1]', 'NVarchar(MAX)'),
                 N.value('(Height)[1]', 'NVarchar(MAX)'),
                 N.value('(IsMandatory)[1]', 'Varchar(MAX)'),
                 N.value('(CreatedBy)[1]', 'int'),
                 Getdate(),
                 Getdate(),
                 1,
                 N.value('(UpdatedBy)[1]', 'int'),
                 N.value('(IPAddress)[1]', 'Varchar(20)')
          FROM   @XML.nodes('/Table/VoucherReachCols') AS T(N);
      END TRY

      BEGIN CATCH
          SET @Error = Error_number() + ' ' + Error_message();
      END CATCH
  END

1 个答案:

答案 0 :(得分:0)

错误非常自我解释。尝试替换

N.value('(ControlID)[1]', 'NVarchar(MAX)'),
N.value('(MaxLength)[1]', 'NVarchar(MAX)'),

N.value('(ControlID)[1]', 'int'),
N.value('(MaxLength)[1]', 'int'),

编辑:检查源表:IPAddress varchar(1) - 在实际插入中我注意到varchar(20)的IPAddress。您应该更改表并增加此列宽。