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

insert binary data from xml

 
   Database Help (Home) -> XML RSS
Next:  InterForest Server 2003 domain migration of SQL 2..  
Author Message
Gambero

External


Since: Nov 13, 2008
Posts: 2



(Msg. 1) Posted: Thu Nov 13, 2008 5:29 pm
Post subject: insert binary data from xml
Archived from groups: microsoft>public>sqlserver>xml (more info?)

Hi, i have to "bulk" insert in my table some binary values (files) reading
from xml text..
i use this way:

create table test(MyKey varchar(32), FileData varbinary(MAX));
declare @xml xml;
set @xml =
'<?xml version="1.0" encoding="utf-8" ?>
<test>
<Row MyKey="1" FileData="BinaryDataInBase64FormatString" />
<Row MyKey="2" FileData="BinaryDataInBase64FormatString" />
<Row MyKey="3" FileData="BinaryDataInBase64FormatString" />
<Row MyKey="4" FileData="BinaryDataInBase64FormatString" />
<Row MyKey="5" FileData="BinaryDataInBase64FormatString" />
<Row MyKey="6" FileData="BinaryDataInBase64FormatString" />
<Row MyKey="7" FileData="BinaryDataInBase64FormatString" />
<Row MyKey="8" FileData="BinaryDataInBase64FormatString" />
<Row MyKey="9" FileData="BinaryDataInBase64FormatString" />
<Row MyKey="10" FileData="BinaryDataInBase64FormatString" />
</test>';

INSERT INTO test(MyKey,FileData)
SELECT
Row.value('@MyKey', 'varchar(32)') as MyKey,
Row.value('@FileData', 'varbinary(MAX)') as FileData
FROM @xml.nodes('/test/Row') AS myTable(Row);

this method work fine but in this way i load in memory all files data
(converted in base64).
I would like to use the file paths instead like:

declare @xml xml;
set @xml =
'<?xml version="1.0" encoding="utf-8" ?>
<test>
<Row MyKey="1" FileData="c:\File1.jpg" />
<Row MyKey="2" FileData="c:\File2.jpg" />
<Row MyKey="3" FileData="c:\File3.jpg" />
</test>';
INSERT INTO test(MyKey,FileData)
SELECT
Row.value('@MyKey', 'varchar(32)') as MyKey,
ConvertThisPathToVarBinary(Row.value('@FileData', 'varbinary(MAX)'))
as FileData
FROM @xml.nodes('/test/Row') AS myTable(Row);

there is a way?
Thanks

 >> Stay informed about: insert binary data from xml 
Back to top
Login to vote
Farmer

External


Since: Jul 27, 2007
Posts: 13



(Msg. 2) Posted: Tue Nov 25, 2008 7:47 pm
Post subject: Re: insert binary data from xml [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

How about this?

--INSERT INTO test(MyKey,FileData)
SELECT
Row.value('@MyKey', 'varchar(32)') as MyKey
cast(Row.value('@FileData', 'varchar(MAX)') as varbinary(max))
as FileData
FROM @xml.nodes('/test/Row') AS myTable(Row);



"Gambero" wrote in message

> Hi, i have to "bulk" insert in my table some binary values (files) reading
> from xml text..
> i use this way:
>
> create table test(MyKey varchar(32), FileData varbinary(MAX));
> declare @xml xml;
> set @xml =
> '<?xml version="1.0" encoding="utf-8" ?>
> <test>
> <Row MyKey="1" FileData="BinaryDataInBase64FormatString" />
> <Row MyKey="2" FileData="BinaryDataInBase64FormatString" />
> <Row MyKey="3" FileData="BinaryDataInBase64FormatString" />
> <Row MyKey="4" FileData="BinaryDataInBase64FormatString" />
> <Row MyKey="5" FileData="BinaryDataInBase64FormatString" />
> <Row MyKey="6" FileData="BinaryDataInBase64FormatString" />
> <Row MyKey="7" FileData="BinaryDataInBase64FormatString" />
> <Row MyKey="8" FileData="BinaryDataInBase64FormatString" />
> <Row MyKey="9" FileData="BinaryDataInBase64FormatString" />
> <Row MyKey="10" FileData="BinaryDataInBase64FormatString" />
> </test>';
>
> INSERT INTO test(MyKey,FileData)
> SELECT
> Row.value('@MyKey', 'varchar(32)') as MyKey,
> Row.value('@FileData', 'varbinary(MAX)') as FileData
> FROM @xml.nodes('/test/Row') AS myTable(Row);
>
> this method work fine but in this way i load in memory all files data
> (converted in base64).
> I would like to use the file paths instead like:
>
> declare @xml xml;
> set @xml =
> '<?xml version="1.0" encoding="utf-8" ?>
> <test>
> <Row MyKey="1" FileData="c:\File1.jpg" />
> <Row MyKey="2" FileData="c:\File2.jpg" />
> <Row MyKey="3" FileData="c:\File3.jpg" />
> </test>';
> INSERT INTO test(MyKey,FileData)
> SELECT
> Row.value('@MyKey', 'varchar(32)') as MyKey,
> ConvertThisPathToVarBinary(Row.value('@FileData',
> 'varbinary(MAX)')) as FileData
> FROM @xml.nodes('/test/Row') AS myTable(Row);
>
> there is a way?
> Thanks
>

 >> Stay informed about: insert binary data from xml 
Back to top
Login to vote
Gambero

External


Since: Nov 13, 2008
Posts: 2



(Msg. 3) Posted: Wed Nov 26, 2008 5:25 pm
Post subject: Re: insert binary data from xml [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> How about this?
>
> --INSERT INTO test(MyKey,FileData)
> SELECT
> Row.value('@MyKey', 'varchar(32)') as MyKey
> cast(Row.value('@FileData', 'varchar(MAX)') as varbinary(max))
> as FileData
> FROM @xml.nodes('/test/Row') AS myTable(Row);

sorry... i need to store in db the contents of the file.. like:

> --INSERT INTO test(MyKey,FileData)
> SELECT
> Row.value('@MyKey', 'varchar(32)') as MyKey
> FilePathToVarbinary(Row.value('@FileData', 'varchar(MAX)') as
> FileData
> FROM @xml.nodes('/test/Row') AS myTable(Row);


but inside a function is not possible to use dinamic sql to read the file
with openrowset...
any idea?
thanks
 >> Stay informed about: insert binary data from xml 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
How to write binary data in xml - I am using dom object to writing xml document, but I didn't enter binary data. I have tried using <base64> , <bin.hex> but didn't find the solution. If u have an idea, reply me its a highly apprietaiatable

"for xml" via rpc - why do I receive binary data? - Hi, I guess this is an old topic but I looked and could not find a clue: I have a stored procedure "xmlproc" on "sqlserver_A", which returns xml via a "select ... for xml auto" query. "sqlserver_B" is linked to sq...

Problem with bulk insert - Hi all, I am newbie in all the stuff about xml importing into sql server. What I try to do is simple. It is take an xml file and drop it into a table. I am using VS2005, SQLXML 4.0 and SQL Server 2000 (I think there is no problem of compatibility) Whe...

how to insert xml into sql 2005 - Using the following code how do I actually insert the data from the xml file into the sql 2005 database: declare @xml varchar(max) declare @xmlHandle int -- Bulk load the xml from the file set @xml = ( select * from openrowset (..

insert xml file into remote SQL Server 2005? - I'm stumped as to how to insert data (shredded) from an XML file into a table's columns. I tried using bcp but get "not trusted connection" error (its a remote server so not using Windows Authentication). I don't think I can use bulk load wi...
   Database Help (Home) -> XML 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 ]