在SQL Server中使用XML数据

XML (eXtensible Markup Language) is one of the most common formats used to share information between different platforms. Owing to its simplicity and readability, it has become the de-facto standa...
摘要由CSDN通过智能技术生成

XML (eXtensible Markup Language) is one of the most common formats used to share information between different platforms. Owing to its simplicity and readability, it has become the de-facto standard for data sharing. In addition, XML is easily extendable.

XML(可扩展标记语言)是用于在不同平台之间共享信息的最常见格式之一。 由于其简单性和可读性,它已成为数据共享的实际标准。 另外,XML易于扩展。

In this article, we will see how we can work with XML in SQL Server. We will see how to convert tables in SQL into XML, how to load XML documents into SQL Server and how to create SQL tables from XML documents.

在本文中,我们将看到如何在SQL Server中使用XML。 我们将看到如何将SQL中的表转换为XML,如何将XML文档加载到SQL Server中以及如何从XML文档创建SQL表。

Let’s first generate some dummy data. We will use this data to create XML documents. Execute the following script:

让我们首先生成一些虚拟数据。 我们将使用此数据来创建XML文档。 执行以下脚本:

CREATE DATABASE Showroom
 
Use Showroom
CREATE TABLE Car  
(  
  CarId int identity(1,1) primary key,  
  Name varchar(100),  
  Make varchar(100),  
  Model int ,  
  Price int ,  
  Type varchar(20)  
)  
    
insert into Car( Name, Make,  Model , Price, Type)
VALUES ('Corrolla','Toyota',2015, 20000,'Sedan'),
('Civic','Honda',2018, 25000,'Sedan'),
('Passo','Toyota',2012, 18000,'Hatchback'),
('Land Cruiser','Toyota',2017, 40000,'SUV'),
('Corrolla','Toyota',2011, 17000,'Sedan'),
('Vitz','Toyota',2014, 15000,'Hatchback'),
('Accord','Honda',2018, 28000,'Sedan'),
('7500','BMW',2015, 50000,'Sedan'),
('Parado','Toyota',2011, 25000,'SUV'),
('C200','Mercedez',2010, 26000,'Sedan'),
('Corrolla','Toyota',2014, 19000,'Sedan'),
('Civic','Honda',2015, 20000,'Sedan')

In the script above, we created a Showroom database with one table Car. The Car table has five attributes: CarId, Name, Make, Model, Price, and Type. Next, we added 12 dummy records in the Car table.

在上面的脚本中,我们使用一个表Car创建了一个Showroom数据库。 Car表具有五个属性:CarId,名称,品牌,型号,价格和类型。 接下来,我们在Car表中添加了12条虚拟记录。

从SQL表转换为XML (Converting into XML from SQL tables)

The simplest way to convert data from SQL tables into XML format is to use the FOR XML AUTO and FOR XML PATH clauses.

将数据从SQL表转换为XML格式的最简单方法是使用FOR XML AUTO和FOR XML PATH子句。

SQL SERVER中的FOR XML AUTO (FOR XML AUTO in SQL SERVER)

The FOR XML AUTO clause converts each column in the SQL table into an attribute in the corresponding XML document.

FOR XML AUTO子句将SQL表中的每一列转换为相应XML文档中的属性。

Execute the following script:

执行以下脚本:

USE Showroom
SELECT * FROM Car
FOR XML AUTO

In the console output you will see the following:

在控制台输出中,您将看到以下内容:

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值