惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Cisco Blogs
Schneier on Security
Schneier on Security
T
Tor Project blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tenable Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
Security Latest
Security Latest
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
Scott Helme
Scott Helme
Webroot Blog
Webroot Blog
Project Zero
Project Zero
Google Online Security Blog
Google Online Security Blog
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
Hacker News: Ask HN
Hacker News: Ask HN
PCI Perspectives
PCI Perspectives
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
W
WeLiveSecurity
Attack and Defense Labs
Attack and Defense Labs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
N
News | PayPal Newsroom
Help Net Security
Help Net Security
The Hacker News
The Hacker News
H
Heimdal Security Blog
O
OpenAI News
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
Simon Willison's Weblog
Simon Willison's Weblog
G
GRAHAM CLULEY
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 叶小钗
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
I
Intezer
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Security Affairs
P
Proofpoint News Feed
S
Secure Thoughts
腾讯CDC
Google DeepMind News
Google DeepMind News
量子位
罗磊的独立博客

博客园 - 蒜头

Mailbox unavailable. The server response was: 5.7.1 Unable to relay for (email address). 再谈Images到xps,pdf的转换 部分Office 2007文件格式转换为xps和pdf代码整理 Image.FromFile gives "Out of Memory" Exception for icon - 蒜头 简单介绍PDF,XPS,Images,Office 2007之间的转换方法 - 蒜头 - 博客园 SQL 2005 全文检索(续) 简单实现C#生成Excel 2007文件并下载 简单应用ReportViewer控件 配置SQL Server Session方法 采用负载均衡,部署了两个ASP.NET 2.0的站点服务器碰到的问题 初试VSTS 2008(TFS安装) Windows Server 2003分区修改方法[转载] 制作VSTO 2005 SE开发的Office 2007 AddIn的安装包 一个简单的document library event handler VS 2005 SP1 安装错误 [续] 一个简单的Checkbox Custom Field Type ASP.NET 2.0 SQL Cache 配置方法 解读Document Library关于权限的对象模型 SharePoint应用AJAX.NET和AJAX Control Toolkit
SQL Server 2005中xml类型和函数的简单应用
蒜头 · 2008-04-25 · via 博客园 - 蒜头

大家都知道SQL Server 2005新增了xml字段类型,我们可以利用它来实现批量操作数据库的需要,减少应用程序频繁、反复的建立数据库连接的情况发生,比如批量删除,我们可以在应用程序中构建如下xml:

<Delete><ID>1</ID><ID>2</ID><ID>3</ID></Delete>

在数据库中可以通过下面的脚本获得这些ID:

Select 
        T.ID.value(
'.''int'As ID
From
        
@xmlParam.nodes('/Delete/ID'as T(ID)

运行结果如下:

将xml类型作为存储过程的参数,批量删除的存储过程如下:

Create Procedure pro_Batch_Delete
@xmlParam xml
As

Delete from 
    t_TableName 
Where ID in 
    (
Select 
        T.ID.value(
'.''int'As ID
    
From
        
@xmlParam.nodes('/Delete/ID'as T(ID))

大家也知道,DataSet中的数据可以直接序列化成xml,通过dataSet.GetXml(),我们可以将这个xml作为存储过程参数,实现稍微复杂的功能,比如批量插入数据,在数据库中怎样将这个xml转换为表呢,我们先看DataSet序列化的xml结构:

<DataSet>
  
<Table><ID>1</ID><Count>1</Count></Table>
  
<Table><ID>2</ID><Count>2</Count></Table>
  
<Table><ID>3</ID><Count>3</Count></Table>
</DataSet>

当然也可以包含多个表,这里我们以一个表举例,转换脚本如下:

Declare @Xml xml
Set @Xml = '
<DataSet>
  <Table><ID>1</ID><Count>1</Count></Table>
  <Table><ID>2</ID><Count>2</Count></Table>
  <Table><ID>3</ID><Count>3</Count></Table>
</DataSet>
'
Select 
    T2.ID.value(
'.''int'As ID, T3.[Count].value('.','int'As [Count]
From 
    (
Select  
        T.Records.query(
'ID'As ID, 
        T.Records.query(
'Count'As [Count]
    
From 
        
@Xml.nodes('/DataSet/Table'As T(Records)
    ) 
As T1
Cross Apply T1.ID.nodes('ID'As T2(ID)
Cross Apply T1.[Count].nodes('Count'As T3([Count])

执行此脚本,可以得到我们想要的表:

批量插入记录的存储过程如下:

Create Procedure pro_Batch_Insert
@xmlParam xml
As

Insert Into t_TableName (ID, [Count])
(
    
Select 
        T2.ID.value(
'.''int'As ID, T3.[Count].value('.','int'As [Count]
    
From 
        (
Select  
            T.Records.query(
'ID'As ID, 
            T.Records.query(
'Count'As [Count]
        
From 
            
@xmlParam.nodes('/DataSet/Table'As T(Records)
        ) 
As T1
    
Cross Apply T1.ID.nodes('ID'As T2(ID)
    
Cross Apply T1.[Count].nodes('Count'As T3([Count])
)

同样,可以实现批量update的功能,这里不具体列举了,实现核Insert是基本相同的。
这里只使用部分SQL2005关于xml几个谓词和函数,在MSDN中都有说明,还有其他更强大的功能等大家一起去发掘.