2

I have a stored procedure that runs fine using the execute command in SSMS, however putting the same command in a job gives the following error.

line 9, character 9, unexpected end of input

The code takes a very long XML string in UTF-8 encoding and puts it into a single nvarchar(max) cell. Then puts this string into a XML cell in a different table, allowing me to query the individual parts of the XML code using the nodes function. I cannot put the data directly into a nvarchar cell due to encoding differences.

I can't reproduce the string here as it is very very long.

I'm just looking for ideas really as to where it might be going wrong.

Here is what I know so far:

  1. The procedure runs without issue when executed manually

  2. I have checked permission issues, and that doesn't seem to be the problem. The agent runs under my own account and I am a sysadmin on the database

  3. I split the procedure into separate parts to locate exactly where the problem is occurring. Once again the separate procedures run fine when executed manually but an error occurs when run through SQL Server agent.

  4. When the query is run separately through SQL Server Agent it gives a slightly different error. This leads me to believe it is an encoding issue. However I am getting the XML from a webpage and I can't change the encoding on the webpage.

line 1, character 38, unable to switch the encoding

I know this is a long shot since you can't replicate the issue but if anyone could give an idea as to where to start looking for an answer, it would be greatly appreciated.

Edit1: Thank you so much for your help. I had to take a little time away from the problem to clear my head. Back at it this week.

Ok I think I've finally figured out the root of the problem. This is the code I am using to take the data from the website. It is truncating the data being pulled from the website. It runs perfectly when executed in SSMS. It truncated the data to 2048 characters when run through a batch file and truncates the data to 512 characters when run using a SS Job.

These values can not be random. There must be a setting somewhere but I can't find it. Any ideas?

Here's the code:

DECLARE 

@url nvarchar(max),
@win integer,
@hr integer ,
@Date date,
@SearchDate nvarchar(50)

Delete from XMLParsing.dbo.TextData

Set @Date = GETDATE()

set @SearchDate = CAST(@Date as nvarchar(50))

set @SearchDate = REPLACE(@SearchDate,'-','')

Select @url = 'http://semorep.sem-o.com/DataCollection/DataSets.asmx/queryDatasetXML?DatasetName=SET_CAL&User=primplims@gmail.com&Password=testsemo&FromDate=20130103&ToDate=20130111&P1=Energy&P2=Statements&P3=Initial&P4=&P5='

EXEC @hr=sp_OACreate 'WinHttp.WinHttpRequest.5.1',@win OUT 
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win 

EXEC @hr=sp_OAMethod @win, 'Open',NULL,'GET',@url,'false'
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win 

EXEC @hr=sp_OAMethod @win,'Send'
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win 

INSERT XMLParsing.dbo.TextData(SEMO_Data)
EXEC @hr=sp_OAGetProperty @win,'ResponseText'
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win

EXEC @hr=sp_OADestroy @win 
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win 
Jennifer Nolan
  • 123
  • 1
  • 8
  • 3
    Can you let us know what's at line 9 character 9? – Paul Sasik Apr 15 '13 at 15:46
  • As we can see you said you have permission's but does the connection string connect with a different user? and if so does it have permissions. – Necro Apr 15 '13 at 15:49
  • 2
    Are you putting the command in the job, or a stored procedure call in the job? – Aaron Bertrand Apr 15 '13 at 15:54
  • How long is the XML string? – J0e3gan Apr 15 '13 at 15:56
  • @PaulSasik I believe this is at `line 9 character 9 Set @Date = GETDATE()` – Jennifer Nolan Apr 15 '13 at 15:56
  • @Necro The connection string connects with the same user – Jennifer Nolan Apr 15 '13 at 15:57
  • @AaronBertrand The job is calling the stored procedure, but thats a good idea. I'll try running the command using a job – Jennifer Nolan Apr 15 '13 at 15:59
  • @J0e3gan the string is 238,619 characters. I looked it up already but Is this too long for the datatype? I might have got the wrong answer. The main reason I said it was very long is cause that many character is way too long for a forum. – Jennifer Nolan Apr 15 '13 at 16:01
  • @AaronBertrand I get the same error when I run the command in the job as when I put the stored procedure call in the job – Jennifer Nolan Apr 15 '13 at 16:23
  • I'm finished up here for the day and can't bring the SQL code home with me. But I'll be checking this post regularly this evening and give as much extra info as I can. Thanks for all your help so far. – Jennifer Nolan Apr 15 '13 at 16:28
  • At the various points in the string's lifecycle, can you put it in an XML parser to verify that it continues to be valid XML? – Melanie Apr 15 '13 at 18:27
  • @Melanie I've tried putting the XML into a Parser at various points and it's still valid. I'm almost certain at this stage it has to do with the UTF encoding of the XML. I just can't for the life of me figure out why it works manually but not as a job. – Jennifer Nolan Apr 16 '13 at 10:43
  • Hi, can you check which SET options you are using in each scenario. You can find this out by running SELECT @@OPTIONS in SSMS and through the agent job. You can find what options are set by using the code at http://www.mssqltips.com/sqlservertip/1415/determining-set-options-for-a-current-session-in-sql-server/ – Steve Ford Apr 16 '13 at 15:46
  • @SteveFord There is indeed a difference in the set options. SSMS has arithabort and quoted_identifier turned on but Agent does not. Would this affect the query do you think? – Jennifer Nolan Apr 17 '13 at 08:01
  • @JenniferNolan can you post the code with a much smaller XML string so we can get an idea of your code. Do you call your procedure in the Job? – Steve Ford Apr 17 '13 at 13:25

3 Answers3

6

Specify

SET TEXTSIZE 200000 

in the SQL Agent job step. The default TEXTSIZE for SQL Agent is 512.

SilverlightFox
  • 32,436
  • 11
  • 76
  • 145
sander
  • 61
  • 1
  • 2
1

Just an update. The reason this wasn't working correctly is that the XML being taken from the site was being truncated. I never fully figured out why this was happening but I used wget for windows instead of WinHttp.WinHttpRequest.5.1 which took all the data from the site and passed it directly to a webpage from which I could parse the data.

Jennifer Nolan
  • 123
  • 1
  • 8
0

Just put SET TEXTSIZE (with a great value)

and SQL Agent do the rest.