Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have a 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 seperate parts to locate exactly where the problem is occuring. Once again the seperate procedures run fine when executed manually but an error occurs when run through SQL Server agent.

  4. When the query is run seperately 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.

EDIT 1: The code for obtaining the XML is as follows:

Select @url = 'http://....'

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 #TextData(HTML)
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 
share|improve this question

migrated from serverfault.com Apr 16 '13 at 1:31

This question came from our site for professional system and network administrators.

    
"The code takes a very long XML string in UTF-8 encoding and puts it into a single nvarchar(max) cell." -- please show us the code that does this. From the first error message, you may have an escaping issue. –  Jon Seigel Apr 16 '13 at 2:23
    
@JonSeigel I've put the code for retrieving the XML string above. –  Jennifer Nolan Apr 16 '13 at 10:53

2 Answers 2

Set up the SQL Profiler to show begin and end events for statements inside of a stored procedure. That should help you narrow the problem down to a single statement inside of a line. My shoot-from-the-hip thought is that there is something wrong with the quoting on the string.

share|improve this answer
    
Thanks @Darin, I've setup the Profiler and I'm having a look at it now. It's not giving a lot of information however. I've probably setup the Trace incorrectly (my first time using SQL Profiler). I'll keep trying though until I figure out how to at very least use Profiler correctly. –  Jennifer Nolan Apr 16 '13 at 10:53

line 1, character 38, unable to switch the encoding

I ran in to this a while back sql server wants the source string type to match the utf code.

    SELECT CAST('<?xml version = "1.0" encoding = "utf-8"?><nobody> </nobody>' AS XML)
    GO
    SELECT CAST('<?xml version = "1.0" encoding = "utf-16"?><nobody> </nobody>' AS XML)
    GO
    SELECT CAST(N'<?xml version = "1.0" encoding = "utf-8"?><nobody> </nobody>' AS XML)
    GO
    SELECT CAST(N'<?xml version = "1.0" encoding = "utf-16"?><nobody> </nobody>' AS XML)
    GO

You should see only the first and forth query have results. This is where the xml declared utf-"bits per character" matches the sql datatype.

line 9, character 9, unexpected end of input

I would run this problem from a different direction.

  1. have the procedure output the received xml
  2. check for permissions issues with the service accounts (difference between sql server and agent services)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.