SQL Agent Job Error - String or binary data would be truncated. [SQLSTATE 22001] (Error 8152)

  • I'm running into the following message, "String or binary data would be truncated. [SQLSTATE 22001] (Error 8152)" when running a sql agent job. I'm attempting to execute a stored procedure through the job. Keep in mind that when I run the stored procedure in a normal query window, it works fine and only fails when running it as a scheduled job. My guess is that it has to do with how SQL Jobs execute procedures (especially long procedures) but I have no idea how to fix this. If I use Set Ansi_Warnings OFF, the job will work fine, however, I don't know what other issues this may cause.

    Thanks for the help.

  • Hey Josh,

    From what i've read after doing some searches is, we would need to know what code is running. Alot of people that have had the same issue had formatting issues.

    Cheers,

    Tac

  • I was able to grab the specific code that's causing the problem, here you go,

    USE [ARCHIVE]

    GO

    /****** Object: StoredProcedure [dbo].[SP_Archive_Cleanup] Script Date: 09/20/2013 22:54:57 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    ALTER PROCEDURE [dbo].[SP_Archive_Cleanup] @TABLE VARCHAR(10)

    AS

    BEGIN

    IF (@TABLE='ALL')

    BEGIN

    DELETE

    FROM PRODUCTION.dbo.TABLE1

    WHERE PK IN (SELECT PK FROM ARCHIVE.dbo.TABLE1)

    DELETE

    FROM PRODUCTION.dbo.TABLE2

    WHERE PK IN (SELECT PK FROM ARCHIVE.dbo.TABLE2)

    DELETE

    FROM PRODUCTION.dbo.TABLE3

    WHERE PK IN (SELECT PK FROM ARCHIVE.dbo.TABLE3)

    DELETE

    FROM PRODUCTION.dbo.TABLE4

    WHERE PK IN (SELECT PK FROM ARCHIVE.dbo.TABLE4)

    DELETE

    FROM PRODUCTION.dbo.TABLE5

    WHERE PK IN (SELECT PK FROM ARCHIVE.dbo.TABLE5)

    DELETE

    FROM PRODUCTION.dbo.TABLE6

    WHERE PK IN (SELECT PK FROM ARCHIVE.dbo.TABLE6)

    END

    IF (@TABLE = 'TABLE1')

    BEGIN

    DELETE

    FROM PRODUCTION.dbo.TABLE1

    WHERE PK IN (SELECT PK FROM ARCHIVE.dbo.TABLE1)

    END

    IF (@TABLE = 'TABLE2')

    BEGIN

    DELETE

    FROM PRODUCTION.dbo.TABLE2

    WHERE PK IN (SELECT PK FROM ARCHIVE.dbo.TABLE2)

    END

    IF (@TABLE = 'TABLE3')

    BEGIN

    DELETE

    FROM PRODUCTION.dbo.TABLE3

    WHERE PK IN (SELECT PK FROM ARCHIVE.dbo.TABLE3)

    END

    IF (@TABLE ='TABLE4')

    BEGIN

    DELETE

    FROM PRODUCTION.dbo.TABLE4

    WHERE PK IN (SELECT PK FROM ARCHIVE.dbo.TABLE4)

    END

    IF (@TABLE = 'TABLE5')

    BEGIN

    DELETE

    FROM PRODUCTION.dbo.TABLE5

    WHERE PK IN (SELECT PK FROM ARCHIVE.dbo.TABLE5)

    END

    IF (@TABLE = 'TABLE6')

    BEGIN

    DELETE

    FROM PRODUCTION.dbo.TABLE6

    WHERE PK IN (SELECT PK FROM ARCHIVE.dbo.TABLE6)

    END

    END

  • Hello Josh,

    The other posts that i've seen the actual variable was just too small to hold the information at some point. What i would do is try increasing the VARCHAR(10) to something like VARCHAR(20) and see if the code runs 🙂

    you may have to play around with that size to see what works.

    Please let me know if that works out for you.

    Cheers,

    Tac

  • may be the value you are passing to the variable @table is greater than varchar(10) which results in string truncation..

    please check this

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • use sp_help TABLENAME from production and archive, and checked if data type are same in both table.

    in this case our interest is PK column

  • Unfortunately, it's none of those issues. However, I did notice something new.

    I removed the stored procedure from my original large procedure as it's not necessarily needed, but it does fix some potential issues that could arise. I just wanted to see what happened if I did that. When I execute the job now, it says it completed, however, even though no errors are displayed the script didn't work. When I run the same exact procedure and script manually, it works fine. I've been looking around on some forums and it seems like there are quite a few people who have similar issues running large scripts and procedures via sql agent jobs. I'm going to try to run the script via a bat file to see how it works. If it works fine, then I'm going to write sql agent jobs off as just being finicky.

    Perhaps one of you may know if there's a good writeup behind sql jobs somewhere that explains exactly how it works. That could help me understand why this job keeps failing.

    Thanks,

    Josh

  • I realize this is an old post - but perhaps others - like me find this entry due to a similar problem. In my case I had an insert statement with a select statement to load a reporting table. The solution turned out to be that I had a field name that had a space in it and I had used double quotes around the field (not smart) Once I changed it to [] around the field name the problem went away.

  • I'm having trouble with what seems to be this exact issue. I have a stored procedure that has been working for years, and suddenly I get this error about string or binary data being truncated. I inferred the stored procedure in which the error occured, ran it manually to identify the errant statement, but no dice, it ran just fine. Numerous iterations of speculative fixings produced nothing, until I tried SET ANSI_WARNING OFF. SQL Job Monitor reported the same error as before, but my custom log table shows that the process actually made it a little farther than before. 

    If ANYONE has a clue why this is happening, please reply! I suspect that this error is internal to the SQL Job processor, and not related to the code. Thanks!

  • Jesse McLain - Monday, September 11, 2017 9:34 AM

    I'm having trouble with what seems to be this exact issue. I have a stored procedure that has been working for years, and suddenly I get this error about string or binary data being truncated. I inferred the stored procedure in which the error occured, ran it manually to identify the errant statement, but no dice, it ran just fine. Numerous iterations of speculative fixings produced nothing, until I tried SET ANSI_WARNING OFF. SQL Job Monitor reported the same error as before, but my custom log table shows that the process actually made it a little farther than before. 

    If ANYONE has a clue why this is happening, please reply! I suspect that this error is internal to the SQL Job processor, and not related to the code. Thanks!

    If you have truncation errors when running a stored procedure through a job in SQL Server Agent but no errors when executing manually, try changing the textsize option. When Agent logs in to execute a job, the default textsize is 1024.
    In the job step before executing the stored procedure, try setting the text size to something greater than 1024. For example:
    SET TEXTSIZE 5000

    Sue

  • Hi Sue, thanks for your reply! I added a line SET TEXTSIZE 2147483647, and the stored procedure worked once, but now I'm having the same problem. This is the error I get:

    09/17/2017 11:39:33,sql_job_name,Error,8,sql_server_name,sql_step_name,sql_sproc_name,,Executed as user: user_name. Warning! The maximum key length is 900 bytes. The index 'idx_name' has maximum length of 8917 bytes. For some combination of large values<c/> the insert/update operation will fail. [SQLSTATE 01000] (Message 1945)  Warning: The maximum length of the row exceeds the permissible limit of 8060 bytes. For some combination of large values<c/> the insert/update operation will fail. [SQLSTATE 01000] (Message 1981)  String or binary data would be truncated. [SQLSTATE 22001] (Error 8152)  The statement has been terminated. [SQLSTATE 01000] (Error 3621).  The step failed.,07:58:16,16,3621,,,,0

  • Jesse McLain - Monday, September 18, 2017 8:05 AM

    Hi Sue, thanks for your reply! I added a line SET TEXTSIZE 2147483647, and the stored procedure worked once, but now I'm having the same problem. This is the error I get:

    09/17/2017 11:39:33,sql_job_name,Error,8,sql_server_name,sql_step_name,sql_sproc_name,,Executed as user: user_name. Warning! The maximum key length is 900 bytes. The index 'idx_name' has maximum length of 8917 bytes. For some combination of large values<c/> the insert/update operation will fail. [SQLSTATE 01000] (Message 1945)  Warning: The maximum length of the row exceeds the permissible limit of 8060 bytes. For some combination of large values<c/> the insert/update operation will fail. [SQLSTATE 01000] (Message 1981)  String or binary data would be truncated. [SQLSTATE 22001] (Error 8152)  The statement has been terminated. [SQLSTATE 01000] (Error 3621).  The step failed.,07:58:16,16,3621,,,,0

    These errors have nothing to do with the previous one with the stored procedure, job step, etc.
    It's whatever you are doing in the job step where the Index Key size exceeds the maximum and the row size exceeds the maximums. 
    You can find more information on both of these requirements in these articles:
    Maximum Size of Index Keys
    Row-Overflow Data Exceeding 8 KB

    Sue

  • Do you have trigger on the table that you want insert record

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply