Upload multiple XML files into XML data type columns

SQL Server database administrators often have some needs such as importing multiple files into a table in the database. This article will discuss how to upload multiple files (especially XML files) into the XML data column of the SQL Server database.

Prerequisites

a. Make sure xp_cmdshell is allowed. If not, execute some of the commands below

use master
go
sp_configure 'show advanced options', 1
go
reconfigure with override
go
sp_configure 'xp_cmdshell', 1
go
reconfigure with override
go

b. Create a database and the table used for this import using the following commands. If not, you can use existing databases.

USE [master]
GO
/ ****** Object: Database [XMLTest]
Script Date: 04/17/2007 01:49:43 ****** /
IF EXISTS (SELECT name FROM sys.databases WHERE name = N'XMLTest ')
DROP DATABASE [XMLTest]
go
create database XMLTest
go
use XMLTest
go

c. Make sure you have created a table with an XML data type. Also, make sure that at least one column in the table can contain the value of the file name as follows

 use [XMLTest] / ****** object: table [dbo]. [myimage] script date: 09/10/2006 21:55:46 ****** / if exists (select * from sys.objects where object_id = object_id (N '[MYXML]') and type in (N'u ')) drop table [MYXML] go create table [MYXML] ([id] int identity (1,1), [XML File Name] varchar (100), [Data] XML) Go 

d. Suppose you want to upload the entire .bmp file from the C: XML directory (see Figures 1 and 2) into the 'MYXML' table in the 'dbo' schema on the database 'XMLTest'

Upload multiple XML files into XML data type columns Picture 1
Figure 1

Content example Customer5.xml

 2007-03-31T06: 40: 38.0000000-05: 00 Rainbow.River 1AE A-Accessible 761 Stopped 30 2007-03-31T06: 40: 38.0000000-05: 00 Rainbow.River 1AE Not-Accessible 870 Stopped 30 2007-03- 31T06: 40: 38.0000000-05: 00 Rainbow.River 1AE A-Accessible 97F Started 30 

e. Create a usp_uploadXMLFilesm procedure [use copy and paste the code below or download the existing usp_uploadXMLFiles file]. This creation is to create a stored procedure usp_uploadXMLfiles on the master database so it can execute and call any database.

USE [master]
GO
/ ****** Object: StoredProcedure [dbo]. [Usp_uploadXMLfiles] Script Date: 09/10/2006 23:33:34 ****** /
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID (N '[dbo]. [Usp_uploadXMLfiles]') AND type in (N'P ', N'PC'))
DROP PROCEDURE [dbo]. [Usp_uploadXMLfiles]
go
set quoted_identifier off
go
create procedure usp_uploadXMLfiles
@databasename varchar (128),
@schemaname varchar (128),
@tablename varchar (128),
@FileNameColumn varchar (128),
@xmlcolumn varchar (128),
@path varchar (500),
@filetype varchar (10),
@printorexec varchar (5) = 'print'
as
đặt nocount trên
declare @dircommand varchar (1500)
declare @insertquery varchar (2000)
declare @updatequery varchar (2000)
declare @count int
declare @maxcount int
declare @filename varchar (500)
set @ count = 1
set @dircommand = 'dir / b' + @ path + @ filetype
create #dir table (name varchar (1500))
insert #dir (name) exec master.xp_cmdshell @dircommand
delete from #dir where name is NULL
create table # dir2 (ident ident id (1,1), name varchar (1500))
chèn vào một tên danh sách dir2 từ #dir
--select * from # dir2
set @maxcount = ident_current ('# dir2')
print 'set quoted_identifier off'
print 'go'
while @count <= @ maxcount
begin
set @filename = (select name from # dir2 where id = @count)
set @insertquery = 'Insert into [' + @ databasename + ']. [' + @ schemaname + ']. [' + @ tablename + ']
(['+ @ filenamecolumn +']) values ​​("'+ @ filename +'") '
set @updatequery = 'update [' + @ databasename + ']. [' + @ schemaname + ']. [' + @ tablename + ']
set ['+ @ xmlcolumn +'] = (SELECT * FROM OPENROWSET (BULK "'+ @ path + @ filename +'", SINGLE_BLOB) AS x)
WHERE ['+ @ filenamecolumn +'] = "'+ @ filename +'" '
if @printorexec = 'print'
begin
print @insertquery
print @updatequery
end
if @printorexec = 'exec'
begin
set @ insertquery = 'set quoted_identifier off' + char (10) + char (13) + @ insertquery
set @ updatequery = 'set quoted_identifier off' + char (10) + char (13) + @ updatequery
exec (@insertquery)
exec (@updatequery)
end
set @count = @count +1
end
go

This procedure accepts the following parameters:

@databasename = The name of the database has an existing schema and table.

@schemaname = Reducing database of tables with existing tables

@tablename = Name of the table where the files will be uploaded

@FileNameColumn = The name of the column in the table with the file name to store

@XMLcolumn = Columns of existing XML data types with files will be stored as XML

@path = Path of all files needed to upload. Example 'C: Windows'

@filetype = Type of file to upload. For example '* .XML'

@printorexec = If 'Print' is set as a parameter, it will create and display commands. If 'Exec' is set as a parameter, it will immediately execute the command which means uploading all files.

f. Execute the procedure with the parameter printorexec = 'print' according to the code below

Exec master . [usp_uploadXMLfiles]
@databasename = 'XMLTest',
@schemaname = 'dbo',
@tablename = 'MYXML',
@FileNameColumn = 'XML File Name',
@XMLcolumn = 'Data',
@path = 'c: XML',
@filetype = '*. xml',
@printorexec = 'print'

This procedure will create all the necessary commands for creating one line for each file and update the line with the attribute file according to the following code.

set quoted_identifier off
go
Insert into [XMLTest]. [Dbo]. [MYXML] ([XML File Name]) values ​​("Customer1.xml")
update [XMLTest]. [dbo]. [MYXML] set [Data] = (SELECT *
FROM OPENROWSET (BULK "c: XMLCustomer1.xml", SINGLE_BLOB) AS x)
WHERE [XML File Name] = "Customer1.xml"
Insert into [XMLTest]. [Dbo]. [MYXML] ([XML File Name]) values ​​("Customer2.xml")
update [XMLTest]. [dbo]. [MYXML] set [Data] = (SELECT *
FROM OPENROWSET (BULK "c: XMLCustomer2.xml", SINGLE_BLOB) AS x)
WHERE [XML File Name] = "Customer2.xml"
Insert into [XMLTest]. [Dbo]. [MYXML] ([XML File Name]) values ​​("Customer3.xml")
update [XMLTest]. [dbo]. [MYXML] set [Data] = (SELECT *
FROM OPENROWSET (BULK "c: XMLCustomer3.xml", SINGLE_BLOB) AS x)
WHERE [XML File Name] = "Customer3.xml"
Insert into [XMLTest]. [Dbo]. [MYXML] ([XML File Name]) values ​​("Customer4.xml")
update [XMLTest]. [dbo]. [MYXML] set [Data] = (SELECT *
FROM OPENROWSET (BULK "c: XMLCustomer4.xml", SINGLE_BLOB) AS x)
WHERE [XML File Name] = "Customer4.xml"
Insert into [XMLTest]. [Dbo]. [MYXML] ([XML File Name]) values ​​("Customer5.xml")
update [XMLTest]. [dbo]. [MYXML] set [Data] = (SELECT *
FROM OPENROWSET (BULK "c: XMLCustomer5.xml", SINGLE_BLOB) AS x)
WHERE [XML File Name] = "Customer5.xml"
Insert into [XMLTest]. [Dbo]. [MYXML] ([XML File Name]) values ​​("Customer6.xml")
update [XMLTest]. [dbo]. [MYXML] set [Data] = (SELECT *
FROM OPENROWSET (BULK "c: XMLCustomer6.xml", SINGLE_BLOB) AS x)
WHERE [XML File Name] = "Customer6.xml"
Insert into [XMLTest]. [Dbo]. [MYXML] ([XML File Name]) values ​​("Customer7.xml")
update [XMLTest]. [dbo]. [MYXML] set [Data] = (SELECT *
FROM OPENROWSET (BULK "c: XMLCustomer7.xml", SINGLE_BLOB) AS x)
WHERE [XML File Name] = "Customer7.xml"

g. Execute the procedure with the printorexec = 'exec' parameter under the code below

Exec master . [usp_uploadXMLfiles]
@databasename = 'XMLTest',
@schemaname = 'dbo',
@tablename = 'MYXML',
@FileNameColumn = 'XML File Name',
@XMLcolumn = 'Data',
@path = 'c: XML',
@filetype = '*. xml',
@printorexec = 'exec'

The code will upload all xml files

hour. Now we will query the table using the SQL statement executed as below

use XMLTest
go
select * from myxml
go

This procedure will return the result as shown in Figure 2:

Upload multiple XML files into XML data type columns Picture 2
Figure 2

i. Click on the XML data connections to generate the results as shown below

 2007-03-3106: 40: 38.0000000-05: 00 Dancing.Doll 1AE A-Accessible 761 Stopped 30 2007-03-31T06: 40: 38.0000000-05: 00 1AE Not-Accessible Dancing.Doll 200770- 31T06: 40: 38.0000000-05: 00 Dancing.Doll 1AE A-Accessible 97F Started 30 
4.2 ★ | 10 Vote

May be interested

  • How to Add Columns in a Pivot TableHow to Add Columns in a Pivot Table
    today's tipsmake will show you how to create and insert new columns into a pivot table on microsoft excel using the pivot table tool. you can change existing rows, fields, or data into columns, or create columns of calculated data using custom formulas.
  • Lock one or more data columns on Excel worksheet - Freeze data in ExcelLock one or more data columns on Excel worksheet - Freeze data in Excel
    to lock one or more data columns in excel worksheet, you do as follows: open the sheet containing the data columns to be locked, then select all sheets by pressing ctrl + a. right-click and choose format cells ...
  • Upload files via FTP with FileZillaUpload files via FTP with FileZilla
    if your job is a web administrator or need to upload data via ftp, you will find the program handy. no installation required, optimal connection speed and absolutely free to help you manage files quickly
  • How to Compare Data in ExcelHow to Compare Data in Excel
    this wikihow teaches how to compare different sets of data in excel, from two columns in the same spreadsheet to two different excel files. highlight the first cell of a blank column. when comparing two columns in a worksheet, you'll be...
  • Quickly split data with Text to Columns in ExcelQuickly split data with Text to Columns in Excel
    there are many types of data that need to be filtered and separated, which may require a lot of complicated functions. fortunately in excel has integrated text to columns feature
  • Upload File in PHPUpload File in PHP
    a php script can be used with an html form that allows users to upload files to the server. first these files are uploaded to a temporary directory, then moved to a destination by a php script.
  • Tips for handling columns in Microsoft WordTips for handling columns in Microsoft Word
    the task of dividing the column of data text in word will help the reader to capture faster the content of the text. some of the following column handling tips will help us more easily handle word text columns.
  • how to merge pdf files, merge multiple PDF fileshow to merge pdf files, merge multiple PDF files
    merge pdf files or merge pdf files together to help you manage small pdf files more easily or save time when having to send multiple files to others. there are many ways to merge multiple pdf files together, in this article tipsmake
  • How to fix errors can not upload files to Google DriveHow to fix errors can not upload files to Google Drive
    the popularity of google drive is undisputed, but sometimes this leading cloud service also faces the problem of not being able to upload data. here is how to fix it when something goes wrong.
  • How to decompress files on your computer quicklyHow to decompress files on your computer quickly
    to send or share multiple files at the same time, users often compress them into a single file, making them lighter and faster to upload online. by using the winrar tool, you can compress or decompress files on your computer much easier.