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

推荐订阅源

S
SegmentFault 最新的问题
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
博客园_首页
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans
T
The Blog of Author Tim Ferriss
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
P
Proofpoint News Feed
博客园 - 司徒正美
有赞技术团队
有赞技术团队
Engineering at Meta
Engineering at Meta
Last Week in AI
Last Week in AI
MongoDB | Blog
MongoDB | Blog

博客园 - 恭喜发财

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:...
恭喜发财 · 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.