2017-04-14 52 views
0

我有一個myNumber varchar(50)列的表,其中存儲的值爲導致零的值,如'0000001111'varchar值的轉換溢出了一個int列,而REPLACE

現在我想替換領先'0000''12',如'12001111'

我想這樣的說法:

UPDATE myDB.dbo.myTable 
SET myNumber = REPLACE(myNumber, '0000', '12') 
WHERE myNumber LIKE '0000%' 

但是,這導致了錯誤:爲什麼這個錯誤造成的,如果所有的列都varchar

Msg 248, Level 16, State 1, Procedure trplist_for_Inserted_Updated_Deleted Line 80
The conversion of the varchar value "831116399075' overflowed an int column.

我該怎麼辦?

修訂

對不起球員,錯誤的原因,它是表觸發。

這裏是觸發邏輯

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER OFF 
GO 

ALTER TRIGGER [dbo].[trplist_for_Inserted_Updated_Deleted] 
ON [dbo].[pList] 
FOR Insert, Update, Delete 
NOT FOR REPLICATION 
AS 
    SET NOCOUNT ON 

    Declare @Result_check Int 
    Declare @NameTable nvarchar(30) 
    set @NameTable='plist' 

    Declare @FieldName nvarchar(30) 
    set @FieldName='ChangeTime' 

    Declare @Columns varbinary(MAX) 
    set @Columns=COLUMNS_UPDATED() 

    Execute CheckChangeFields @Columns, @NameTable, @FieldName, @Result_check 

    if @Result_check = 1 
    begin 
     return 
    end 

    set @FieldName='DateTimeInArchive' 
    Execute CheckChangeFields @Columns, @NameTable, @FieldName, @Result_check 

    if @Result_check = 1 
    begin 
     return 
    end 

    Declare @stateRecord Int 

    IF EXISTS(SELECT * FROM inserted) 
     IF EXISTS(SELECT * FROM deleted) 
     BEGIN 
      SET @stateRecord = 1 --Update 
      --PRINT 'Update' 
     END 
     ELSE 
     BEGIN 
      --PRINT 'Insert' 
      SET @stateRecord = 0 --Insert 
     END 
    ELSE 
    BEGIN 
     --PRINT 'Is DELETE' 
     IF EXISTS(SELECT * FROM deleted) 
     BEGIN 
      SET @stateRecord = 2 --Delete 
      --PRINT 'DELETE' 
     END 
     ELSE 
     BEGIN 
      SET @stateRecord = -1 
      --PRINT 'No DELETE' 
     END 
    END 

    IF @stateRecord = -1 
     RETURN 

    declare @id_value int 
Declare @status_record int 
declare @inn int 
declare @Company int 
declare @tabnumber varchar(50) 
if (@stateRecord in (0,1)) 
BEGIN 
    --inserted or updated 
    --проверка на изменение поля StatusRecord 
declare @Result_check_status_record int 

