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

推荐订阅源

aimingoo的专栏
aimingoo的专栏
G
GRAHAM CLULEY
A
Arctic Wolf
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
Security Archives - TechRepublic
Security Archives - TechRepublic
小众软件
小众软件
S
Schneier on Security
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
Blog — PlanetScale
Blog — PlanetScale
Webroot Blog
Webroot Blog
美团技术团队
T
Tor Project blog
F
Full Disclosure
H
Hacker News: Front Page
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google Online Security Blog
Google Online Security Blog
Help Net Security
Help Net Security
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Troy Hunt's Blog
SecWiki News
SecWiki News
P
Proofpoint News Feed
Microsoft Security Blog
Microsoft Security Blog
PCI Perspectives
PCI Perspectives
The Hacker News
The Hacker News
M
MIT News - Artificial intelligence
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
Schneier on Security
Schneier on Security
博客园_首页
K
Kaspersky official blog
MongoDB | Blog
MongoDB | Blog
V
Vulnerabilities – Threatpost
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The GitHub Blog
The GitHub Blog
Scott Helme
Scott Helme
T
Tenable Blog
L
Lohrmann on Cybersecurity
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
U
Unit 42
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Hacker News: Ask HN
Hacker News: Ask HN
A
About on SuperTechFans
B
Blog
Last Week in AI
Last Week in AI

博客园 - 恭喜发财

android 电话状态的监听(来电和去电) (转)Xml DML - 恭喜发财 - 博客园 在WinForm中控制GIF动画的启停的一种方法(转) SQL Server Backup file in standard Zip format Working with binary large objects (BLOBs) c# 得到局域网中可用SqlServer服务器列表(转) C#实现UDP协议(转) 健康大讲堂—凡膳皆药 寓医于食 ReportView(RDSL)参考资料 How To Print Using Custom Page Sizes on Windows NT and Windows 2000(VB6) 汇总c#.net常用函数和方法集 在多线程中如何调用Winform 使用C#在进度条中显示复制文件的进度(转) c#将大文件读取或写入到数据库(带进度条的源码)(转) 写C#自定义控件的心得 Finalize(析构函数)、Dispose、Close 的区别与使用 如何在WinForm中对DataGrid进行分页显示(转) 如何用C#做一个类似于桌面插件的程序(转) c#连接各类数据库大全
(转)sql:variable() binding and modify method of xquery: insert XML DML
恭喜发财 · 2010-04-25 · via 博客园 - 恭喜发财

 

10月25日

sql:variable() binding and modify method of xquery: insert XML DML

/*

In SQL Server 2005, we can use sql:variable and sql:column function to bind relational data into XML by means of xquery. However, the binding has special syntax and it is easy to be confused.

1) insert ELEMENT:

The element text can be bind to an sql variable:

*/

USE AdventureWorks;

GO

DECLARE

@myDoc xml

SET

@myDoc = '<Root>

<ProductDescription ProductID="1" ProductName="Road Bike">

<Features>

</Features>

</ProductDescription>

</Root>'

DECLARE

@v nvarchar(50),@v1 nvarchar(50), @Maintenance nvarchar(50)

SET

@Maintenance=N'No Maintenance Required'

SET

@myDoc.modify('

insert <Maintenance>{"Refill Water As Needed"}</Maintenance> as last

into (/Root/ProductDescription/Features)[1]'

)

SET

@myDoc.modify('

insert <Maintenance>Refill Gas As Needed</Maintenance> as last

into (/Root/ProductDescription/Features)[1]'

)

SET

@myDoc.modify('

insert <Maintenance>{sql:variable("@Maintenance")}</Maintenance> as last

into (/Root/ProductDescription/Features)[1]'

)

SET

@Maintenance=N'Oil Required Every Month'

SET

@myDoc.modify('

insert <Maintenance>{sql:variable("@Maintenance")}</Maintenance> as last

into (/Root/ProductDescription/Features)[1]'

)

SELECT

@myDoc

/*

The syntax is to use {sql:variable("@sqlvariable")}. Note if a constant value is used, the '{"' and '"}' pair showed in the first sample can be igored, as showed in the second one, which is the most frequently used syntax for constant.

The '{"' must go together. You can not have '{' without double quotes. SQL Server may insert a blank element or fail. If the constant has spaces, it will fail.

e.g.: the following will insert a empty <Maintenance> element. However if you change {Refill} to {Refill Water}, it will fail.

*/

DECLARE

@myDoc xml

SET

@myDoc = '<Root>

<ProductDescription ProductID="1" ProductName="Road Bike">

<Features>

</Features>

</ProductDescription>

</Root>'

SET

@myDoc.modify('

insert <Maintenance>{Refill}</Maintenance> as last

into (/Root/ProductDescription/Features)[1]'

)

SELECT

@myDoc

GO

/*

2)  insert ATTRIBUTE

The syntax is {sql:variable("@sql_variable")}. If the value is a constant and it does not have spaces,

the double quotes pair can be ommited. The bracket '{' pair can never be ommited. If you change the attribute PartList {"P1 P2 P3"} to attribute PartList {P1 P2 P3}, it will fail

*/

DECLARE

@myDoc xml

SET

@myDoc = '<Root>

<ProductDescription ProductID="1" ProductName="Road Bike">

<Features>

</Features>

</ProductDescription>

</Root>'

DECLARE

@v nvarchar(50),@v1 nvarchar(50)

SELECT

@v='20',@v1='M001'

SET

@myDoc.modify('insert ( attribute PartList {"P1 P2 P3"},

attribute PriceSuggested {100000.00},

attribute LaborHours {sql:variable("@v")},

attribute Code {sql:variable("@v1")})

into (/Root/ProductDescription)[1]'

)

SELECT

@myDoc

To summary, always use {sql:variable("@sql_variable")} syntax in insert XML DML regardless for elements or attributes. For constant, type the constant directly as the text of the element and enclose the constant in {" "} pair for attributes.