"字符串或二进制数据将被截断"更新时出错,但插入新记录时没有

时间:2015-11-05 13:24:33

标签: sql-server sql-server-2008

据我所知,当我们尝试插入超过列容量的字符时会出现此错误。

但是我无法理解的是最近的输出,其中我在表中更新时遇到错误,但是当新记录插入到具有相同参数大小的表中时没有。

表格结构:

Entity 
(
     @EntityID       int,  
     @Name           varchar(200),  
     @FullName       varchar(50),  
     @ParentEntityID int,  
     @ShortDesc      varchar(255),  
     @LongDesc       varchar(2000)  
)

当我尝试插入新行时,它只会截断值大于其大小的参数(此处为@FullName ),但是当我尝试更新现有的相同值时记录,它只是抛出异常。

为什么它也不能在这里截断?

编辑:: 更新程序。

    ALTER PROCEDURE [dbo].[Entity_Update]  
    @EntityID       int,  
    @Name           varchar(200),  
    @FullName       varchar(410),  
    @ParentEntityID int,  
    @ShortDesc      varchar(255),  
    @LongDesc       varchar(2000)  
AS  

set nocount on;  

update dbo.Entity set  
    Name = @Name,  
    FullName = @FullName,  
    ParentEntityID = @ParentEntityID,  
    ShortDesc = @ShortDesc,  
    LongDesc = @LongDesc,  
    DateUpdated = GETUTCDATE()  
where EntityID = @EntityID;  

return 0;

INSERT程序是:

   ALTER PROCEDURE [dbo].[Entity_Insert]    
    @Name           varchar(200),    
    @FullName       varchar(410),    
    @ParentEntityID int,    
    @ShortDesc      varchar(255),    
    @LongDesc       varchar(2000),    
    @UserID         int,    
    @EntityID       int OUTPUT    
AS    

set nocount on;    
set xact_abort on;    

begin tran    

insert dbo.Entity (    
    Name,    
    FullName,    
    ParentEntityID,    
    ShortDesc,    
    LongDesc,    
    RecordStatus,    
    DateUpdated)    
values (@Name,    
        @FullName,    
        @ParentEntityID,    
        @ShortDesc,    
        @LongDesc,    
        1,    
        GETUTCDATE());    

select @EntityID = SCOPE_IDENTITY();  

1 个答案:

答案 0 :(得分:0)

CREATE TABLE Entity
  (
    EntityID INT IDENTITY
                 NOT NULL
                 PRIMARY KEY ,
    Name VARCHAR(200) ,
    FullName VARCHAR(50) ,
    ParentEntityID INT ,
    ShortDesc VARCHAR(255) ,
    LongDesc VARCHAR(2000)
  );



INSERT  [Entity] ( [Name], [FullName], [ParentEntityID], [ShortDesc],
                   [LongDesc] )
VALUES  ( 'Name', 'FullName', 0, 'Short', 'Long' );

UPDATE  [Entity]
SET     [LongDesc] = 'Here is an updated value'
WHERE   [FullName] = 'FullName';

SELECT  *
FROM    [Entity] AS [e];
UPDATE  [Entity]
SET     [LongDesc] = LEFT('Yet another update ' + REPLICATE('blah ', 1000),
                          2000)
WHERE   [FullName] = 'FullName';
SELECT  *
FROM    [Entity] AS [e];

非常适合我。