if Exists(select * from Deleted where checksum(StatusRecord) In (select Checksum(StatusRecord) from Inserted)) 
select @Result_check_status_record= 1--одинаковые 
else 
    select @Result_check_status_record= 0--разные 


    DECLARE cursor_inserted_trpList_Inserted_Updated_Delete_logs CURSOR LOCAL FOR select id, StatusRecord, INN, TabNumber, Company from inserted 
    OPEN cursor_inserted_trpList_Inserted_Updated_Delete_logs 
    FETCH NEXT FROM cursor_inserted_trpList_Inserted_Updated_Delete_logs INTO @id_value, @status_record,@inn, @tabnumber, @Company 
    WHILE @@FETCH_STATUS = 0 
    BEGIN 

    if (@inn<>'') 
    begin 
    if Exists(select id from plist where (id<> @id_value) and ([email protected])) 
    begin 
     RollBack 
     RAISERROR('Данный INN уже имеется в базе', 16,2) 
     CLOSE cursor_inserted_trpList_Inserted_Updated_Delete_logs 
     return 
    end 
    end 

    if (@tabnumber<>'') and (@tabnumber<>'0') 
    begin 
    if @Company = 0 
    begin 

     if Exists(select id from plist where (id<> @id_value) and ([email protected]) and (Company<=0)) 
     begin 
     RollBack 
     RAISERROR('Данный TabNumber уже имеется в базе', 16,2) 
     CLOSE cursor_inserted_trpList_Inserted_Updated_Delete_logs 
     return 
     end 
    end 
    else 
    begin 

     if Exists(select id from plist where (id<> @id_value) and ([email protected]) and ([email protected])) 
     begin 
     RollBack 
     RAISERROR('Данный TabNumber уже имеется в базе в данном подразделении', 16,2) 
     CLOSE cursor_inserted_trpList_Inserted_Updated_Delete_logs 
     return 
     end 
    end 
    end 

    if ((@status_record&1)>0) 
    begin 
    if (@Result_check_status_record=0) 
    begin 
     Execute GustIsRelease @id_value 

    update guest set IDNoFace=0 where [email protected]_value 
    Declare @dmtm datetime 

    if Exists(select id from plist where ([email protected]_value) and (IsNull(DateTimeInArchive, '')='')) 
    begin 
     Update plist set DateTimeInArchive=GetDate() where ([email protected]_value) and (IsNull(DateTimeInArchive, '')='') 
    end 
    end 
    end 
    else 
    begin 

    if Exists(select id from plist where ([email protected]_value) and (IsNull(DateTimeInArchive, 1)<>1)) 
    Update plist set DateTimeInArchive=Null where ([email protected]_value) and (IsNull(DateTimeInArchive, 1)<>1) 
    end 

    FETCH NEXT FROM cursor_inserted_trpList_Inserted_Updated_Delete_logs INTO @id_value, @status_record,@inn, @tabnumber, @Company 
    END 

    CLOSE cursor_inserted_trpList_Inserted_Updated_Delete_logs 
END 


if (@stateRecord=2) 
BEGIN 

    DECLARE cursor_inserted_trpList_Inserted_Updated_Delete_logs CURSOR LOCAL FOR select id from deleted 
    OPEN cursor_inserted_trpList_Inserted_Updated_Delete_logs 
    FETCH NEXT FROM cursor_inserted_trpList_Inserted_Updated_Delete_logs INTO @id_value 
    WHILE @@FETCH_STATUS = 0 
    BEGIN 

    if Exists(select id from pmark where [email protected]_value) 
    begin 
    RollBack 
    RAISERROR('У сотрудника остались активные пароли', 16,2) 
    CLOSE cursor_inserted_trpList_Inserted_Updated_Delete_logs 
    return 
    end 

    if Exists(select id from guest where [email protected]_value) 
    begin 
    RollBack 
    RAISERROR('Сотрудник привязан к посетителю', 16,2) 
    CLOSE cursor_inserted_trpList_Inserted_Updated_Delete_logs 
    return 
    end 

    update guest set IDNoFace=0 where [email protected]_value 
    update guest set ReceiveListId=0 where [email protected]_value 
    FETCH NEXT FROM cursor_inserted_trpList_Inserted_Updated_Delete_logs INTO @id_value 
    END 

    CLOSE cursor_inserted_trpList_Inserted_Updated_Delete_logs 

END 
+0

看起來像你的領域是整數,數量比整數 – FLICKER

+0

的能力有什麼更大trplist_for_inserted_updated_deleted?看起來你在桌上有一個失敗的觸發器。你能發佈觸發邏輯嗎?您發佈的查詢沒有任何問題。 –

+0

此值'831116399075'似乎不包含前導零。你確定'UPDATE'語句是導致問題的原因嗎? –

回答

0

你可以使用STUFF

UPDATE myDB.dbo.myTable 
SET myNumber=stuff(myNumber, 1,4, '12') 
WHERE myNumber LIKE '0000%' 
0

使用可以使用如下SUBSTRING/STUFF:

SUBSTRING:

UPDATE @tblTest 
SET Number='12'+SUBSTRING(Number,5,LEN(NUMBER)) 
WHERE LEFT(Number,4)='0000' 
AND LEN(Number)>4 

STUFF:

UPDATE @tblTest 
SET Number=STUFF(Number, 1,4, '12') 
WHERE LEFT(Number,4)='0000' 
AND LEN(Number)>4 

enter image description here

0

使用串函數來完成的結果ü要

UPDATE myDB.dbo.myTable 
SET Number='12'+SUBSTRING(myNumber,5) 
WHERE SUBSTRING(myNumber,1,4)='0000'; 
相關問題