Quantcast
Channel: BULK INSERT /BCP with UDT
Viewing all articles
Browse latest Browse all 8

BULK INSERT /BCP with UDT

$
0
0

Hi, pkv,

Sorry for the late reply. After looking at your data, I'm now sure your problem isn't same as mine.

Your task is to BULK INSERTXML data file into a relational table. Also called 'shredding'.

There are 3 ways to do this: (from the MCTS trainning kit book)

1, Use OPENXML and the XML stored procedures

2, Use the XML data type's node() method

3, Use the SQLXML API.

Here are my code for method number 2:

DECLARE

@X XML

 

SELECT @X = X.C

  FROM

OPENROWSET(BULK

  'E:\My Documents\SQL Server Management Studio\Projects\pkv_problem\organizace.xml',

  SINGLE_BLOB

)AS X(C)

 

INSERT

INTO dbo.organiz

  SELECT

C

.value('(./cislo_subjektu/text())[1]','int')AS'cislo_subjektu'

,

C.value('(./reference_subjektu/text())[1]','varchar(30)')AS'reference_subjektu'

,

C.value('(./nazev_subjektu/text())[1]','varchar(100)')AS'nazev_subjektu'

,

C.value('(./nazev_zkraceny/text())[1]','varchar(40)')AS'nazev_zkraceny'

,

C.value('(./ulice/text())[1]','char(40)')AS'ulice'

,

C.value('(./psc/text())[1]','char(15)')AS'psc'

,

C.value('(./misto/text())[1]','char(40)')AS'misto'

,

C.value('(./ico/text())[1]','char(15)')AS'ico'

,

C.value('(./dic/text())[1]','char(15)')AS'dic'

,

C.value('(./uverovy_limit/text())[1]','money')AS'uverovy_limit'

,

C.value('(./stav_limitu/text())[1]','money')AS'stav_limitu'

FROM @X.nodes('/root/organizace') T(C)

As you can see, this solution has nothing to do with a format file here.

(The SELECT clause in the INSERT INTO statement can be replaced by this one:

SELECT

C

.value('*[1]','int')AS'cislo_subjektu'

,

C.value('*[2]','varchar(30)')AS'reference_subjektu'

,

C.value('*[3]','varchar(100)')AS'nazev_subjektu'

,

C.value('*[4]','varchar(40)')AS'nazev_zkraceny'

,

C.value('*[5]','char(40)')AS'ulice'

,

C.value('*Devil','char(15)')AS'psc'

,

C.value('*[7]','char(40)')AS'misto'

,

C.value('*Music','char(15)')AS'ico'

,

C.value('*[9]','char(15)')AS'dic'

,

C.value('*[10]','money')AS'uverovy_limit'

,

C.value('*[11]','money')AS'stav_limitu'

FROM @X.nodes('/root/organizace') T(C)

should give you same result.)

I guess you may have a misconception here, for bcp.exe or BULK INSERT, a format file can be in two formats: a text one or a XML one. But both only work for a flat data file. It seems you are binding together a XML data file with a XML format file, hoping to shred data from XML into relational, no, that won't work.

Regards,

dong


Viewing all articles
Browse latest Browse all 8

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>