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
 Transact-SQL (2005)
 Multiple SELECT statements to build XML File

Author  Topic 

BravehearT1326
Starting Member

24 Posts

Posted - 2010-09-02 : 05:49:29
Hi there - wonder if anyone can assist with the following query I have.

I need to produce an XML file in a specific format and believe the best way to do this is to run multiple select statements to generate each of the segments within the file and join all the output together from each statement to produce the file I need.

My question is how do I do the joining of the output?

File needs to be in the format of :

<TEST xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CurrencyTotalCount>13</CurrencyTotalCount>
<CurrencyRateInsertCount>1</CurrencyRateInsertCount>
<CURRENCYRATEINSERTCOUNT>310638</CURRENCYRATEINSERTCOUNT>
<CurrencyRateHistoricInsertCount>12</CurrencyRateHistoricInsertCount>
<PersonTotalCount>0</PersonTotalCount>
<PersonInsertCount>0</PersonInsertCount>
<PersonUpdateCount>0</PersonUpdateCount>
<PolicyTotalCount>0</PolicyTotalCount>
<PolicyInsertCount>0</PolicyInsertCount>
<PolicyRefreshCount>0</PolicyRefreshCount>
<PolicyRenewalCount>0</PolicyRenewalCount>
<PolicyDeleteCount>0</PolicyDeleteCount>
<PolicyUpdateCount>0</PolicyUpdateCount>
<PolicyCancelCount>0</PolicyCancelCount>
<PolicyReinstateCount>0</PolicyReinstateCount>

<CurrencyRateHistoricInsert>
<BaseCurrency>GBP</BaseCurrency>
</CurrencyRateHistoricInsert>
..
..
.. Other sections go here
..
..
..
..
</TEST>

I have managed to get each of the sections individually but cant seem to put them together to make 1 big file.

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-09-02 : 06:14:02
Your requirement is not clear. Please elaborate.

Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-09-02 : 06:22:25
Have you heard of the FOR XML clause?

Can't you just use that?


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

BravehearT1326
Starting Member

24 Posts

Posted - 2010-09-02 : 07:23:16
The query I have built is as follows:



SET NOCOUNT ON
SET ANSI_NULLS ON

DROP TABLE #CurrencyList
DROP TABLE #Effective_date

-- added Thu
DECLARE @currencyrateinsertcount numeric(10)
set @currencyrateinsertcount = (
Select DISTINCT count (*) from Exchange_Rate_History
WHERE environment_key= 'UK')


CREATE TABLE #CurrencyList(CurrencyList_Date datetime NOT NULL)

SELECT DISTINCT
Effective_Date
INTO #Effective_Date
FROM dbo.Exchange_Rate_History;

--WITH XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema-instance' as xsi )
SELECT
CAST (
( SELECT

13 AS CurrencyTotalCount,
1 AS CurrencyRateInsertCount,
@currencyrateinsertcount AS CURRENCYRATEINSERTCOUNT, -- Example of variable for counters elememts.
12 AS CurrencyRateHistoricInsertCount,
0 AS PersonTotalCount,
0 AS PersonInsertCount,
0 AS PersonUpdateCount,
0 AS PolicyTotalCount,
0 AS PolicyInsertCount,
0 AS PolicyRefreshCount,
0 AS PolicyRenewalCount,
0 AS PolicyDeleteCount,
0 AS PolicyUpdateCount,
0 AS PolicyCancelCount,
0 AS PolicyReinstateCount

--FROM #Effective_Date
FOR XML PATH (''), ROOT ('counters')
) AS XML) ,
-- Adding in the CurrencyRateHistoricInsert Section
CAST(( SELECT DISTINCT
'GBP' as "CurrencyRateHistoricInsert/BaseCurrency"
FOR XML PATH('')) AS XML) ,



CAST(
( SELECT
CurrencyRatesInstance.Effective_date AS "CurrencyRatesInstance/DATE",
CurrencyList.CurrencyList_Date AS "CurrencyRatesInstance/CurrencyList",
CurrencyRate.Currency_Key AS "CurrencyRatesInstance/CurrencyList/CurrencyRate/Name",
CurrencyRate.Exchange_Rate AS "CurrencyRatesInstance/CurrencyList/CurrencyRate/Rate"
FROM #Effective_Date CurrencyRatesInstance
INNER JOIN Exchange_Rate_History_View CurrencyRate
ON CurrencyRatesInstance.effective_Date = CurrencyRate.effective_Date
LEFT OUTER JOIN #CurrencyList CurrencyList
ON CurrencyList.CurrencyList_Date = CurrencyRate.effective_Date
WHERE Environment_Key = 'DL'
and CurrencyRatesInstance.effective_Date > '20100808'
ORDER BY CurrencyRatesInstance.effective_Date DESC
FOR XML PATH ('')
) AS XML) [CurrencyRatesInstanceList] --CurrencyRatesInstanceList
FOR XML PATH (''), ELEMENTS


This is producing output as follows:
<counters>
<CurrencyTotalCount>13</CurrencyTotalCount>
<CurrencyRateInsertCount>1</CurrencyRateInsertCount>
<CURRENCYRATEINSERTCOUNT>310638</CURRENCYRATEINSERTCOUNT>
<CurrencyRateHistoricInsertCount>12</CurrencyRateHistoricInsertCount>
<PersonTotalCount>0</PersonTotalCount>
<PersonInsertCount>0</PersonInsertCount>
<PersonUpdateCount>0</PersonUpdateCount>
<PolicyTotalCount>0</PolicyTotalCount>
<PolicyInsertCount>0</PolicyInsertCount>
<PolicyRefreshCount>0</PolicyRefreshCount>
<PolicyRenewalCount>0</PolicyRenewalCount>
<PolicyDeleteCount>0</PolicyDeleteCount>
<PolicyUpdateCount>0</PolicyUpdateCount>
<PolicyCancelCount>0</PolicyCancelCount>
<PolicyReinstateCount>0</PolicyReinstateCount>
</counters>
<CurrencyRateHistoricInsert>
<BaseCurrency>GBP</BaseCurrency>
</CurrencyRateHistoricInsert>
<CurrencyRatesInstanceList>
<CurrencyRatesInstance>
<DATE>2010-09-01T00:00:00</DATE>
<CurrencyList>
<CurrencyRate>
<Name>AE1</Name>
<Rate>3.421600</Rate>
</CurrencyRate>
</CurrencyList>
</CurrencyRatesInstance>
<CurrencyRatesInstance>
<DATE>2010-09-01T00:00:00</DATE>
<CurrencyList>
<CurrencyRate>
<Name>AED</Name>
<Rate>3.530800</Rate>
</CurrencyRate>
</CurrencyList>
</CurrencyRatesInstance>

I am not sure that this is the best way to generate the XML output. The output needs to be in a particular format for a 3rd party piece of software and I hav very little experisnce of using XML - typical
Go to Top of Page
   

- Advertisement -