Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 SSIS and Import/Export (2005)
 SQL convert to XML - Error Message

Author  Topic 

Neal44
Starting Member

17 Posts

Posted - 2011-03-07 : 04:29:50
Hi,

When I run the following T-SQL code against my Database

T-SQL
WITH Unpivotted (ZIPCODEFROM ,ZIPCODETO,Value,Name) AS (
SELECT ZIPCODEFROM ,NULL,'start','ID'
FROM dbo.Mileage
WHERE ZIPCODEFROM IS NOT NULL

UNION ALL

SELECT ZIPCODEFROM ,NULL,'Start','NAME'
FROM dbo.Mileage
WHERE ZIPCODEFROM IS NOT NULL

UNION ALL

SELECT ZIPCODEFROM ,NULL,'1','RIGIDACC'
FROM dbo.Mileage
WHERE ZIPCODEFROM IS NOT NULL

UNION ALL

SELECT NULL ,ZIPCODETO,'end','ID'
FROM dbo.Mileage
WHERE ZIPCODETO IS NOT NULL

UNION ALL

SELECT NULL ,ZIPCODETO,'End','NAME'
FROM dbo.Mileage
WHERE ZIPCODETO IS NOT NULL

UNION ALL

SELECT ZIPCODEFROM ,NULL,ZIPCODEFROM,'POSTCODE'
FROM dbo.Mileage
WHERE ZIPCODEFROM IS NOT NULL

UNION ALL

SELECT NULL ,ZIPCODETO,ZIPCODETO,'POSTCODE'
FROM dbo.Mileage
WHERE ZIPCODETO IS NOT NULL

)
SELECT 'DEPOT' AS "@ObjectType",
(SELECT a.Name AS "@KeywordName",
a.Value AS "text()"
FROM Unpivotted a
WHERE a.ZIPCODEFROM=b.ZIPCODEFROM
FOR XML PATH('Value'), ROOT('Object'), TYPE)
FROM dbo.Mileage b
WHERE b.ZIPCODEFROM<>''

UNION ALL

SELECT 'CUST' AS "@ObjectType",
(SELECT a.Name AS "@KeywordName",
a.Value AS "text()"
FROM Unpivotted a
WHERE a.ZIPCODETO=b.ZIPCODETO
FOR XML PATH('Value'), ROOT('Object'), TYPE)
FROM dbo.Mileage b
WHERE b.ZIPCODETO<>''
FOR XML PATH('ObjectSequence'), ROOT('SupplierData'), TYPE;



it produces xml similar to the example shown below:

XML
<SupplierData>
<ObjectSequence ObjectType="DEPOT">
<Object>
<Value KeywordName="ID">start</Value>
<Value KeywordName="NAME">Start</Value>
<Value KeywordName="RIGIDACC">1</Value>
<Value KeywordName="POSTCODE">XX37 9</Value>
</Object>
</ObjectSequence>
<ObjectSequence ObjectType="CUST">
<Object>
<Value KeywordName="ID">end</Value>
<Value KeywordName="NAME">End</Value>
<Value KeywordName="POSTCODE">ZZ12 4</Value>
</Object>
</ObjectSequence>
</SupplierData>


However, when I run this code as 'Direct input' in an 'Execute SQL Task' I get an error:
'XML document must have a top level element. Error processing resource.'


The Source Output is '<?MSSQLError HResult="0x80004005" Source="Microsoft XML Extensions to SQL Server" Description="No description provided"?>'

Any ideas please?

Thanks in advance,




Neal

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-03-07 : 05:21:25
Are you using "SET NOCOUNT ON;" in your code?



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

Neal44
Starting Member

17 Posts

Posted - 2011-03-07 : 06:03:10
Thanks for the tip. I wasn't using the 'SET NOCOUNT ON' - But even when including this, it is still producing the same error message.

Neal
Go to Top of Page

Neal44
Starting Member

17 Posts

Posted - 2011-03-07 : 07:00:18
Once the code is executed, the result appears to be blank ''. I pass the result to a string variable. When I debug the variable, the value is:
{<ROOT></ROOT>\r\n}

(I use code within a script task to strip the 'ROOT' elements from the string later in the package).



Neal
Go to Top of Page
   

- Advertisement -