SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Error When converting Nvarchar to XML


Error When converting Nvarchar to XML

Author
Message
Teee-SQL
Teee-SQL
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1120 Visits: 229
Hi all,

I'm trying to convert a string to XML, I'm not sure why I'm getting the error:XML parsing: line 1, character 14, equal expected
When I run the stored proc below to select the XML data. Please help

Kind Regards
Teee

CREATE PROCEDURE Test

AS
BEGIN

DECLARE @sql Nvarchar(max)

SET @sql = '<Data Extract>'
SET @sql = @sql + (SELECT Top 2 FirstName, LastName
FROM
[dbo].[CandPersonal] pe
INNER JOIN CandProfile pr on pe.CandId = pr.Id
WHERE pr.isGraduateApplicant = '1'
FOR XML PATH('Candidate'), ROOT('Graduates'))
SET @sql = @sql + (SELECT Top 2 FirstName, LastName
FROM
[dbo].[CandPersonal] pe
INNER JOIN CandProfile pr on pe.CandId = pr.Id
WHERE pr.isGraduateApplicant = '0'
FOR XML PATH('Candidate'), ROOT('ExperiencedHires'))
SET @sql = @sql + '/<Data Extract>'



SELECT CAST(@sql as XML) --XML parsing: line 1, character 14, equal expected
END
anthony.green
anthony.green
SSC-Forever
SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)

Group: General Forum Members
Points: 46911 Visits: 7624
What is the output of @SQL before you convert it to XML?


DECLARE @sql Nvarchar(max)

SET @sql = '<Data Extract>'
SET @sql = @sql + (SELECT Top 2 FirstName, LastName
FROM
[dbo].[CandPersonal] pe
INNER JOIN CandProfile pr on pe.CandId = pr.Id
WHERE pr.isGraduateApplicant = '1'
FOR XML PATH('Candidate'), ROOT('Graduates'))
SET @sql = @sql + (SELECT Top 2 FirstName, LastName
FROM
[dbo].[CandPersonal] pe
INNER JOIN CandProfile pr on pe.CandId = pr.Id
WHERE pr.isGraduateApplicant = '0'
FOR XML PATH('Candidate'), ROOT('ExperiencedHires'))
SET @sql = @sql + '/<Data Extract>'

SELECT @SQL





How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Teee-SQL
Teee-SQL
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1120 Visits: 229
here's the output:
<Data Extract><Graduates><Candidate><FirstName>Shaun</FirstName><LastName>Slabber</LastName></Candidate><Candidate><FirstName>paul</FirstName><LastName>devine</LastName></Candidate></Graduates><ExperiencedHires><Candidate><FirstName>paul</FirstName><LastName>devine</LastName></Candidate><Candidate><FirstName>Meera</FirstName><LastName>Bhana</LastName></Candidate></ExperiencedHires>/<Data Extract>
Mark Cowne
Mark Cowne
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11286 Visits: 26292
Your end tag is wrong

'/<Data Extract>'

should be

'</Data Extract>'

____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




anthony.green
anthony.green
SSC-Forever
SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)SSC-Forever (46K reputation)

Group: General Forum Members
Points: 46911 Visits: 7624
Also you cannot have spaces in element names, <Data Extract> needs to be <DataExtract>


DECLARE @SQL NVARCHAR(MAX) = '<DataExtract><Graduates><Candidate><FirstName>Shaun</FirstName><LastName>Slabber</LastName></Candidate><Candidate><FirstName>paul</FirstName><LastName>devine</LastName></Candidate></Graduates><ExperiencedHires><Candidate><FirstName>paul</FirstName><LastName>devine</LastName></Candidate><Candidate><FirstName>Meera</FirstName><LastName>Bhana</LastName></Candidate></ExperiencedHires></DataExtract>'
select @SQL
select CAST(@sql as xml)





How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Teee-SQL
Teee-SQL
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1120 Visits: 229
Thanks so much, I removed the spaces and corrected the syntax. It works perfectly.
:-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search