知方可补不足~sqlserver中对xml类型字段的操作

回到目录

在sqlserver中有很多种数据类型,而XML数据类型是比较新奇怪的一种格式,我们平常接触的可能比较少,用的也少,而在某些场合,使用XML类型可能会使我们的开发变简单,下面就是一种情况:

表test有字段Menu,它保存的信息可能有两种方式,对标准menu,扩展menu,自定义Menu等,而这些方式所需要的键/值是不同的,有些同学会说使用Json串来存储,这当然是一种很不错的方式,但是数据库并没有这种类型,所以在查询,聚合时非常困难,这些我们想到了xml格式,它是SQLSERVER的是一种数据类型,支持查询,聚合等操作,下面我们来学习一下.

1 XML字段结构如下

<WxPaymentConfig>
<demo>zhang</demo>
<price></price>
</WxPaymentConfig>

2 查询数据,通过XML类型作为条件

SELECT *
FROM demo
WHERE Menu.value('(/WxPaymentConfig/demo)[1]', 'nvarchar(50)') = 'zhang'

3 查询数据同时对它进行统计,即执行某些聚合操作

SELECT  SUM(Menu.value('(/WxPaymentConfig/price)[1]', 'decimal'))
FROM demo
WHERE Menu.value('(/WxPaymentConfig/demo)[1]', 'nvarchar(50)') = 'zhang'

结果是我们所希望看到的

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARcAAABcCAIAAADLWWaYAAAGUUlEQVR4nO2dMY+bPhyG+UKtInnKlq9QKZ1v8ze4dM036FKl8pbhdNNNUdSZsTo8VFGkTEzZ0PWvY8vm/+CEI4B/OBiKIe+jV4gSLlg5nvxsA9fgv7/Jcrnc//mdLS2ZzWY37Q/AWAnyFs0AALdzZREAoAEfFiEI0iywCEFcA4sQxDWwCEFcU7RIDYrePz4E2RcsGtx5OZvNem8DgnxYNDiF9n9+L5fLvsuhFb1/UEinGbxFgyhHg2gk0jhWFnEhK8N4Mf+49ZUWhWEYhuHmmvX6qcdPWdfMHhuAdBpbi4RU5wjFDaEtCoKA+GezVFokhCx0qJIk4UL0+CkPpWYizdKwFpWrEOOSMSeLAjOm96QtStNUvin5pp5f3zmHRUhXsbIo/+0eRVG2HoahXjmdTkJIxrjxMBcTCEnytpjWCyEsO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no2eKrh9H8zRAVe8sujjehGhQe0sduWPG3t0uF4EnPHLIl2OTKWDmKMr70ZIlduIexdAC/hmkY+BRYCmxiLXhwANZCdlR+/fOrAIEPT2rKtSqutDtEvfvyngL5RFXfeUypd6fF72/ZsC/lJjUUdH1W++Gw5BEHT0UYARUG9RR4N1/R3ftx22oBYBgj4t6luNG0AtAgSoRVagFgGCZhZZXaxsXItW88ul0/nKZUuLoBYBglstevn2WZ+qLVhUWYu2i8lFg8vqdjGZLLbnLZPF1m5Lq6AWAQIfa1HeqMliu1vN8wVnvtpZbWkV1CJA0KdFdeOicy3KFxe9brOlXYtQiwCBv7Vou5gEhc5afxahFgECT2tRptDOD4tQiwCBj7Uor9BuZzcKwrgI9Id/tWg1L85VY44O+I13tejjsk8QBMG5JuF6EfAZ3LtgBWoRIMB9dFagFgEC1CIrUIsAQb1FrT55fQa1CIyJGoscnrCuAbUIjIY+nxjf9/0Q+E3Lvn9TwF/6+T/GARgTsAgAV2ARAK7AIgBcgUUAuAKLAHAFFgHgCiwCwBVYBIArsAgAV2ARAK7AIgBcgUUAuAKLAHAFFgHgCiwCwBVYBIArsAgAV2ARAK7AIgBcgUUAuAKLAHDlf97M3KAx7PdUAAAAAElFTkSuQmCC" alt="" />

回到目录

上一篇:php – 如何让IIS6识别pathinfo路由而不是返回“404 Undescribed”错误?


下一篇:cudaCheckError() failed : invalid device function解决方法