Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

Wednesday, March 28, 2012

How to schedule an export of data to xml

Hi gurus,

I need to be able to export a table to an xml file on the 1st of every month. How can I accomplish this using SQL.

Thanks!

SQL is structured query language, it has nothing to do with scheduling.

If your using SQL Server 2000 or later, you can schedule a "job" to run at intervals, using a utility called DTS. It also has the ability to create xml files from your database tables.

how to save xml document in database table?

I have a db in Sql Server Express 2005. Into this db I have a table with an XML field and I want to save an XML document into this field through SQL.
Any suggestion to do this operation?
Thank you

Mirko

There is a nice article about XML fields and SQL server.

http://www.developer.com/db/article.php/3565996

However there is a problem on that article I'll paste the insert statements here:

INSERT INTO Document (Description, DocumentStore)
VALUES('Bruce''s poem',
N'<?xml version="1.0" ?>
<Document Name="Poem">
<Author>Bruce</Author>
<Text>The cat/is flat.</Text>
</Document>')

INSERT INTO Document (Description, DocumentStore)
VALUES('Code of Hammurabi',
N'<?xml version="1.0" ?>
<Document Name="Code">
<Author>Hammurabi</Author>
<Text>An eye for an eye, a tooth for a tooth.</Text>
</Document>')

INSERT INTO Document (Description, DocumentStore)
VALUES('Nursery Rhyme',
N'<?xml version="1.0" ?>
<Document Name="Jack and Jill">
<Author>Mother Hubbard</Author>
<Text>Jack and Jill/went up the hill.</Text>
</Document>')

|||

Thank you!

Mirko

how to save xml document in database table?

I have a db in Sql Server Express 2005. Into this db I have a table with an XML field and I want to save an XML document into this field through SQL.
Any suggestion to do this operation?
Thank you

Mirko

There is a nice article about XML fields and SQL server.

http://www.developer.com/db/article.php/3565996

However there is a problem on that article I'll paste the insert statements here:

INSERT INTO Document (Description, DocumentStore)
VALUES('Bruce''s poem',
N'<?xml version="1.0" ?>
<Document Name="Poem">
<Author>Bruce</Author>
<Text>The cat/is flat.</Text>
</Document>')

INSERT INTO Document (Description, DocumentStore)
VALUES('Code of Hammurabi',
N'<?xml version="1.0" ?>
<Document Name="Code">
<Author>Hammurabi</Author>
<Text>An eye for an eye, a tooth for a tooth.</Text>
</Document>')

INSERT INTO Document (Description, DocumentStore)
VALUES('Nursery Rhyme',
N'<?xml version="1.0" ?>
<Document Name="Jack and Jill">
<Author>Mother Hubbard</Author>
<Text>Jack and Jill/went up the hill.</Text>
</Document>')

|||

Thank you!

Mirko

Monday, March 26, 2012

How to save for xml statement into a file

Hi

I have a T-SQL like :

Code Snippet

select * from customers for xml PATH , ROOT

and how to save the result to a xml file .

Refer to Books Online about using BCP.

Look in Books Online, Topic: 'BCP Utility'

How to save FOR XML results into TEXT column?

Help! It appears that the FOR XML clause doesn't 100% get done what I
need to accommplish.
Problem: With Orders table (no Order Details) as:
OrderID int
OrderData ntext
OrderDate smalldatetime
OrderProcessed bit
I have another query that does a select on an Order Details and
Customer table which wnds with FOR XML.
I need to get the results of that query stored in the OrderData column
from above in XML format.
How do I accomplish ths using SQL server tools?
"CD" <doober@.family.us> wrote in message
news:s535f0pk4c6sro2puv3h783t7femfj1gms@.4ax.com...
[snip]
> I need to get the results of that query stored in the OrderData column
> from above in XML format.
> How do I accomplish ths using SQL server tools?
You can't without a lot of effort. If you're storing the XML for caching
purposes you're better off using something like the caching in ASP.Net.
Bryant
|||
>You can't without a lot of effort. If you're storing the
XML for caching
>purposes you're better off using something like the
caching in ASP.Net.
>
No, I need to use it to post orders to another downsteam
system that requires the order data in XML in a single
field, one record for each order.
|||Wait for Yukon.
Read How XML is treated in Yukon
[vbcol=seagreen]
>--Original Message--
the
>XML for caching
>caching in ASP.Net.
>No, I need to use it to post orders to another downsteam
>system that requires the order data in XML in a single
>field, one record for each order.
>.
>
|||To add some more information: In SQL Server 2005, you can nest FOR XML
expressions and thus could write an expression that provides XML data in XML
in a field, one per row.
Best regards
Michael
"Nitin" <anonymous@.discussions.microsoft.com> wrote in message
news:2b75901c46894$9b9861c0$a501280a@.phx.gbl...[vbcol=seagreen]
> Wait for Yukon.
> Read How XML is treated in Yukon
>
> the

Monday, March 19, 2012

How to return xml datatype column values ?

Hi ,

I want to return the xml datatype values in the output of my query.

I am querrying on one table which has xml datatype by selelcting the column in select clause and would like to get the values in output.

How do i get output with out/with including the column name in group by clause?

Can some one help me here.

--Smita.

You have a XML column and you want to break the XML as columns ? is this what you want ?|||

Rick,

I have a coulmn with xml datatype and want to get entire column value in one output.

-Smitha.

|||

Something like this ?

declare @.x xml

set @.x='<Root>

<Node>

<Node name="S.No">1</Node>

<Node name="S.Level">1</Node>

<Node name="S.FName">Data1</Node>

<Node name="S.LName">Data2</Node>

<Node name="Sl #" />

</Node>

<Node>

<Node name="S.No">2</Node>

<Node name="S.Level">2</Node>

<Node name="S.FName">Data22</Node>

<Node name="S.LName">Data33</Node>

<Node name="Sl #" />

</Node>

</Root>'

select cast(r.query('for $data in ./Node return data($data) ') as varchar(max)) as [Column]

from @.x.nodes('/Root/Node') as X(r)

How to return the result (XML) with FOR XML EXPLICIT

Dear SQLXML specialists and other animals,
I got the following SP, that runs a SQL statement with FOR XML
EXPLICIT:
**** SQL STATEMENT *****
CREATE PROCEDURE SP_F06_GetActions
(
@.MSISDN as varchar(20),
@.SessionId as varchar(64)
)
AS
BEGIN
SELECT
1 as tag
,NULL as parent
,'F06' as [RESPONSE!1!Flow_Name!Element]
,[MSISDN] as [RESPONSE!1!MSISDN!Element]
,getdate() as [RESPONSE!1!Timestamp!Element]
,@.SessionId as [RESPONSE!1!SessionId!Element]
,0 as [RESPONSE!1!Status!Element]
,NULL as [Actions!2!NumOfActions!Element]
,NULL as [Action!3!Id]
,NULL as [Action!3!ActionId!Element]
,NULL as [Action!3!ActionName!Element]
,NULL as [Action!3!ActionType!Element]
,NULL as [Action!3!From!Element]
,NULL as [Action!3!To!Element]
,NULL as [Action!3!ActionStart!Element]
,NULL as [Action!3!ActionStop!Element]
,NULL as [Action!3!ActionDurationThreshold!Elemen
t]
,NULL as [Action!3!ActionVideoURL!Element]
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
UNION
SELECT
2
,1
,NULL
,NULL
,NULL
,NULL
,NULL
, dbo.getNumOfActionsByMSISDN(MSISDN)
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
UNION
SELECT
3
,2
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,[ActionId]
,[ActionId]
,[ActionName]
,[ActionType]
,[From]
,[To]
,[ActionStart]
,[ActionStop]
,[ActionDurationThreshold]
,[ActionVideoURL]
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
WHERE [MSISDN]=@.MSISDN
--ORDER BY 3, 1
FOR XML EXPLICIT
RETURN
END
GO
**** END OF SQL ***********
Well, it returns the following XML:
***** XML *********************
<RESPONSE>
<Flow_Name>F06</Flow_Name>
<MSISDN>9053*********</MSISDN>
<Timestamp>2007-11-15T14:37:24.467</Timestamp>
<SessionId>45717B60-0A0F-434B-9FB9-CC8F647BFEA7</SessionId>
<Status>0</Status>
<Actions>
<NumOfActions>3</NumOfActions>
<Action Id="10001">
<ActionId>10001</ActionId>
<ActionName>Acme</ActionName>
<ActionType>1</ActionType>
<From>00:00:00</From>
<To>12:00:00</To>
<ActionStart>2007-11-15T09:52:56.513</ActionStart>
<ActionStop>2007-11-25T09:52:56.513</ActionStop>
<ActionDurationThreshold>8</ActionDurationThreshold>
<ActionVideoURL>http://www.dynu.com/media/video/video1.g3p</
ActionVideoURL>
</Action>
<Action Id="10002">
<ActionId>10002</ActionId>
<ActionName>Pepsi</ActionName>
<ActionType>1</ActionType>
<From>12:00</From>
<To>16:00:00</To>
<ActionStart>2007-11-15T09:53:17.640</ActionStart>
<ActionStop>2007-11-25T09:53:17.640</ActionStop>
<ActionDurationThreshold>7</ActionDurationThreshold>
<ActionVideoURL>http://www.dynu.com/media/video/video2.g3p</
ActionVideoURL>
</Action>
<Action Id="10003">
<ActionId>10003</ActionId>
<ActionName>Momo Deterjan</ActionName>
<ActionType>1</ActionType>
<From>18:00:00</From>
<To>22:00:00</To>
<ActionStart>2007-11-15T09:56:32.950</ActionStart>
<ActionStop>2007-11-25T09:56:32.950</ActionStop>
<ActionDurationThreshold>6</ActionDurationThreshold>
<ActionVideoURL>http://www.dynu.com/media/video/video2.g3p</
ActionVideoURL>
</Action>
</Actions>
</RESPONSE>
**** END OF XML ************
Therefore, what I want is to assign this XML to a variable. To do
that, I tried the following (but failed):
Exec @.RC=SP_F06_GetActions
@.MSISDN,
@.SessionId
So, my QUESTION is HOW CAN I RETRIEVE THIS XML INTO A VARIABLE?
Thanks in advance for your help and interest.
Regards,
Ali<ali.koyuncu@.gmail.com> wrote in message
news:ea5805dd-f725-49c8-a641-68f2ea767a14@.f13g2000hsa.googlegroups.com...
> Dear SQLXML specialists and other animals,
> I got the following SP, that runs a SQL statement with FOR XML
> EXPLICIT:
> **** SQL STATEMENT *****
> CREATE PROCEDURE SP_F06_GetActions
> (
> @.MSISDN as varchar(20),
> @.SessionId as varchar(64)
> )
> AS
> BEGIN
> SELECT
> 1 as tag
> ,NULL as parent
> ,'F06' as [RESPONSE!1!Flow_Name!Element]
> ,[MSISDN] as [RESPONSE!1!MSISDN!Element]
> ,getdate() as [RESPONSE!1!Timestamp!Element]
> ,@.SessionId as [RESPONSE!1!SessionId!Element]
> ,0 as [RESPONSE!1!Status!Element]
> ,NULL as [Actions!2!NumOfActions!Element]
> ,NULL as [Action!3!Id]
> ,NULL as [Action!3!ActionId!Element]
> ,NULL as [Action!3!ActionName!Element]
> ,NULL as [Action!3!ActionType!Element]
> ,NULL as [Action!3!From!Element]
> ,NULL as [Action!3!To!Element]
> ,NULL as [Action!3!ActionStart!Element]
> ,NULL as [Action!3!ActionStop!Element]
> ,NULL as [Action!3!ActionDurationThreshold!Elemen
t]
> ,NULL as [Action!3!ActionVideoURL!Element]
> FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
> UNION
> SELECT
> 2
> ,1
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> , dbo.getNumOfActionsByMSISDN(MSISDN)
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
> UNION
> SELECT
> 3
> ,2
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,[ActionId]
> ,[ActionId]
> ,[ActionName]
> ,[ActionType]
> ,[From]
> ,[To]
> ,[ActionStart]
> ,[ActionStop]
> ,[ActionDurationThreshold]
> ,[ActionVideoURL]
> FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
> WHERE [MSISDN]=@.MSISDN
> --ORDER BY 3, 1
> FOR XML EXPLICIT
> RETURN
> END
> GO
> **** END OF SQL ***********
> Well, it returns the following XML:
> ***** XML *********************
> <RESPONSE>
> <Flow_Name>F06</Flow_Name>
> <MSISDN>9053*********</MSISDN>
> <Timestamp>2007-11-15T14:37:24.467</Timestamp>
> <SessionId>45717B60-0A0F-434B-9FB9-CC8F647BFEA7</SessionId>
> <Status>0</Status>
> <Actions>
> <NumOfActions>3</NumOfActions>
> <Action Id="10001">
> <ActionId>10001</ActionId>
> <ActionName>Acme</ActionName>
> <ActionType>1</ActionType>
> <From>00:00:00</From>
> <To>12:00:00</To>
> <ActionStart>2007-11-15T09:52:56.513</ActionStart>
> <ActionStop>2007-11-25T09:52:56.513</ActionStop>
> <ActionDurationThreshold>8</ActionDurationThreshold>
> <ActionVideoURL>http://www.dynu.com/media/video/video1.g3p</
> ActionVideoURL>
> </Action>
> <Action Id="10002">
> <ActionId>10002</ActionId>
> <ActionName>Pepsi</ActionName>
> <ActionType>1</ActionType>
> <From>12:00</From>
> <To>16:00:00</To>
> <ActionStart>2007-11-15T09:53:17.640</ActionStart>
> <ActionStop>2007-11-25T09:53:17.640</ActionStop>
> <ActionDurationThreshold>7</ActionDurationThreshold>
> <ActionVideoURL>http://www.dynu.com/media/video/video2.g3p</
> ActionVideoURL>
> </Action>
> <Action Id="10003">
> <ActionId>10003</ActionId>
> <ActionName>Momo Deterjan</ActionName>
> <ActionType>1</ActionType>
> <From>18:00:00</From>
> <To>22:00:00</To>
> <ActionStart>2007-11-15T09:56:32.950</ActionStart>
> <ActionStop>2007-11-25T09:56:32.950</ActionStop>
> <ActionDurationThreshold>6</ActionDurationThreshold>
> <ActionVideoURL>http://www.dynu.com/media/video/video2.g3p</
> ActionVideoURL>
> </Action>
> </Actions>
> </RESPONSE>
> **** END OF XML ************
> Therefore, what I want is to assign this XML to a variable. To do
> that, I tried the following (but failed):
> Exec @.RC=SP_F06_GetActions
> @.MSISDN,
> @.SessionId
> So, my QUESTION is HOW CAN I RETRIEVE THIS XML INTO A VARIABLE?
> Thanks in advance for your help and interest.
> Regards,
> Ali
Which version of SQL Server? In 2000 you can't do what you want, in 2005 you
need to use the TYPE directive.
http://technet.microsoft.com/en-us/...y/ms345110.aspx
Joe Fawcett (MVP - XML)
http://joe.fawcett.name|||I use SQL Server 2005.
How to use TYPE? Can you give me simple example?
Thanks,
Ali
On 15 Kas=FDm, 15:56, "Joe Fawcett" <joefawc...@.newsgroup.nospam> wrote:
> <ali.koyu...@.gmail.com> wrote in message
> news:ea5805dd-f725-49c8-a641-68f2ea767a14@.f13g2000hsa.googlegroups.com...
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Which version of SQL Server? In 2000 you can't do what you want, in 2005 y=[/color
]
ou
> need to use the TYPE directive.http://technet.microsoft.com/en-us/library/=[/color
]
ms345110.aspx
> --
> Joe Fawcett (MVP - XML)
> http://joe.fawcett.name|||I use SQL Server 2005.
How to use TYPE? Can you give me simple example?
Thanks,
Ali
On 15 Kas=FDm, 15:56, "Joe Fawcett" <joefawc...@.newsgroup.nospam> wrote:
> <ali.koyu...@.gmail.com> wrote in message
> news:ea5805dd-f725-49c8-a641-68f2ea767a14@.f13g2000hsa.googlegroups.com...
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Which version of SQL Server? In 2000 you can't do what you want, in 2005 y=[/color
]
ou
> need to use the TYPE directive.http://technet.microsoft.com/en-us/library/=[/color
]
ms345110.aspx
> --
> Joe Fawcett (MVP - XML)
> http://joe.fawcett.name|||I use SQL Server 2005.
How to use TYPE? Can you give me simple example?
Thanks,
Ali
On 15 Kas=FDm, 15:56, "Joe Fawcett" <joefawc...@.newsgroup.nospam> wrote:
> <ali.koyu...@.gmail.com> wrote in message
> news:ea5805dd-f725-49c8-a641-68f2ea767a14@.f13g2000hsa.googlegroups.com...
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Which version of SQL Server? In 2000 you can't do what you want, in 2005 y=[/color
]
ou
> need to use the TYPE directive.http://technet.microsoft.com/en-us/library/=[/color
]
ms345110.aspx
> --
> Joe Fawcett (MVP - XML)
> http://joe.fawcett.name|||When I use TYPE, it gave me the following error :( *damn*
Msg 1086, Level 15, State 1, Line 72
The FOR XML clause is invalid in views, inline functions, derived
tables, and subqueries when they contain a set operator. To work
around, wrap the SELECT containing a set operator using derived table
syntax and apply FOR XML on top of it.
On 15 Kas=C4=B1m, 16:08, ali.koyu...@.gmail.com wrote:
> I use SQL Server 2005.
> How to use TYPE? Can you give me simple example?
> Thanks,
> Ali
> On 15 Kas=C3=BDm, 15:56, "Joe Fawcett" <joefawc...@.newsgroup.nospam> wrote=[/color
]
:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
you
y/ms345110.aspx
>
>
>|||At first, many thans to Joe...
then YAY! I solved. Just changed my SP as follows:
ALTER PROCEDURE SP_F06_GetActions
(
@.MSISDN as varchar(20),
@.SessionId as varchar(64),
@.ResultXML as xml=3DNULL output --NEW!
)
AS
BEGIN
SELECT
1 as tag
,NULL as parent
,'F06' as [RESPONSE!1!Flow_Name!Element]
,[MSISDN] as [RESPONSE!1!MSISDN!Element]
,getdate() as [RESPONSE!1!Timestamp!Element]
,@.SessionId as [RESPONSE!1!SessionId!Element]
,0 as [RESPONSE!1!Status!Element]
,NULL as [Actions!2!NumOfActions!Element]
,NULL as [Action!3!Id]
,NULL as [Action!3!ActionId!Element]
,NULL as [Action!3!ActionName!Element]
,NULL as [Action!3!ActionType!Element]
,NULL as [Action!3!From!Element]
,NULL as [Action!3!To!Element]
,NULL as [Action!3!ActionStart!Element]
,NULL as [Action!3!ActionStop!Element]
,NULL as [Action!3!ActionDurationThreshold!Elemen
t]
,NULL as [Action!3!ActionVideoURL!Element]
INTO #TempTable -- DEFINING A TEMP TABLE!
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
UNION
SELECT
2
,1
,NULL
,NULL
,NULL
,NULL
,NULL
, dbo.getNumOfActionsByMSISDN(MSISDN)
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
UNION
SELECT
3
,2
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,[ActionId]
,[ActionId]
,[ActionName]
,[ActionType]
,[From]
,[To]
,[ActionStart]
,[ActionStop]
,[ActionDurationThreshold]
,[ActionVideoURL]
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
WHERE [MSISDN]=3D@.MSISDN
SET @.ResultXML=3D(SELECT * FROM #TempTable FOR XML EXPLICIT, TYPE)
DROP TABLE #TempTable
RETURN
END
GO
On 15 Kas=C4=B1m, 16:08, ali.koyu...@.gmail.com wrote:
> I use SQL Server 2005.
> How to use TYPE? Can you give me simple example?
> Thanks,
> Ali
> On 15 Kas=C3=BDm, 15:56, "Joe Fawcett" <joefawc...@.newsgroup.nospam> wrote=[/color
]
:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
you
y/ms345110.aspx
>
>
>|||At first, many thans to Joe...
then YAY! I solved. Just changed my SP as follows:
ALTER PROCEDURE SP_F06_GetActions
(
@.MSISDN as varchar(20),
@.SessionId as varchar(64),
@.ResultXML as xml=3DNULL output --NEW!
)
AS
BEGIN
SELECT
1 as tag
,NULL as parent
,'F06' as [RESPONSE!1!Flow_Name!Element]
,[MSISDN] as [RESPONSE!1!MSISDN!Element]
,getdate() as [RESPONSE!1!Timestamp!Element]
,@.SessionId as [RESPONSE!1!SessionId!Element]
,0 as [RESPONSE!1!Status!Element]
,NULL as [Actions!2!NumOfActions!Element]
,NULL as [Action!3!Id]
,NULL as [Action!3!ActionId!Element]
,NULL as [Action!3!ActionName!Element]
,NULL as [Action!3!ActionType!Element]
,NULL as [Action!3!From!Element]
,NULL as [Action!3!To!Element]
,NULL as [Action!3!ActionStart!Element]
,NULL as [Action!3!ActionStop!Element]
,NULL as [Action!3!ActionDurationThreshold!Elemen
t]
,NULL as [Action!3!ActionVideoURL!Element]
INTO #TempTable -- DEFINING A TEMP TABLE!
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
UNION
SELECT
2
,1
,NULL
,NULL
,NULL
,NULL
,NULL
, dbo.getNumOfActionsByMSISDN(MSISDN)
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
UNION
SELECT
3
,2
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,[ActionId]
,[ActionId]
,[ActionName]
,[ActionType]
,[From]
,[To]
,[ActionStart]
,[ActionStop]
,[ActionDurationThreshold]
,[ActionVideoURL]
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
WHERE [MSISDN]=3D@.MSISDN
SET @.ResultXML=3D(SELECT * FROM #TempTable FOR XML EXPLICIT, TYPE)
DROP TABLE #TempTable
RETURN
END
GO
On 15 Kas=C4=B1m, 16:08, ali.koyu...@.gmail.com wrote:
> I use SQL Server 2005.
> How to use TYPE? Can you give me simple example?
> Thanks,
> Ali
> On 15 Kas=C3=BDm, 15:56, "Joe Fawcett" <joefawc...@.newsgroup.nospam> wrote=[/color
]
:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
you
y/ms345110.aspx
>
>
>|||At first, many thans to Joe...
then YAY! I solved. Just changed my SP as follows:
ALTER PROCEDURE SP_F06_GetActions
(
@.MSISDN as varchar(20),
@.SessionId as varchar(64),
@.ResultXML as xml=3DNULL output --NEW!
)
AS
BEGIN
SELECT
1 as tag
,NULL as parent
,'F06' as [RESPONSE!1!Flow_Name!Element]
,[MSISDN] as [RESPONSE!1!MSISDN!Element]
,getdate() as [RESPONSE!1!Timestamp!Element]
,@.SessionId as [RESPONSE!1!SessionId!Element]
,0 as [RESPONSE!1!Status!Element]
,NULL as [Actions!2!NumOfActions!Element]
,NULL as [Action!3!Id]
,NULL as [Action!3!ActionId!Element]
,NULL as [Action!3!ActionName!Element]
,NULL as [Action!3!ActionType!Element]
,NULL as [Action!3!From!Element]
,NULL as [Action!3!To!Element]
,NULL as [Action!3!ActionStart!Element]
,NULL as [Action!3!ActionStop!Element]
,NULL as [Action!3!ActionDurationThreshold!Elemen
t]
,NULL as [Action!3!ActionVideoURL!Element]
INTO #TempTable -- DEFINING A TEMP TABLE!
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
UNION
SELECT
2
,1
,NULL
,NULL
,NULL
,NULL
,NULL
, dbo.getNumOfActionsByMSISDN(MSISDN)
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
UNION
SELECT
3
,2
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,[ActionId]
,[ActionId]
,[ActionName]
,[ActionType]
,[From]
,[To]
,[ActionStart]
,[ActionStop]
,[ActionDurationThreshold]
,[ActionVideoURL]
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
WHERE [MSISDN]=3D@.MSISDN
SET @.ResultXML=3D(SELECT * FROM #TempTable FOR XML EXPLICIT, TYPE)
DROP TABLE #TempTable
RETURN
END
GO
On 15 Kas=C4=B1m, 16:08, ali.koyu...@.gmail.com wrote:
> I use SQL Server 2005.
> How to use TYPE? Can you give me simple example?
> Thanks,
> Ali
> On 15 Kas=C3=BDm, 15:56, "Joe Fawcett" <joefawc...@.newsgroup.nospam> wrote=[/color
]
:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
you
y/ms345110.aspx
>
>
>|||If you're using SQL 2005, how about using FOR XML PATH instead? Much
simpler.
<ali.koyuncu@.gmail.com> wrote in message
news:ea5805dd-f725-49c8-a641-68f2ea767a14@.f13g2000hsa.googlegroups.com...
> Dear SQLXML specialists and other animals,
> I got the following SP, that runs a SQL statement with FOR XML
> EXPLICIT:
> **** SQL STATEMENT *****
> CREATE PROCEDURE SP_F06_GetActions
> (
> @.MSISDN as varchar(20),
> @.SessionId as varchar(64)
> )
> AS
> BEGIN
> SELECT
> 1 as tag
> ,NULL as parent
> ,'F06' as [RESPONSE!1!Flow_Name!Element]
> ,[MSISDN] as [RESPONSE!1!MSISDN!Element]
> ,getdate() as [RESPONSE!1!Timestamp!Element]
> ,@.SessionId as [RESPONSE!1!SessionId!Element]
> ,0 as [RESPONSE!1!Status!Element]
> ,NULL as [Actions!2!NumOfActions!Element]
> ,NULL as [Action!3!Id]
> ,NULL as [Action!3!ActionId!Element]
> ,NULL as [Action!3!ActionName!Element]
> ,NULL as [Action!3!ActionType!Element]
> ,NULL as [Action!3!From!Element]
> ,NULL as [Action!3!To!Element]
> ,NULL as [Action!3!ActionStart!Element]
> ,NULL as [Action!3!ActionStop!Element]
> ,NULL as [Action!3!ActionDurationThreshold!Elemen
t]
> ,NULL as [Action!3!ActionVideoURL!Element]
> FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
> UNION
> SELECT
> 2
> ,1
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> , dbo.getNumOfActionsByMSISDN(MSISDN)
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
> UNION
> SELECT
> 3
> ,2
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,[ActionId]
> ,[ActionId]
> ,[ActionName]
> ,[ActionType]
> ,[From]
> ,[To]
> ,[ActionStart]
> ,[ActionStop]
> ,[ActionDurationThreshold]
> ,[ActionVideoURL]
> FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
> WHERE [MSISDN]=@.MSISDN
> --ORDER BY 3, 1
> FOR XML EXPLICIT
> RETURN
> END
> GO
> **** END OF SQL ***********
> Well, it returns the following XML:
> ***** XML *********************
> <RESPONSE>
> <Flow_Name>F06</Flow_Name>
> <MSISDN>9053*********</MSISDN>
> <Timestamp>2007-11-15T14:37:24.467</Timestamp>
> <SessionId>45717B60-0A0F-434B-9FB9-CC8F647BFEA7</SessionId>
> <Status>0</Status>
> <Actions>
> <NumOfActions>3</NumOfActions>
> <Action Id="10001">
> <ActionId>10001</ActionId>
> <ActionName>Acme</ActionName>
> <ActionType>1</ActionType>
> <From>00:00:00</From>
> <To>12:00:00</To>
> <ActionStart>2007-11-15T09:52:56.513</ActionStart>
> <ActionStop>2007-11-25T09:52:56.513</ActionStop>
> <ActionDurationThreshold>8</ActionDurationThreshold>
> <ActionVideoURL>http://www.dynu.com/media/video/video1.g3p</
> ActionVideoURL>
> </Action>
> <Action Id="10002">
> <ActionId>10002</ActionId>
> <ActionName>Pepsi</ActionName>
> <ActionType>1</ActionType>
> <From>12:00</From>
> <To>16:00:00</To>
> <ActionStart>2007-11-15T09:53:17.640</ActionStart>
> <ActionStop>2007-11-25T09:53:17.640</ActionStop>
> <ActionDurationThreshold>7</ActionDurationThreshold>
> <ActionVideoURL>http://www.dynu.com/media/video/video2.g3p</
> ActionVideoURL>
> </Action>
> <Action Id="10003">
> <ActionId>10003</ActionId>
> <ActionName>Momo Deterjan</ActionName>
> <ActionType>1</ActionType>
> <From>18:00:00</From>
> <To>22:00:00</To>
> <ActionStart>2007-11-15T09:56:32.950</ActionStart>
> <ActionStop>2007-11-25T09:56:32.950</ActionStop>
> <ActionDurationThreshold>6</ActionDurationThreshold>
> <ActionVideoURL>http://www.dynu.com/media/video/video2.g3p</
> ActionVideoURL>
> </Action>
> </Actions>
> </RESPONSE>
> **** END OF XML ************
> Therefore, what I want is to assign this XML to a variable. To do
> that, I tried the following (but failed):
> Exec @.RC=SP_F06_GetActions
> @.MSISDN,
> @.SessionId
> So, my QUESTION is HOW CAN I RETRIEVE THIS XML INTO A VARIABLE?
> Thanks in advance for your help and interest.
> Regards,
> Ali

How to return the result (XML) with FOR XML EXPLICIT

Dear SQLXML specialists and other animals,
I got the following SP, that runs a SQL statement with FOR XML
EXPLICIT:
**** SQL STATEMENT *****
CREATE PROCEDURE SP_F06_GetActions
(
@.MSISDNas varchar(20),
@.SessionIdas varchar(64)
)
AS
BEGIN
SELECT
1 as tag
,NULL as parent
,'F06' as [RESPONSE!1!Flow_Name!Element]
,[MSISDN] as [RESPONSE!1!MSISDN!Element]
,getdate() as [RESPONSE!1!Timestamp!Element]
,@.SessionId as [RESPONSE!1!SessionId!Element]
,0 as [RESPONSE!1!Status!Element]
,NULL as [Actions!2!NumOfActions!Element]
,NULL as [Action!3!Id]
,NULL as [Action!3!ActionId!Element]
,NULL as [Action!3!ActionName!Element]
,NULL as [Action!3!ActionType!Element]
,NULL as [Action!3!From!Element]
,NULL as [Action!3!To!Element]
,NULL as [Action!3!ActionStart!Element]
,NULL as [Action!3!ActionStop!Element]
,NULL as [Action!3!ActionDurationThreshold!Element]
,NULL as [Action!3!ActionVideoURL!Element]
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
UNION
SELECT
2
,1
,NULL
,NULL
,NULL
,NULL
,NULL
, dbo.getNumOfActionsByMSISDN(MSISDN)
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
UNION
SELECT
3
,2
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,[ActionId]
,[ActionId]
,[ActionName]
,[ActionType]
,[From]
,[To]
,[ActionStart]
,[ActionStop]
,[ActionDurationThreshold]
,[ActionVideoURL]
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
WHERE [MSISDN]=@.MSISDN
--ORDER BY 3, 1
FOR XML EXPLICIT
RETURN
END
GO
**** END OF SQL ***********
Well, it returns the following XML:
***** XML *********************
<RESPONSE>
<Flow_Name>F06</Flow_Name>
<MSISDN>9053*********</MSISDN>
<Timestamp>2007-11-15T14:37:24.467</Timestamp>
<SessionId>45717B60-0A0F-434B-9FB9-CC8F647BFEA7</SessionId>
<Status>0</Status>
<Actions>
<NumOfActions>3</NumOfActions>
<Action Id="10001">
<ActionId>10001</ActionId>
<ActionName>Acme</ActionName>
<ActionType>1</ActionType>
<From>00:00:00</From>
<To>12:00:00</To>
<ActionStart>2007-11-15T09:52:56.513</ActionStart>
<ActionStop>2007-11-25T09:52:56.513</ActionStop>
<ActionDurationThreshold>8</ActionDurationThreshold>
<ActionVideoURL>http://www.dynu.com/media/video/video1.g3p</
ActionVideoURL>
</Action>
<Action Id="10002">
<ActionId>10002</ActionId>
<ActionName>Pepsi</ActionName>
<ActionType>1</ActionType>
<From>12:00</From>
<To>16:00:00</To>
<ActionStart>2007-11-15T09:53:17.640</ActionStart>
<ActionStop>2007-11-25T09:53:17.640</ActionStop>
<ActionDurationThreshold>7</ActionDurationThreshold>
<ActionVideoURL>http://www.dynu.com/media/video/video2.g3p</
ActionVideoURL>
</Action>
<Action Id="10003">
<ActionId>10003</ActionId>
<ActionName>Momo Deterjan</ActionName>
<ActionType>1</ActionType>
<From>18:00:00</From>
<To>22:00:00</To>
<ActionStart>2007-11-15T09:56:32.950</ActionStart>
<ActionStop>2007-11-25T09:56:32.950</ActionStop>
<ActionDurationThreshold>6</ActionDurationThreshold>
<ActionVideoURL>http://www.dynu.com/media/video/video2.g3p</
ActionVideoURL>
</Action>
</Actions>
</RESPONSE>
**** END OF XML ************
Therefore, what I want is to assign this XML to a variable. To do
that, I tried the following (but failed):
Exec @.RC=SP_F06_GetActions
@.MSISDN,
@.SessionId
So, my QUESTION is HOW CAN I RETRIEVE THIS XML INTO A VARIABLE?
Thanks in advance for your help and interest.
Regards,
Ali
<ali.koyuncu@.gmail.com> wrote in message
news:ea5805dd-f725-49c8-a641-68f2ea767a14@.f13g2000hsa.googlegroups.com...
> Dear SQLXML specialists and other animals,
> I got the following SP, that runs a SQL statement with FOR XML
> EXPLICIT:
> **** SQL STATEMENT *****
> CREATE PROCEDURE SP_F06_GetActions
> (
> @.MSISDN as varchar(20),
> @.SessionId as varchar(64)
> )
> AS
> BEGIN
> SELECT
> 1 as tag
> ,NULL as parent
> ,'F06' as [RESPONSE!1!Flow_Name!Element]
> ,[MSISDN] as [RESPONSE!1!MSISDN!Element]
> ,getdate() as [RESPONSE!1!Timestamp!Element]
> ,@.SessionId as [RESPONSE!1!SessionId!Element]
> ,0 as [RESPONSE!1!Status!Element]
> ,NULL as [Actions!2!NumOfActions!Element]
> ,NULL as [Action!3!Id]
> ,NULL as [Action!3!ActionId!Element]
> ,NULL as [Action!3!ActionName!Element]
> ,NULL as [Action!3!ActionType!Element]
> ,NULL as [Action!3!From!Element]
> ,NULL as [Action!3!To!Element]
> ,NULL as [Action!3!ActionStart!Element]
> ,NULL as [Action!3!ActionStop!Element]
> ,NULL as [Action!3!ActionDurationThreshold!Element]
> ,NULL as [Action!3!ActionVideoURL!Element]
> FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
> UNION
> SELECT
> 2
> ,1
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> , dbo.getNumOfActionsByMSISDN(MSISDN)
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
> UNION
> SELECT
> 3
> ,2
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,[ActionId]
> ,[ActionId]
> ,[ActionName]
> ,[ActionType]
> ,[From]
> ,[To]
> ,[ActionStart]
> ,[ActionStop]
> ,[ActionDurationThreshold]
> ,[ActionVideoURL]
> FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
> WHERE [MSISDN]=@.MSISDN
> --ORDER BY 3, 1
> FOR XML EXPLICIT
> RETURN
> END
> GO
> **** END OF SQL ***********
> Well, it returns the following XML:
> ***** XML *********************
> <RESPONSE>
> <Flow_Name>F06</Flow_Name>
> <MSISDN>9053*********</MSISDN>
> <Timestamp>2007-11-15T14:37:24.467</Timestamp>
> <SessionId>45717B60-0A0F-434B-9FB9-CC8F647BFEA7</SessionId>
> <Status>0</Status>
> <Actions>
> <NumOfActions>3</NumOfActions>
> <Action Id="10001">
> <ActionId>10001</ActionId>
> <ActionName>Acme</ActionName>
> <ActionType>1</ActionType>
> <From>00:00:00</From>
> <To>12:00:00</To>
> <ActionStart>2007-11-15T09:52:56.513</ActionStart>
> <ActionStop>2007-11-25T09:52:56.513</ActionStop>
> <ActionDurationThreshold>8</ActionDurationThreshold>
> <ActionVideoURL>http://www.dynu.com/media/video/video1.g3p</
> ActionVideoURL>
> </Action>
> <Action Id="10002">
> <ActionId>10002</ActionId>
> <ActionName>Pepsi</ActionName>
> <ActionType>1</ActionType>
> <From>12:00</From>
> <To>16:00:00</To>
> <ActionStart>2007-11-15T09:53:17.640</ActionStart>
> <ActionStop>2007-11-25T09:53:17.640</ActionStop>
> <ActionDurationThreshold>7</ActionDurationThreshold>
> <ActionVideoURL>http://www.dynu.com/media/video/video2.g3p</
> ActionVideoURL>
> </Action>
> <Action Id="10003">
> <ActionId>10003</ActionId>
> <ActionName>Momo Deterjan</ActionName>
> <ActionType>1</ActionType>
> <From>18:00:00</From>
> <To>22:00:00</To>
> <ActionStart>2007-11-15T09:56:32.950</ActionStart>
> <ActionStop>2007-11-25T09:56:32.950</ActionStop>
> <ActionDurationThreshold>6</ActionDurationThreshold>
> <ActionVideoURL>http://www.dynu.com/media/video/video2.g3p</
> ActionVideoURL>
> </Action>
> </Actions>
> </RESPONSE>
> **** END OF XML ************
> Therefore, what I want is to assign this XML to a variable. To do
> that, I tried the following (but failed):
> Exec @.RC=SP_F06_GetActions
> @.MSISDN,
> @.SessionId
> So, my QUESTION is HOW CAN I RETRIEVE THIS XML INTO A VARIABLE?
> Thanks in advance for your help and interest.
> Regards,
> Ali
Which version of SQL Server? In 2000 you can't do what you want, in 2005 you
need to use the TYPE directive.
http://technet.microsoft.com/en-us/library/ms345110.aspx
Joe Fawcett (MVP - XML)
http://joe.fawcett.name
|||I use SQL Server 2005.
How to use TYPE? Can you give me simple example?
Thanks,
Ali
On 15 Kasm, 15:56, "Joe Fawcett" <joefawc...@.newsgroup.nospam> wrote:
> <ali.koyu...@.gmail.com> wrote in message
> news:ea5805dd-f725-49c8-a641-68f2ea767a14@.f13g2000hsa.googlegroups.com...
>
>
>
>
>
>
>
>
>
> Which version of SQL Server? In 2000 you can't do what you want, in 2005 you
> need to use the TYPE directive.http://technet.microsoft.com/en-us/library/ms345110.aspx
> --
> Joe Fawcett (MVP - XML)
> http://joe.fawcett.name
|||I use SQL Server 2005.
How to use TYPE? Can you give me simple example?
Thanks,
Ali
On 15 Kasm, 15:56, "Joe Fawcett" <joefawc...@.newsgroup.nospam> wrote:
> <ali.koyu...@.gmail.com> wrote in message
> news:ea5805dd-f725-49c8-a641-68f2ea767a14@.f13g2000hsa.googlegroups.com...
>
>
>
>
>
>
>
>
>
> Which version of SQL Server? In 2000 you can't do what you want, in 2005 you
> need to use the TYPE directive.http://technet.microsoft.com/en-us/library/ms345110.aspx
> --
> Joe Fawcett (MVP - XML)
> http://joe.fawcett.name
|||I use SQL Server 2005.
How to use TYPE? Can you give me simple example?
Thanks,
Ali
On 15 Kasm, 15:56, "Joe Fawcett" <joefawc...@.newsgroup.nospam> wrote:
> <ali.koyu...@.gmail.com> wrote in message
> news:ea5805dd-f725-49c8-a641-68f2ea767a14@.f13g2000hsa.googlegroups.com...
>
>
>
>
>
>
>
>
>
> Which version of SQL Server? In 2000 you can't do what you want, in 2005 you
> need to use the TYPE directive.http://technet.microsoft.com/en-us/library/ms345110.aspx
> --
> Joe Fawcett (MVP - XML)
> http://joe.fawcett.name
|||When I use TYPE, it gave me the following error *damn*
Msg 1086, Level 15, State 1, Line 72
The FOR XML clause is invalid in views, inline functions, derived
tables, and subqueries when they contain a set operator. To work
around, wrap the SELECT containing a set operator using derived table
syntax and apply FOR XML on top of it.
On 15 Kas?m, 16:08, ali.koyu...@.gmail.com wrote:[vbcol=seagreen]
> I use SQL Server 2005.
> How to use TYPE? Can you give me simple example?
> Thanks,
> Ali
> On 15 Kasym, 15:56, "Joe Fawcett" <joefawc...@.newsgroup.nospam> wrote:
>
>
>
>
>
>
>
>
>
>
>
|||At first, many thans to Joe...
then YAY! I solved. Just changed my SP as follows:
ALTER PROCEDURE SP_F06_GetActions
(
@.MSISDNas varchar(20),
@.SessionIdas varchar(64),
@.ResultXMLas xml=NULL output --NEW!
)
AS
BEGIN
SELECT
1 as tag
,NULL as parent
,'F06' as [RESPONSE!1!Flow_Name!Element]
,[MSISDN] as [RESPONSE!1!MSISDN!Element]
,getdate() as [RESPONSE!1!Timestamp!Element]
,@.SessionId as [RESPONSE!1!SessionId!Element]
,0 as [RESPONSE!1!Status!Element]
,NULL as [Actions!2!NumOfActions!Element]
,NULL as [Action!3!Id]
,NULL as [Action!3!ActionId!Element]
,NULL as [Action!3!ActionName!Element]
,NULL as [Action!3!ActionType!Element]
,NULL as [Action!3!From!Element]
,NULL as [Action!3!To!Element]
,NULL as [Action!3!ActionStart!Element]
,NULL as [Action!3!ActionStop!Element]
,NULL as [Action!3!ActionDurationThreshold!Element]
,NULL as [Action!3!ActionVideoURL!Element]
INTO #TempTable -- DEFINING A TEMP TABLE!
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
UNION
SELECT
2
,1
,NULL
,NULL
,NULL
,NULL
,NULL
, dbo.getNumOfActionsByMSISDN(MSISDN)
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
UNION
SELECT
3
,2
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,[ActionId]
,[ActionId]
,[ActionName]
,[ActionType]
,[From]
,[To]
,[ActionStart]
,[ActionStop]
,[ActionDurationThreshold]
,[ActionVideoURL]
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
WHERE [MSISDN]=@.MSISDN
SET @.ResultXML=(SELECT * FROM #TempTable FOR XML EXPLICIT, TYPE)
DROP TABLE #TempTable
RETURN
END
GO
On 15 Kas?m, 16:08, ali.koyu...@.gmail.com wrote:[vbcol=seagreen]
> I use SQL Server 2005.
> How to use TYPE? Can you give me simple example?
> Thanks,
> Ali
> On 15 Kasym, 15:56, "Joe Fawcett" <joefawc...@.newsgroup.nospam> wrote:
>
>
>
>
>
>
>
>
>
>
>
|||At first, many thans to Joe...
then YAY! I solved. Just changed my SP as follows:
ALTER PROCEDURE SP_F06_GetActions
(
@.MSISDNas varchar(20),
@.SessionIdas varchar(64),
@.ResultXMLas xml=NULL output --NEW!
)
AS
BEGIN
SELECT
1 as tag
,NULL as parent
,'F06' as [RESPONSE!1!Flow_Name!Element]
,[MSISDN] as [RESPONSE!1!MSISDN!Element]
,getdate() as [RESPONSE!1!Timestamp!Element]
,@.SessionId as [RESPONSE!1!SessionId!Element]
,0 as [RESPONSE!1!Status!Element]
,NULL as [Actions!2!NumOfActions!Element]
,NULL as [Action!3!Id]
,NULL as [Action!3!ActionId!Element]
,NULL as [Action!3!ActionName!Element]
,NULL as [Action!3!ActionType!Element]
,NULL as [Action!3!From!Element]
,NULL as [Action!3!To!Element]
,NULL as [Action!3!ActionStart!Element]
,NULL as [Action!3!ActionStop!Element]
,NULL as [Action!3!ActionDurationThreshold!Element]
,NULL as [Action!3!ActionVideoURL!Element]
INTO #TempTable -- DEFINING A TEMP TABLE!
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
UNION
SELECT
2
,1
,NULL
,NULL
,NULL
,NULL
,NULL
, dbo.getNumOfActionsByMSISDN(MSISDN)
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
UNION
SELECT
3
,2
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,[ActionId]
,[ActionId]
,[ActionName]
,[ActionType]
,[From]
,[To]
,[ActionStart]
,[ActionStop]
,[ActionDurationThreshold]
,[ActionVideoURL]
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
WHERE [MSISDN]=@.MSISDN
SET @.ResultXML=(SELECT * FROM #TempTable FOR XML EXPLICIT, TYPE)
DROP TABLE #TempTable
RETURN
END
GO
On 15 Kas?m, 16:08, ali.koyu...@.gmail.com wrote:[vbcol=seagreen]
> I use SQL Server 2005.
> How to use TYPE? Can you give me simple example?
> Thanks,
> Ali
> On 15 Kasym, 15:56, "Joe Fawcett" <joefawc...@.newsgroup.nospam> wrote:
>
>
>
>
>
>
>
>
>
>
>
|||At first, many thans to Joe...
then YAY! I solved. Just changed my SP as follows:
ALTER PROCEDURE SP_F06_GetActions
(
@.MSISDNas varchar(20),
@.SessionIdas varchar(64),
@.ResultXMLas xml=NULL output --NEW!
)
AS
BEGIN
SELECT
1 as tag
,NULL as parent
,'F06' as [RESPONSE!1!Flow_Name!Element]
,[MSISDN] as [RESPONSE!1!MSISDN!Element]
,getdate() as [RESPONSE!1!Timestamp!Element]
,@.SessionId as [RESPONSE!1!SessionId!Element]
,0 as [RESPONSE!1!Status!Element]
,NULL as [Actions!2!NumOfActions!Element]
,NULL as [Action!3!Id]
,NULL as [Action!3!ActionId!Element]
,NULL as [Action!3!ActionName!Element]
,NULL as [Action!3!ActionType!Element]
,NULL as [Action!3!From!Element]
,NULL as [Action!3!To!Element]
,NULL as [Action!3!ActionStart!Element]
,NULL as [Action!3!ActionStop!Element]
,NULL as [Action!3!ActionDurationThreshold!Element]
,NULL as [Action!3!ActionVideoURL!Element]
INTO #TempTable -- DEFINING A TEMP TABLE!
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
UNION
SELECT
2
,1
,NULL
,NULL
,NULL
,NULL
,NULL
, dbo.getNumOfActionsByMSISDN(MSISDN)
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
UNION
SELECT
3
,2
,NULL
,NULL
,NULL
,NULL
,NULL
,NULL
,[ActionId]
,[ActionId]
,[ActionName]
,[ActionType]
,[From]
,[To]
,[ActionStart]
,[ActionStop]
,[ActionDurationThreshold]
,[ActionVideoURL]
FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
WHERE [MSISDN]=@.MSISDN
SET @.ResultXML=(SELECT * FROM #TempTable FOR XML EXPLICIT, TYPE)
DROP TABLE #TempTable
RETURN
END
GO
On 15 Kas?m, 16:08, ali.koyu...@.gmail.com wrote:[vbcol=seagreen]
> I use SQL Server 2005.
> How to use TYPE? Can you give me simple example?
> Thanks,
> Ali
> On 15 Kasym, 15:56, "Joe Fawcett" <joefawc...@.newsgroup.nospam> wrote:
>
>
>
>
>
>
>
>
>
>
>
|||If you're using SQL 2005, how about using FOR XML PATH instead? Much
simpler.
<ali.koyuncu@.gmail.com> wrote in message
news:ea5805dd-f725-49c8-a641-68f2ea767a14@.f13g2000hsa.googlegroups.com...
> Dear SQLXML specialists and other animals,
> I got the following SP, that runs a SQL statement with FOR XML
> EXPLICIT:
> **** SQL STATEMENT *****
> CREATE PROCEDURE SP_F06_GetActions
> (
> @.MSISDN as varchar(20),
> @.SessionId as varchar(64)
> )
> AS
> BEGIN
> SELECT
> 1 as tag
> ,NULL as parent
> ,'F06' as [RESPONSE!1!Flow_Name!Element]
> ,[MSISDN] as [RESPONSE!1!MSISDN!Element]
> ,getdate() as [RESPONSE!1!Timestamp!Element]
> ,@.SessionId as [RESPONSE!1!SessionId!Element]
> ,0 as [RESPONSE!1!Status!Element]
> ,NULL as [Actions!2!NumOfActions!Element]
> ,NULL as [Action!3!Id]
> ,NULL as [Action!3!ActionId!Element]
> ,NULL as [Action!3!ActionName!Element]
> ,NULL as [Action!3!ActionType!Element]
> ,NULL as [Action!3!From!Element]
> ,NULL as [Action!3!To!Element]
> ,NULL as [Action!3!ActionStart!Element]
> ,NULL as [Action!3!ActionStop!Element]
> ,NULL as [Action!3!ActionDurationThreshold!Element]
> ,NULL as [Action!3!ActionVideoURL!Element]
> FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
> UNION
> SELECT
> 2
> ,1
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> , dbo.getNumOfActionsByMSISDN(MSISDN)
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
> UNION
> SELECT
> 3
> ,2
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,NULL
> ,[ActionId]
> ,[ActionId]
> ,[ActionName]
> ,[ActionType]
> ,[From]
> ,[To]
> ,[ActionStart]
> ,[ActionStop]
> ,[ActionDurationThreshold]
> ,[ActionVideoURL]
> FROM [CampaignDB].[dbo].[VI_MSISDN_RELATED_ACTIONS]
> WHERE [MSISDN]=@.MSISDN
> --ORDER BY 3, 1
> FOR XML EXPLICIT
> RETURN
> END
> GO
> **** END OF SQL ***********
> Well, it returns the following XML:
> ***** XML *********************
> <RESPONSE>
> <Flow_Name>F06</Flow_Name>
> <MSISDN>9053*********</MSISDN>
> <Timestamp>2007-11-15T14:37:24.467</Timestamp>
> <SessionId>45717B60-0A0F-434B-9FB9-CC8F647BFEA7</SessionId>
> <Status>0</Status>
> <Actions>
> <NumOfActions>3</NumOfActions>
> <Action Id="10001">
> <ActionId>10001</ActionId>
> <ActionName>Acme</ActionName>
> <ActionType>1</ActionType>
> <From>00:00:00</From>
> <To>12:00:00</To>
> <ActionStart>2007-11-15T09:52:56.513</ActionStart>
> <ActionStop>2007-11-25T09:52:56.513</ActionStop>
> <ActionDurationThreshold>8</ActionDurationThreshold>
> <ActionVideoURL>http://www.dynu.com/media/video/video1.g3p</
> ActionVideoURL>
> </Action>
> <Action Id="10002">
> <ActionId>10002</ActionId>
> <ActionName>Pepsi</ActionName>
> <ActionType>1</ActionType>
> <From>12:00</From>
> <To>16:00:00</To>
> <ActionStart>2007-11-15T09:53:17.640</ActionStart>
> <ActionStop>2007-11-25T09:53:17.640</ActionStop>
> <ActionDurationThreshold>7</ActionDurationThreshold>
> <ActionVideoURL>http://www.dynu.com/media/video/video2.g3p</
> ActionVideoURL>
> </Action>
> <Action Id="10003">
> <ActionId>10003</ActionId>
> <ActionName>Momo Deterjan</ActionName>
> <ActionType>1</ActionType>
> <From>18:00:00</From>
> <To>22:00:00</To>
> <ActionStart>2007-11-15T09:56:32.950</ActionStart>
> <ActionStop>2007-11-25T09:56:32.950</ActionStop>
> <ActionDurationThreshold>6</ActionDurationThreshold>
> <ActionVideoURL>http://www.dynu.com/media/video/video2.g3p</
> ActionVideoURL>
> </Action>
> </Actions>
> </RESPONSE>
> **** END OF XML ************
> Therefore, what I want is to assign this XML to a variable. To do
> that, I tried the following (but failed):
> Exec @.RC=SP_F06_GetActions
> @.MSISDN,
> @.SessionId
> So, my QUESTION is HOW CAN I RETRIEVE THIS XML INTO A VARIABLE?
> Thanks in advance for your help and interest.
> Regards,
> Ali

How to return the difference between simple xml on SQL Server 2005?

I have two simple xml as:
<row>
<a>1</a>
<b>3</b>
<c>5</c>
</row>
<row>
<a>2</a>
<b>3</b>
<c>7</c>
</row>
What statement can return the difference between xml as result as:
<row>
<a>Yes</a>
<b>No</b>
<c>Yes</c>
</row>
-- This works, but it's ugly...
DECLARE @.x1 XML, @.x2 XML, @.x XML, @.xv varchar(MAX)
SET @.x1 = '<row><a>1</a><b>3</b><c>5</c></row>'
SET @.x2 = '<row><a>2</a><b>3</b><c>7</c></row>'
SET @.xv = ''
;WITH x1 AS
(SELECT
T.C.value('.', 'int') AS rowValue
, T.C.value('local-name(.)', 'varchar(10)') AS rowName
FROM @.x1.nodes('row/*') AS T(C))
,x2 AS
(SELECT
T.C.value('.', 'int') AS rowValue
, T.C.value('local-name(.)', 'varchar(10)') AS rowName
FROM @.x2.nodes('row/*') AS T(C))
SELECT @.xv = @.xv + ('<' + x1.rowName + '>'
+ CASE x1.rowValue WHEN x2.rowValue THEN 'Yes' ELSE 'No' END
+ '</' + x1.rowName + '>')
FROM x1 INNER JOIN x2 ON x1.rowName = x2.rowName
SELECT @.x = CAST('<row>' + @.xv + '</row>' AS XML)
SELECT @.x
Peter DeBetta, MVP - SQL Server
http://sqlblog.com
"ABC" <abc@.abc.com> wrote in message
news:ecz7aPjuHHA.1496@.TK2MSFTNGP06.phx.gbl...
>I have two simple xml as:
> <row>
> <a>1</a>
> <b>3</b>
> <c>5</c>
> </row>
>
> <row>
> <a>2</a>
> <b>3</b>
> <c>7</c>
> </row>
>
> What statement can return the difference between xml as result as:
> <row>
> <a>Yes</a>
> <b>No</b>
> <c>Yes</c>
> </row>
>
|||Note: If we could dynamically construct elements, there would be a much
easier solution...
Peter DeBetta, MVP - SQL Server
http://sqlblog.com
"ABC" <abc@.abc.com> wrote in message
news:ecz7aPjuHHA.1496@.TK2MSFTNGP06.phx.gbl...
>I have two simple xml as:
> <row>
> <a>1</a>
> <b>3</b>
> <c>5</c>
> </row>
>
> <row>
> <a>2</a>
> <b>3</b>
> <c>7</c>
> </row>
>
> What statement can return the difference between xml as result as:
> <row>
> <a>Yes</a>
> <b>No</b>
> <c>Yes</c>
> </row>
>
|||Thanks you for helpful, I think it can help me.
How about dynamically construct elements can be easiler solution?
"Peter W. DeBetta" <debettap@.hotmail.com> wrote in message
news:eP%23NzKQvHHA.3364@.TK2MSFTNGP02.phx.gbl...
> Note: If we could dynamically construct elements, there would be a much
> easier solution...
> --
> Peter DeBetta, MVP - SQL Server
> http://sqlblog.com
> --
> "ABC" <abc@.abc.com> wrote in message
> news:ecz7aPjuHHA.1496@.TK2MSFTNGP06.phx.gbl...
>

How to return the difference between simple xml on SQL Server 2005?

I have two simple xml as:
<row>
<a>1</a>
<b>3</b>
<c>5</c>
</row>
<row>
<a>2</a>
<b>3</b>
<c>7</c>
</row>
What statement can return the difference between xml as result as:
<row>
<a>Yes</a>
<b>No</b>
<c>Yes</c>
</row>-- This works, but it's ugly...
DECLARE @.x1 XML, @.x2 XML, @.x XML, @.xv varchar(MAX)
SET @.x1 = '<row><a>1</a><b>3</b><c>5</c></row>'
SET @.x2 = '<row><a>2</a><b>3</b><c>7</c></row>'
SET @.xv = ''
;WITH x1 AS
(SELECT
T.C.value('.', 'int') AS rowValue
, T.C.value('local-name(.)', 'varchar(10)') AS rowName
FROM @.x1.nodes('row/*') AS T(C))
,x2 AS
(SELECT
T.C.value('.', 'int') AS rowValue
, T.C.value('local-name(.)', 'varchar(10)') AS rowName
FROM @.x2.nodes('row/*') AS T(C))
SELECT @.xv = @.xv + ('<' + x1.rowName + '>'
+ CASE x1.rowValue WHEN x2.rowValue THEN 'Yes' ELSE 'No' END
+ '</' + x1.rowName + '>')
FROM x1 INNER JOIN x2 ON x1.rowName = x2.rowName
SELECT @.x = CAST('<row>' + @.xv + '</row>' AS XML)
SELECT @.x
Peter DeBetta, MVP - SQL Server
http://sqlblog.com
--
"ABC" <abc@.abc.com> wrote in message
news:ecz7aPjuHHA.1496@.TK2MSFTNGP06.phx.gbl...
>I have two simple xml as:
> <row>
> <a>1</a>
> <b>3</b>
> <c>5</c>
> </row>
>
> <row>
> <a>2</a>
> <b>3</b>
> <c>7</c>
> </row>
>
> What statement can return the difference between xml as result as:
> <row>
> <a>Yes</a>
> <b>No</b>
> <c>Yes</c>
> </row>
>|||Note: If we could dynamically construct elements, there would be a much
easier solution...
Peter DeBetta, MVP - SQL Server
http://sqlblog.com
--
"ABC" <abc@.abc.com> wrote in message
news:ecz7aPjuHHA.1496@.TK2MSFTNGP06.phx.gbl...
>I have two simple xml as:
> <row>
> <a>1</a>
> <b>3</b>
> <c>5</c>
> </row>
>
> <row>
> <a>2</a>
> <b>3</b>
> <c>7</c>
> </row>
>
> What statement can return the difference between xml as result as:
> <row>
> <a>Yes</a>
> <b>No</b>
> <c>Yes</c>
> </row>
>|||Thanks you for helpful, I think it can help me.
How about dynamically construct elements can be easiler solution?
"Peter W. DeBetta" <debettap@.hotmail.com> wrote in message
news:eP%23NzKQvHHA.3364@.TK2MSFTNGP02.phx.gbl...
> Note: If we could dynamically construct elements, there would be a much
> easier solution...
> --
> Peter DeBetta, MVP - SQL Server
> http://sqlblog.com
> --
> "ABC" <abc@.abc.com> wrote in message
> news:ecz7aPjuHHA.1496@.TK2MSFTNGP06.phx.gbl...
>

Monday, March 12, 2012

How to return one xml doument per row

When I do something like "select * from customer for XML auto" I get a single
string containing the xml for all customers in the customer table. I want one
xml string (i.e., document) per row so I can process one document at a time.
Is this possible?
Use FOR XML Explicit. This query should be written in a specific way.
something like this:
select 1 as Tag,
null as Parent,
customers.customerID as [Customer!1!CustomerID!id]
from customers
for xml explicit
Please refer to "EXPLICIT mode" in Books online.
Regards,
Deepak
[I Code, therefore I am]
"ACROWN" wrote:

> When I do something like "select * from customer for XML auto" I get a single
> string containing the xml for all customers in the customer table. I want one
> xml string (i.e., document) per row so I can process one document at a time.
> Is this possible?
|||FOR XML (both auto and explicit mode) is a rowset to XML aggregator. In SQL
Server 2000, this aggregator can only aggregate all the rows.
In SQL Server 2005, you will be able to write something along the lines of
Create table t (id int, name varchar(5))
go
insert into t values (1, 'a')
insert into t values (1, 'b')
insert into t values (2, 'c')
select (select * from t t1 where t1.id = t2.id for xml raw('t'), type)
from t t2
group by t2.id
HTH
Michael
"ACROWN" <ACROWN@.discussions.microsoft.com> wrote in message
news:6D633481-D083-4A18-9FB5-37AA9258FEEE@.microsoft.com...
> When I do something like "select * from customer for XML auto" I get a
> single
> string containing the xml for all customers in the customer table. I want
> one
> xml string (i.e., document) per row so I can process one document at a
> time.
> Is this possible?

How to return multiple columns from FLWOR

I have very simple xml shredding requirement. What I would like to do is to get value of SourcesID and SourcesType returned as columns from the following. Currently I could get both values concatenated but requirement dictate having both values returned as separate columns. Please notice that given statement is a simplified version and I understand using Value function would be an easier solution but I have set of conditions within for that needs to be evaluated before I select SourcesID node. So, I would appreciate if the solution proposed is along the line of given code.

DECLARE @.xml XML

SET @.xml =

'<State>

<SourceState>

<SourcesID>57341</SourcesID>

<SourcesType>50</SourcesType>

</SourceState>

</State>'

SELECT

Element.Val.query(

'for $s in self::node()

return $s//SourcesID/text(),

for $s in self::node()

return $s//SourcesType/text()

') AS SourcesID

FROM @.xml.nodes('/State') AS Element(Val)

Asaf:

I think that I still don't understand what you are looking for in terms of output. Here is my first pass at trying to understand your needs. Please comment so that I might be able to properly target this.

Code Snippet

DECLARE @.xml XML
SET @.xml =
'<State>
<SourceState>
<SourcesID>57341</SourcesID>
<SourcesType>50</SourcesType>
</SourceState>
</State>'
select parsename(cast(SourcesID as varchar), 2) as SourcesID,
substring(parseName(cast(SourcesID as varchar), 1), 2, 99)
as SourcesType
from ( SELECT
Element.Val.query(
'for $s in self::node()/SourceState/SourcesID/text()
return concat(string($s),"."),
for $s in self::node()/SourceState/SourcesType/text()
return string($s)
') AS SourcesID
FROM @.xml.nodes('/State') AS Element(Val)
) x

/*
SourcesID SourcesType
-- -
57341 50
*/

|||

Hi there,

I am sorry for having a bit confusing question but the proposed solution from you was what I needed. What I was hoping, though, was to basically get two columns returned from FLWOR without any concatenation and parsing of returned resultset later on.

Thanks very much.

|||

It is not clear what kind of condition you have but note that you can also apply conditions in path expressions in square brackets so perhaps your problem can be solved by simply applying the condition already when you use the nodes function e.g.

FROM @.xml.nodes('/State[SourceState/SourcesType > 40]') AS Element(Val)

then you can apply the value method to get two columns in the result.

|||

Code Snippet

DECLARE @.xml XML
SET @.xml =
'<State>
<SourceState>
<SourcesID>57341</SourcesID>
<SourcesType>50</SourcesType>
</SourceState>
</State>'
SELECT
Element.Val.query(
'for $s in self::node()
return $s//SourcesID/text()') as a,
Element.val.query(
'for $s in self::node()
return $s//SourcesType/text()') as b
FROM @.xml.nodes('/State') AS Element(Val)

/*
a b
-
57341 50
*/

How to return multiple columns from FLWOR

I have very simple xml shredding requirement. What I would like to do is to get value of SourcesID and SourcesType returned as columns from the following. Currently I could get both values concatenated but requirement dictate having both values returned as separate columns. Please notice that given statement is a simplified version and I understand using Value function would be an easier solution but I have set of conditions within for that needs to be evaluated before I select SourcesID node. So, I would appreciate if the solution proposed is along the line of given code.

DECLARE @.xml XML

SET @.xml =

'<State>

<SourceState>

<SourcesID>57341</SourcesID>

<SourcesType>50</SourcesType>

</SourceState>

</State>'

SELECT

Element.Val.query(

'for $s in self::node()

return $s//SourcesID/text(),

for $s in self::node()

return $s//SourcesType/text()

') AS SourcesID

FROM @.xml.nodes('/State') AS Element(Val)

Asaf:

I think that I still don't understand what you are looking for in terms of output. Here is my first pass at trying to understand your needs. Please comment so that I might be able to properly target this.

Code Snippet

DECLARE @.xml XML
SET @.xml =
'<State>
<SourceState>
<SourcesID>57341</SourcesID>
<SourcesType>50</SourcesType>
</SourceState>
</State>'
select parsename(cast(SourcesID as varchar), 2) as SourcesID,
substring(parseName(cast(SourcesID as varchar), 1), 2, 99)
as SourcesType
from ( SELECT
Element.Val.query(
'for $s in self::node()/SourceState/SourcesID/text()
return concat(string($s),"."),
for $s in self::node()/SourceState/SourcesType/text()
return string($s)
') AS SourcesID
FROM @.xml.nodes('/State') AS Element(Val)
) x

/*
SourcesID SourcesType
-- -
57341 50
*/

|||

Hi there,

I am sorry for having a bit confusing question but the proposed solution from you was what I needed. What I was hoping, though, was to basically get two columns returned from FLWOR without any concatenation and parsing of returned resultset later on.

Thanks very much.

|||

It is not clear what kind of condition you have but note that you can also apply conditions in path expressions in square brackets so perhaps your problem can be solved by simply applying the condition already when you use the nodes function e.g.

FROM @.xml.nodes('/State[SourceState/SourcesType > 40]') AS Element(Val)

then you can apply the value method to get two columns in the result.

|||

Code Snippet

DECLARE @.xml XML
SET @.xml =
'<State>
<SourceState>
<SourcesID>57341</SourcesID>
<SourcesType>50</SourcesType>
</SourceState>
</State>'
SELECT
Element.Val.query(
'for $s in self::node()
return $s//SourcesID/text()') as a,
Element.val.query(
'for $s in self::node()
return $s//SourcesType/text()') as b
FROM @.xml.nodes('/State') AS Element(Val)

/*
a b
-
57341 50
*/

How to return large amount of data in the XML format

I have SQL 2000 and need to retrieve fairly large amout of data (~
50.000 characters) in XML format and then insert it into the field of
the text type.
As 'FOR XML' can't be used with either local variables, INSERT INTO or
SELECT INTO this makes "XML support" quite useless in many aspects.

Can anyone please help me in solving this.
Thanks a lot for your help and time.

PavelPavel (p.golobokov@.ausbulk.com.au) writes:
> I have SQL 2000 and need to retrieve fairly large amout of data (~
> 50.000 characters) in XML format and then insert it into the field of
> the text type.
> As 'FOR XML' can't be used with either local variables, INSERT INTO or
> SELECT INTO this makes "XML support" quite useless in many aspects.

You can try:

INSERT tbl (xmlcol)
SELECT * FROM OPENQUERY(LOCALSVR, 'SELECT ... FOR XML')

Where LOCALSVR has been created as

EXEC sp_addlinkedserver
@.server = 'LOCALVR',
@.srvproduct = '',
@.provider = 'MSDASQL',
@.datasrc = 'LocalServer'

That is, you use the deprecated OLE DB over ODBC provider. This works
so far that you get XML back. However, you may find that the text
has been broken into many rows. (If you would use SQLOLEDB, the real
SQL Server provider, you get a blob back.)

If this does not work out, you will have a find a client to pick up the
XML and send it back.

In SQL 2005, the XML support is considerably enhanced, and you should
be able to do this without weird workarounds.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Friday, March 9, 2012

How to return a data list from the attributes on xml column?

I have a table which contain a xml column.
Currently, I use for-each-row strategy to store xml value apply OPENXML
function to scan xml-row.
It is very very spend-time when there are many rows on table.
Is there any another method?Hi,
Do you mean that the table already has an SQL Server 2005 native XML in it?
You can always (even in SQL Server 2000) use SELECT ... FOR XML to store
rowset information in XML format, without using a for-each-row strategy. SQL
Server 2005 has many enhancements to SELECT... FOR XML, including the
ability to store rowset information as a native XML SQL Server data types.
In SQL Server 2005, the XML data type's nodes() method can be used instead
of OPENXML. It's usually quicker than OPENXML and takes less memory than
OPENXML to process.
Hope this helps,
Bob Beauchemin
http://www.SQLskills.com/blogs/bobb
"ABC" <abc@.abc.com> wrote in message
news:O4l40pEPHHA.5000@.TK2MSFTNGP03.phx.gbl...
>I have a table which contain a xml column.
> Currently, I use for-each-row strategy to store xml value apply OPENXML
> function to scan xml-row.
> It is very very spend-time when there are many rows on table.
> Is there any another method?
>
>

How to return a data list from the attributes on xml column?

I have a table which contain a xml column.
Currently, I use for-each-row strategy to store xml value apply OPENXML
function to scan xml-row.
It is very very spend-time when there are many rows on table.
Is there any another method?
Hi,
Do you mean that the table already has an SQL Server 2005 native XML in it?
You can always (even in SQL Server 2000) use SELECT ... FOR XML to store
rowset information in XML format, without using a for-each-row strategy. SQL
Server 2005 has many enhancements to SELECT... FOR XML, including the
ability to store rowset information as a native XML SQL Server data types.
In SQL Server 2005, the XML data type's nodes() method can be used instead
of OPENXML. It's usually quicker than OPENXML and takes less memory than
OPENXML to process.
Hope this helps,
Bob Beauchemin
http://www.SQLskills.com/blogs/bobb
"ABC" <abc@.abc.com> wrote in message
news:O4l40pEPHHA.5000@.TK2MSFTNGP03.phx.gbl...
>I have a table which contain a xml column.
> Currently, I use for-each-row strategy to store xml value apply OPENXML
> function to scan xml-row.
> It is very very spend-time when there are many rows on table.
> Is there any another method?
>
>

How to retrieve xml element name in sql server 2005?

Hi
Here's a simple example of what I'm trying to achieve:
declare @.xmlNode xml
set @.xmlNode='
<a id="1">
<b id="2" />
<c id="3" />
</a>'
select @.xmlNode.query('//*[@.id="1"]/name()') -- fails since name()
function not allowed
I need to be able to return the name of an element for a particular id,
so the above example should return the value 'a'. If the search were
for id="3" the the result should be 'c'.
However, it seems that the name() function is not (yet?) supported in
sql server 2005. I've looked into local-name() but that's only
supported in a predicate/selector so I can't retrieve an actual value
from it.
I've also looked into the possibility of using the mp:localname
metaproperty in openxml but it doesn't seem like the right solution
since it should be possible to accomplish from within xquery.
It's probably staring me in the face, but I just can't seem to find the
solution.
Any help very much appreciated.
Regards, JamesCorrect syntax of name() and local-name() is,
name(xpath)
not,
xpath/name()
Yes, name() is not yet supported. Use local-name().
Additionally, xpath2.0 or xquery1.0 is occurence sensitive. So called
singleton is required for the local-name(). This is an error.
local-name(//*[@.id="1"])
Do instead,
local-name((//*[@.id="1"])[1])
Pohwan Han. Seoul. Have a nice day.
"jamesagnew" <jamesagnew@.hotmail.com> wrote in message
news:1125962687.996958.18470@.o13g2000cwo.googlegroups.com...
> Hi
> Here's a simple example of what I'm trying to achieve:
> --
> declare @.xmlNode xml
> set @.xmlNode='
> <a id="1">
> <b id="2" />
> <c id="3" />
> </a>'
> select @.xmlNode.query('//*[@.id="1"]/name()') -- fails since name()
> function not allowed
> --
> I need to be able to return the name of an element for a particular id,
> so the above example should return the value 'a'. If the search were
> for id="3" the the result should be 'c'.
> However, it seems that the name() function is not (yet?) supported in
> sql server 2005. I've looked into local-name() but that's only
> supported in a predicate/selector so I can't retrieve an actual value
> from it.
> I've also looked into the possibility of using the mp:localname
> metaproperty in openxml but it doesn't seem like the right solution
> since it should be possible to accomplish from within xquery.
> It's probably staring me in the face, but I just can't seem to find the
> solution.
> Any help very much appreciated.
> Regards, James
>|||That's it! I've got it working as follows:
select @.xmlNode.value('local-name((//*[@.id="1"])[1])','nvarchar(55)')
Han, you're a great asset to this list and your help is much
appreciated.
Thanks again, James

How to retrieve xml element name in sql server 2005?

Hi
Here's a simple example of what I'm trying to achieve:
declare @.xmlNode xml
set @.xmlNode='
<a id="1">
<b id="2" />
<c id="3" />
</a>'
select @.xmlNode.query('//*[@.id="1"]/name()') -- fails since name()
function not allowed
I need to be able to return the name of an element for a particular id,
so the above example should return the value 'a'. If the search were
for id="3" the the result should be 'c'.
However, it seems that the name() function is not (yet?) supported in
sql server 2005. I've looked into local-name() but that's only
supported in a predicate/selector so I can't retrieve an actual value
from it.
I've also looked into the possibility of using the mp:localname
metaproperty in openxml but it doesn't seem like the right solution
since it should be possible to accomplish from within xquery.
It's probably staring me in the face, but I just can't seem to find the
solution.
Any help very much appreciated.
Regards, James
Correct syntax of name() and local-name() is,
name(xpath)
not,
xpath/name()
Yes, name() is not yet supported. Use local-name().
Additionally, xpath2.0 or xquery1.0 is occurence sensitive. So called
singleton is required for the local-name(). This is an error.
local-name(//*[@.id="1"])
Do instead,
local-name((//*[@.id="1"])[1])
Pohwan Han. Seoul. Have a nice day.
"jamesagnew" <jamesagnew@.hotmail.com> wrote in message
news:1125962687.996958.18470@.o13g2000cwo.googlegro ups.com...
> Hi
> Here's a simple example of what I'm trying to achieve:
> --
> declare @.xmlNode xml
> set @.xmlNode='
> <a id="1">
> <b id="2" />
> <c id="3" />
> </a>'
> select @.xmlNode.query('//*[@.id="1"]/name()') -- fails since name()
> function not allowed
> --
> I need to be able to return the name of an element for a particular id,
> so the above example should return the value 'a'. If the search were
> for id="3" the the result should be 'c'.
> However, it seems that the name() function is not (yet?) supported in
> sql server 2005. I've looked into local-name() but that's only
> supported in a predicate/selector so I can't retrieve an actual value
> from it.
> I've also looked into the possibility of using the mp:localname
> metaproperty in openxml but it doesn't seem like the right solution
> since it should be possible to accomplish from within xquery.
> It's probably staring me in the face, but I just can't seem to find the
> solution.
> Any help very much appreciated.
> Regards, James
>
|||That's it! I've got it working as follows:
select @.xmlNode.value('local-name((//*[@.id="1"])[1])','nvarchar(55)')
Han, you're a great asset to this list and your help is much
appreciated.
Thanks again, James

Friday, February 24, 2012

How to retrieve data as XML from SQL database

Hi,

I have a website which is designed to search for employee information. I have the search system working which does exactly what I want to, but as an added feature I want there to be a button which, when someone clicks on it, it takes whatever the previous search was and generates a set of data in XML format which is based on the results. For example:

User searches for all entries with Forename = John; Results are listed in a gridview as per expected.

User then presses button with XML on it, and page pops up with just the XML output on it, i.e. whatever results are on the gridview but in a nested XML format

<records>
<record>
<Forename>John</Forename>
<Surname>Smith</Surname>
<Email>j.smith@.blah.com</Email>
<Ext>1234</Ext>
<DeptList>History</DeptList>
</record>
</records>

I have created a stored procedure which will take the parameters from the search boxes and return the above information, but I don't know if this is the best way. Here it is for those interested:

CREATE

PROCEDURE ps_record_SELECT_NameSurnameEmailExtDeptasXML

@.Forename

varchar(50),

@.Surname

varchar(50),

@.Email

varchar(50),

@.Ext

varchar(4),

@.DeptList

varchar(50)

AS

SELECT

Forename, Surname, Email, Ext, DeptList

FROM

dbo.record

WHERE

ForenameLIKECOALESCE(@.Forename,Forename)AND

Surname

LIKECOALESCE(@.Surname,Surname)AND

Email

LIKECOALESCE(@.Email,Email)AND

Ext

LIKECOALESCE(@.Ext,Ext)AND

DeptList

LIKECOALESCE(@.DeptList,DeptList)

FOR

XMLAUTO,ELEMENTS

If someone could be kind enough to help me out with this, I'd be really grateful.

Many thanks,

Tom

Hi tomosap,

Yes, this is a good way to get that.

Another way is to fill everything to run the query and fill data into a DataSet. You will be able to get the DataSet's data in XML format by calling DataSet.WriteXml() method.

Both these methods are good.

HTH. If this does not answer you question, please feel free to mark it as Not Answered and post your reply. Thanks!

|||

Hi,

Thanks for your reply - I'm afraid that my needs have slightly changed since this was posted. I now have an XML file which I transform using XSL so it displays all the data in a gridview. What I want to do is to have a search form so that I can search the data that's in the gridview. I.e.

Name: [INPUT]

Results in Gridview...

User enters name in input, clicks search, then all matches in Name column on the gridview is displayed. I've successfully managed to do this with a basic SQL database using the object source control and passing paramaters, but I don't know how to do this when the data source is XML.

All help would be appreciated.

Thanks,

Tom