Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

exec linkedserver.dbo.forxml_proc: why do I receive "binar..

 
   Database Help (Home) -> Programming RSS
Next:  check the word whether correct or not  
Author Message
DC

External


Since: Jan 11, 2008
Posts: 13



(Msg. 1) Posted: Sat Jan 12, 2008 1:37 am
Post subject: exec linkedserver.dbo.forxml_proc: why do I receive "binary" data?
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Hi,

I guess this is an old topic and I also tried to get an answer in
the ...xml group but to no avail - sorry for the repost. I have a
stored procedure "xmlproc" on "sqlserver_A", which returns xml via a
"select ... for xml auto" query. "sqlserver_B" is linked to
sqlserver_A (I tried to link as "SQL Server", "Native Client" and
"OLEDB") and calls

exec sqlserver_A.mydb.dbo.xmlproc

But instead of returning xml data, I receive a binary string that
looks like

0x440C690064005F0050006F0072007...

Is there anyway around this?

TIA, Regards
DC

 >> Stay informed about: exec linkedserver.dbo.forxml_proc: why do I receive ""binar.. 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 2) Posted: Sat Jan 12, 2008 7:48 am
Post subject: Re: exec linkedserver.dbo.forxml_proc: why do I receive "binary" data? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

DC (dc@upsize.de) writes:
> I guess this is an old topic and I also tried to get an answer in
> the ...xml group but to no avail - sorry for the repost. I have a
> stored procedure "xmlproc" on "sqlserver_A", which returns xml via a
> "select ... for xml auto" query. "sqlserver_B" is linked to
> sqlserver_A (I tried to link as "SQL Server", "Native Client" and
> "OLEDB") and calls
>
> exec sqlserver_A.mydb.dbo.xmlproc
>
> But instead of returning xml data, I receive a binary string that
> looks like
>
> 0x440C690064005F0050006F0072007...
>
> Is there anyway around this?

First why this happens: when FOR XML data is returned through OLE DB,
it is transmitted in a binary format which is more compact than the
text version. The client is assumed to be able to unpack this, and
indeed you can do this in an OLE DB client. However, the application
needs to know that FOR XML data is coming and use different API calls.
In this situation, the client is the calling SQL Server. But there is
no way to tell SQL Server "Hey, I will call a remote SP that emits FOR
XML, so do it right, will you?". (A similar problem exists with SQLCMD:
in SQLCMD if you use FOR XML you will just get a binary back. However,
SQLCMD has a mode switch to deal with the situation.)

When researching alternatives, I found that this was a little more
difficult that I thought. The first attempt was to convert the returned
binary data to xml as suggested by Rubén. No, it is not that simple.
The next idea was to rewrite the procedure to have an output parameter
of the XML datatype. Alas, it is not supported to have output parameter
of LOB types (xml, varchar(MAX) etc) in remote procedure calls.
Eventually I arrived at this solution:

create procedure nisse as
declare @x varbinary(MAX)
set @x = convert(varbinary(MAX),
(select * from sys.objects for xml auto, type))
select @x

That is, add ", type" to the FOR XML clause, convert this to varbinary(MAX)
and return this. On the calling side:

create table #hh(a varbinary(MAX) NOT NULL)
insert #hh(a)
exec NOV07.tempdb.dbo.nisse
select convert(xml, a) from #hh

Use INSERT-EXEC to capture it and convert back to XML. Why the road over
varbinary(MAX)? Because XML is neither supported as result column in
remote queries.

The problem with this solution is that it requires you to rewrite the
procedure you call. If you cannot do this, I think your best option
is to write a CLR stored procedure that connects to the remote server,
possibly using the XmlReader to get the data. (It's not something I've
done myself, so I cannot offer any samples.)


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

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 >> Stay informed about: exec linkedserver.dbo.forxml_proc: why do I receive ""binar.. 
Back to top
Login to vote
Rubén_Garrigós

External


Since: Jan 10, 2008
Posts: 20



