Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL Agent Job Error - String or binary data would be truncated. [SQLSTATE 22001] (Error 8152) Expand / Collapse
Author
Message
Posted Friday, September 20, 2013 11:39 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, November 26, 2014 1:51 PM
Points: 148, Visits: 315
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.
Post #1497110
Posted Friday, September 20, 2013 11:45 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, April 17, 2014 8:45 AM
Points: 5, Visits: 61
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
Post #1497112
Posted Saturday, September 21, 2013 12:00 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, November 26, 2014 1:51 PM
Points: 148, Visits: 315
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
Post #1497113
Posted Saturday, September 21, 2013 11:29 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, April 17, 2014 8:45 AM
Points: 5, Visits: 61
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
Post #1497152
Posted Saturday, September 21, 2013 12:53 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 12:12 AM
Points: 1,953, Visits: 2,397
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/
Post #1497156
Posted Saturday, September 21, 2013 1:00 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, November 24, 2014 1:34 AM
Points: 111, Visits: 342
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
Post #1497157
Posted Monday, September 23, 2013 6:50 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, November 26, 2014 1:51 PM
Points: 148, Visits: 315
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
Post #1497361
Posted Thursday, March 6, 2014 10:26 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, April 3, 2014 4:30 PM
Points: 4, Visits: 6
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.
Post #1548385
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse