在REPLACE时,varchar值的转换溢出了int列

时间:2017-04-14 05:55:03

标签: sql-server database

我的表格中包含myNumber varchar(50)列,其中存储了零前导值,例如'0000001111'

现在,我想将'0000'替换为'12',例如'12001111'

我试过这句话:

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

但这导致了一个错误:

  

Msg 248,Level 16,State 1,Procedure trplist_for_Inserted_Updated_Deleted Line 80
  varchar值“831116399075”的转换溢出了一个int列。

如果所有列都是varchar

,为什么会出现此错误

我该怎么办?

已更新

抱歉,伙计们,错误的原因是桌子触发器。

这是触发逻辑

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 (INN=@inn))
     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 (TabNumber=@tabnumber) 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 (TabNumber=@tabnumber) and (Company=@Company))
      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 PListID=@id_value
     Declare @dmtm datetime

     if Exists(select id from plist where (id=@id_value) and (IsNull(DateTimeInArchive, '')=''))
     begin
      Update plist set DateTimeInArchive=GetDate() where (id=@id_value) and (IsNull(DateTimeInArchive, '')='')
     end
    end
   end
   else
   begin

    if  Exists(select id from plist where (id=@id_value) and (IsNull(DateTimeInArchive, 1)<>1))
     Update plist set DateTimeInArchive=Null where (id=@id_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 owner=@id_value)
   begin
     RollBack
     RAISERROR('У сотрудника остались активные пароли', 16,2)
     CLOSE cursor_inserted_trpList_Inserted_Updated_Delete_logs
     return
   end

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

   update guest set IDNoFace=0 where IDNoFace=@id_value
   update guest set ReceiveListId=0 where ReceiveListId=@id_value
   FETCH NEXT FROM cursor_inserted_trpList_Inserted_Updated_Delete_logs INTO  @id_value
  END

  CLOSE cursor_inserted_trpList_Inserted_Updated_Delete_logs

END

3 个答案:

答案 0 :(得分:0)

您可以使用var Jdata = { "file-0": { "name": "2012-11-23-JokeExplanationCorner.jpg", "type": "image\/jpeg", "tmp_name": "C:\\xampp2\\tmp\\phpA93E.tmp", "error": 0, "size": 378086 }, "file-1": { "name": "11039919_10206003688989240_2065769962_n.jpg", "type": "image\/jpeg", "tmp_name": "C:\\xampp2\\tmp\\phpA940.tmp", "error": 0, "size": 143703 } } var length = Object.keys(Jdata).length; for (var i=0; i< length; i++) { console.log(" name is = " + Jdata['file-'+i]['name']); }

STUFF

答案 1 :(得分: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

答案 2 :(得分:0)

使用substring函数来完成你想要的结果

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