(Msg. 3) Posted: Sat Jan 12, 2008 3:29 pm
Post subject: Re: exec linkedserver.dbo.forxml_proc: why do I receive "binary" data? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

¿Have you tried to cast the binary data to an XML type?

--

Rubén Garrigós
Solid Quality Mentors

"DC" wrote in message

> Hi,
>
> I guess this is an old topic and I also tried to get an answer in
> the ...xml group but to no avail - sorry for the repost. I have a
> stored procedure "xmlproc" on "sqlserver_A", which returns xml via a
> "select ... for xml auto" query. "sqlserver_B" is linked to
> sqlserver_A (I tried to link as "SQL Server", "Native Client" and
> "OLEDB") and calls
>
> exec sqlserver_A.mydb.dbo.xmlproc
>
> But instead of returning xml data, I receive a binary string that
> looks like
>
> 0x440C690064005F0050006F0072007...
>
> Is there anyway around this?
>
> TIA, Regards
> DC
 >> Stay informed about: exec linkedserver.dbo.forxml_proc: why do I receive ""binar.. 
Back to top
Login to vote
DC

External


Since: Jan 11, 2008
Posts: 13



(Msg. 4) Posted: Mon Jan 14, 2008 2:53 am
Post subject: Re: exec linkedserver.dbo.forxml_proc: why do I receive "binary" [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 12 Jan., 16:48, Erland Sommarskog wrote:
> DC (d...@upsize.de) writes:
> > I guess this is an old topic and I also tried to get an answer in
> > the ...xml group but to no avail - sorry for the repost. I have a
> > stored procedure "xmlproc" on "sqlserver_A", which returns xml via a
> > "select ... for xml auto" query. "sqlserver_B" is linked to
> > sqlserver_A (I tried to link as "SQL Server", "Native Client" and
> > "OLEDB") and calls
>
> > exec sqlserver_A.mydb.dbo.xmlproc
>
> > But instead of returning xml data, I receive a binary string that
> > looks like
>
> > 0x440C690064005F0050006F0072007...
>
> > Is there anyway around this?
>
> First why this happens: when FOR XML data is returned through OLE DB,
> it is transmitted in a binary format which is more compact than the
> text version. The client is assumed to be able to unpack this, and
> indeed you can do this in an OLE DB client. However, the application
> needs to know that FOR XML data is coming and use different API calls.
> In this situation, the client is the calling SQL Server. But there is
> no way to tell SQL Server "Hey, I will call a remote SP that emits FOR
> XML, so do it right, will you?". (A similar problem exists with SQLCMD:
> in SQLCMD if you use FOR XML you will just get a binary back. However,
> SQLCMD has a mode switch to deal with the situation.)
>
> When researching alternatives, I found that this was a little more
> difficult that I thought. The first attempt was to convert the returned
> binary data to xml as suggested by Rubén. No, it is not that simple.
> The next idea was to rewrite the procedure to have an output parameter
> of the XML datatype. Alas, it is not supported to have output parameter
> of LOB types (xml, varchar(MAX) etc) in remote procedure calls.
> Eventually I arrived at this solution:
>
>    create procedure nisse as
>    declare @x varbinary(MAX)
>    set @x = convert(varbinary(MAX),
>               (select * from sys.objects for xml auto, type))
>    select @x          
>
> That is, add ", type" to the FOR XML clause, convert this to varbinary(MAX)
> and return this. On the calling side:
>
>    create table #hh(a varbinary(MAX) NOT NULL)
>    insert #hh(a)
>    exec NOV07.tempdb.dbo.nisse
>    select convert(xml, a) from #hh
>
> Use INSERT-EXEC to capture it and convert back to XML. Why the road over
> varbinary(MAX)? Because XML is neither supported as result column in
> remote queries.
>
> The problem with this solution is that it requires you to rewrite the
> procedure you call. If you cannot do this, I think your best option
> is to write a CLR stored procedure that connects to the remote server,
> possibly using the XmlReader to get the data. (It's not something I've
> done myself, so I cannot offer any samples.)
>
> --
> Erland Sommarskog, SQL Server MVP, esq....TakeThisOut@sommarskog.se
>
> Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
> Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

