Job Truncates Data

I just ran into an interesting situation.

I created a Stored Procedure. It returns one JSON field with data over 500k characters. 1,054,073 to be exact. Works fine and does not truncate when I use EXEC. I have executed it over a thousand time and works every time.

When I setup a Job for this SP, it truncates the JSON to 512 characters.

At first, I thought that it is because the JSON method that I was calling which to some part that maybe the reason for the 512 characters. But when I zeroed in, I decided to return REPLICATE('TEST', 99999), it stills truncate to 8K.

Has anyone experienced this with a JOB? Again, SP ran fine.

Hi

I have never experienced this !!!

Somewhere Mapping is going on !! where the destination field is 512 K

hope this helps :slight_smile:

1 Like

REPLICATE itself will truncate to 8,000 chars for that call, since the value type is varchar.

Try this call instead:
REPLICATE(CAST('TEST' AS varchar(max)), 99999) --or 600000 as the number

1 Like

Harish, it can't be the mapping because when running the SP without the Job, it is working as expected. But I will go back and check again. Thanks

[quote="ScottPletcher, post:3, topic:17661"]
REPLICATE(CAST('TEST' AS varchar(max)), 99999)
[/quote] Scott, thanks. That did it for the variable test. Do you have any explanation why this is happening with calling the SP through a Job? Do I need to turn on a feature of some sort?

I am doing a call to a WebAPI. Usually it returns a full JSON but it is truncating the JSON causing an error. Do you have a solution for me on the following when calling through a JOB?

	DROP TABLE IF EXISTS #JSONTable 
	CREATE TABLE #JSONTable (JSONData NVARCHAR(MAX))

	--Place value into a table because returned data can be huge and variable will not be able to handle it.
	INSERT #JSONTable (JSONData)
	EXEC @RC = sp_OAMethod @API, 'responseText'

No, I'm not sure what's going on.

Could you have the job run the proc and see if that works? You could always add an optional parameter to the proc that "told" the proc it was being run thru a job if it needs to change how it executes for a job.

1 Like

What happens if you replace NVARCHAR(MAX) with json data type? Would also really help what is your end goal here. Sometimes knowing the bigger picture would help us give you maybe a better design approach also. Not just DML. Why are you for example using SQL sp_OAMethod instead of other means of getting the data such as powershell or some other approach? Just to understand your design/architecture approach

1 Like

I am not sure how to replace the NVARCHAR(MAX) with JSON data type. If you saying to literally replace it with the word "JSON", it compiles fine, but error when the SP is being called/ran.

It is not thatI am lazy. I don't usually describe the big picture because just exactly where do I start and when there is an orgy of (too much) information, i can lose the audience really quick.

However, I am trying to use the vendor's huge set of APIs to get data. That is the ultimate goal. Their APIs work fine when run in Postman. I can use C# but I choose not to because I wanted to take advantage of OPENJSON and all of SQL Server's built in support for JSON. The problem is not the JSON but the calling of the API using the sp_OAMethod. It has been working fantastic but where it coughs up, I think it is the service account that is running the job. As I have shared before, the SP executes perfectly by itself. As a matter of fact, I connected the SP to a SSRS page and put that on a schedule and that has been running perfectly fine too. It is just the SQL Job is causing the truncate of the JSON.

That brings me to the questions of you suggesting PowerShell and your above JSON datatype.

How would I replace and use the JSON datatype?
How would I use PowerSHell to call an API and then return the JSON to a SP?

Thanks

So I finally found the reason to this. I have to insert this line before the EXECUTE inside the Job:

SET TEXTSIZE 2147483647;

Crazy!

Found the answer here:

1 Like

:face_with_monocle: :flushed: :flushed: :astonished:

Very nice!

Calling web api via powershell

Invoke-WebRequest https://vendor.com/api/vm/

For example I am working on vmware rest api to get details of virtual machines from vmware using rest api they provide

https://vmware.github.io/vsphere-automation-sdk-rest/vsphere/operations/com/vmware/vcenter/vm.get-operation.html

GET https://{server}/rest/vcenter/vm/{vm}

Invoke-WebRequest https://{server}/rest/vcenter/vm/{vm}

2 Likes