Thanks a ton, Erland! You did all the research that I had to do
otherwise. Since it is much more complicated than I thought, it is
probably a better idea to request a recordset and do the "for xml" on
the target server.

Regards
DC
 >> Stay informed about: exec linkedserver.dbo.forxml_proc: why do I receive ""binar.. 
Back to top
Login to vote
Gavin Cannizzaro

External


Since: Mar 29, 2010
Posts: 1



(Msg. 5) Posted: Mon Mar 29, 2010 1:08 pm
Post subject: Thanks! [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I spent all afternoon writing a remote routine based on XML parameter, including OUTPUT. Your post saved me a lot of headaches.

Thanks, Erland!



Erland Sommarskog wrote:

Re: exec linkedserver.dbo.forxml_proc: why do I receive "binary" data?
12-Jan-08

DC (dc@upsize.de) writes:

First why this happens: when FOR XML data is returned through OLE DB,
it is transmitted in a binary format which is more compact than the
text version. The client is assumed to be able to unpack this, and
indeed you can do this in an OLE DB client. However, the application
needs to know that FOR XML data is coming and use different API calls.
In this situation, the client is the calling SQL Server. But there is
no way to tell SQL Server "Hey, I will call a remote SP that emits FOR
XML, so do it right, will you?". (A similar problem exists with SQLCMD:
in SQLCMD if you use FOR XML you will just get a binary back. However,
SQLCMD has a mode switch to deal with the situation.)

When researching alternatives, I found that this was a little more
difficult that I thought. The first attempt was to convert the returned
binary data to xml as suggested by Rub?n. No, it is not that simple.
The next idea was to rewrite the procedure to have an output parameter
of the XML datatype. Alas, it is not supported to have output parameter
of LOB types (xml, varchar(MAX) etc) in remote procedure calls.
Eventually I arrived at this solution:

create procedure nisse as
declare @x varbinary(MAX)
set @x = convert(varbinary(MAX),
(select * from sys.objects for xml auto, type))
select @x

That is, add ", type" to the FOR XML clause, convert this to varbinary(MAX)
and return this. On the calling side:

create table #hh(a varbinary(MAX) NOT NULL)
insert #hh(a)
exec NOV07.tempdb.dbo.nisse
select convert(xml, a) from #hh

Use INSERT-EXEC to capture it and convert back to XML. Why the road over
varbinary(MAX)? Because XML is neither supported as result column in
remote queries.

The problem with this solution is that it requires you to rewrite the
procedure you call. If you cannot do this, I think your best option
is to write a CLR stored procedure that connects to the remote server,
possibly using the XmlReader to get the data. (It's not something I've
done myself, so I cannot offer any samples.)


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

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

Previous Posts In This Thread:

On Saturday, January 12, 2008 7:57 AM
DC wrote:

exec linkedserver.dbo.forxml_proc: why do I receive "binary" data?
Hi,

I guess this is an old topic and I also tried to get an answer in
the ...xml group but to no avail - sorry for the repost. I have a
stored procedure "xmlproc" on "sqlserver_A", which returns xml via a
"select ... for xml auto" query. "sqlserver_B" is linked to
sqlserver_A (I tried to link as "SQL Server", "Native Client" and
"OLEDB") and calls

exec sqlserver_A.mydb.dbo.xmlproc

But instead of returning xml data, I receive a binary string that
looks like

0x440C690064005F0050006F0072007...

Is there anyway around this?

TIA, Regards
DC

On Saturday, January 12, 2008 9:29 AM
novalidaddres wrote:

?Have you tried to cast the binary data to an XML type?
?Have you tried to cast the binary data to an XML type?

--

Rub?n Garrig?s
Solid Quality Mentors

On Saturday, January 12, 2008 10:48 AM
Erland Sommarskog wrote:

Re: exec linkedserver.dbo.forxml_proc: why do I receive "binary" data?
DC (dc@upsize.de) writes:

First why this happens: when FOR XML data is returned through OLE DB,
it is transmitted in a binary format which is more compact than the
text version. The client is assumed to be able to unpack this, and
indeed you can do this in an OLE DB client. However, the application
needs to know that FOR XML data is coming and use different API calls.
In this situation, the client is the calling SQL Server. But there is
no way to tell SQL Server "Hey, I will call a remote SP that emits FOR
XML, so do it right, will you?". (A similar problem exists with SQLCMD:
in SQLCMD if you use FOR XML you will just get a binary back. However,
SQLCMD has a mode switch to deal with the situation.)

When researching alternatives, I found that this was a little more
difficult that I thought. The first attempt was to convert the returned
binary data to xml as suggested by Rub?n. No, it is not that simple.
The next idea was to rewrite the procedure to have an output parameter
of the XML datatype. Alas, it is not supported to have output parameter
of LOB types (xml, varchar(MAX) etc) in remote procedure calls.
Eventually I arrived at this solution:

create procedure nisse as
declare @x varbinary(MAX)
set @x = convert(varbinary(MAX),
(select * from sys.objects for xml auto, type))
select @x

That is, add ", type" to the FOR XML clause, convert this to varbinary(MAX)
and return this. On the calling side:

create table #hh(a varbinary(MAX) NOT NULL)
insert #hh(a)
exec NOV07.tempdb.dbo.nisse
select convert(xml, a) from #hh

Use INSERT-EXEC to capture it and convert back to XML. Why the road over
varbinary(MAX)? Because XML is neither supported as result column in
remote queries.

The problem with this solution is that it requires you to rewrite the
procedure you call. If you cannot do this, I think your best option
is to write a CLR stored procedure that connects to the remote server,
possibly using the XmlReader to get the data. (It's not something I've
done myself, so I cannot offer any samples.)


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

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

On Tuesday, January 15, 2008 10:51 PM
DC wrote:

Re: exec linkedserver.dbo.forxml_proc: why do I receive "binary" data?
On 12 Jan., 16:48, Erland Sommarskog wrote:
)
)
chnol/sql/2005/downloads/books...
reviousversions/books.mspx- Zitierten Text ausblenden -

Thanks a ton, Erland! You did all the research that I had to do
otherwise. Since it is much more complicated than I thought, it is
probably a better idea to request a recordset and do the "for xml" on
the target server.

Regards
DC


Submitted via EggHeadCafe - Software Developer Portal of Choice
Join Lists with LINQ - SharePoint 2010
http://www.eggheadcafe.com/tutorials/aspnet/b0c1cd0d-fe82-444e-a16e-7d...7d38eca
 >> Stay informed about: exec linkedserver.dbo.forxml_proc: why do I receive ""binar.. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
exec of SP vs. exec of lines -

Return value from EXEC - Hi, Is it possible to return a value when using EXE on a sql string? For example: DECLARE @SQL VARCHAR(1000) DECLARE @ROWS INT SET @SQL = 'SELECT COUNT(*) FROM Table' EXEC @SQL, @ROWS OUTPUT This doesn't work, but shows my idea. Can anyone...

How to exec AUTO_UPDATE_STATISTICS_ASYNC? - I'm trying to run this in 2005 alter database mydb AUTO_UPDATE_STATISTICS_ASYNC ON but get this error Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword ON. What am I doing wrong?

Exec across instances? - I know how to use EXEC across servers: EXEC myserver.mydatabase.dbo.myproc But if I am on instance myserver AA and I need to execute something on instance myserver BB? EXEC myserver\BB.mydatabase.dbo.myproc doesn't pass syntax Anyone!

bug in exec sp_help_jobschedule? - When I create a job schedule and run this in QA: exec sp_help_jobschedule @job_id = '{36E92706-9701-45DF-B5FA-8162C5D3B36F}', @include_description = 1 I get a different description than if I look at the description in EM. Here's what I'm getting in QA...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]