热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何从sql中的ntext(xml格式)列获取值-Howtogetvaluefromntext(inxmlformat)columninsql

IhaveacolumninmySQLdatabasethatiscalledTriggers_xml_dataanditstypeisntext.Thecolum

I have a column in my SQL database that is called Triggers_xml_data and its type is ntext. The column is in a xml format and I am trying to get a value from a certain part of the xml. I seen an example of this being done without a column like this:

我的SQL数据库中有一个列叫做Triggers_xml_data,它的类型是ntext。这个列是xml格式的,我正在尝试从xml的某个部分获取值。我看到过这样一个例子:

declare @fileContent xml
set @fileCOntent='
Mehrlein, Roswitha
SJM\MehrlR01
RMehrlein@SJM.com   
Roswita  Mehrlein, Beatrice Porta
SJM Germany
HR/Administration
Sales Representative     
x0lGQRQAAAABAAAAAAAAAAAeAQAyAAAAVgBAAAAA=
Sales'
 
;WITH XMLNAMESPACES ('http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-08-29T12-58-51' as my)
select @fileContent.value('(//my:PositionDepartment)[1]', 'varchar(255)')

But I want to select my column like this:

但我想这样选择我的专栏:

Declare @filevalue xml

select de.triggers_xml_data
from dbo.DEPLOYMENT_ENVIRONMENT as de

But this is not working and I tried to use this @filecontent.value('(//value)[1]','varchar(255)') and making it equal the column value, I have tried casting it but I can't find a way to do this. Is this possible?

但是这不起作用,我尝试使用这个@filecontent.value('(//value)[1]','varchar(255)')并使它等于列值,我试过对它进行强制转换,但是我找不到这样做的方法。这是可能的吗?

When I do this:

当我这样做:

SELECT 
CAST(
    REPLACE(CAST(de.TRIGGERS_XML_DATA AS VARCHAR(MAX)), 'encoding="utf-16"', '')
  AS XML).value('(triggers/triggerDefinition/config/item/value)[1]', 'NVARCHAR(max)') as Item, de.ENVIRONMENT_ID
from dbo.DEPLOYMENT_ENVIRONMENT as de
where de.ENVIRONMENT_ID = 19234819

I am getting a null value returned.

我得到的是一个空值。

Here is an example of what my xml could look like:

下面是我的xml的一个示例:





    true
    
      1
      After successful deployment
      
      true
      com.atlassian.bamboo.triggers.atlassian-bamboo-triggers:afterSuccessfulDeployment
      
      
        
          deployment.trigger.afterSuccessfulDeployment.triggeringEnvironmentId
          19234819
        
      
    
  
  true

1 个解决方案

#1


3  

The XML, as you posted it, is not valid. Your code example does not work... It is not allowed to use a namespace prefix without a namespace declaration. Furthermore your example misses the closing Header-tag...

正如您发布的那样,XML是无效的。您的代码示例不起作用……不允许使用没有名称空间声明的名称空间前缀。此外,您的示例忽略了结束标记……

I corrected this...

我纠正了这个……

DECLARE @yourTbl TABLE(ID INT, YourXML NTEXT);
INSERT INTO @yourTbl VALUES
(1,N'
Mehrlein, Roswitha
SJM\MehrlR01
RMehrlein@SJM.com   
Roswita  Mehrlein, Beatrice Porta
SJM Germany
HR/Administration
Sales Representative     
x0lGQRQAAAABAAAAAAAAAAAeAQAyAAAAVgBAAAAA=
Sales
');

--Lazy approach
SELECT ID
      ,CAST(CAST(YourXml AS NVARCHAR(MAX)) AS XML).value(N'(//*:PositionDepartment)[1]','nvarchar(max)')
FROM @yourTbl;


--explicit approach
WITH XMLNAMESPACES('DummyUrl' AS my)
SELECT ID
      ,CAST(CAST(YourXml AS NVARCHAR(MAX)) AS XML).value(N'(/my:Header/my:PositionDepartment)[1]','nvarchar(max)')
FROM @yourTbl

Some Background

If possible you should not store XML in other format than XML and further more one should avoid NTEXT, as it is depricated since SS2005!.

如果可能的话,您不应该以XML以外的格式存储XML,而且应该更多地避免使用NTEXT,因为自从SS2005以来,NTEXT就被剥夺了。

You have to cast NTEXT to NVARCHAR(MAX) first, than cast this to XML. The second will break, if the XML is not valid. That means: If the XML is really the way you posted it, this cannot work!

您必须首先将NTEXT转换为NVARCHAR(MAX),而不是将其转换为XML。如果XML无效,则第二个将中断。这意味着:如果XML真的是您发布的方式,那么它就不能工作!

UPDATE: String-based approach, if XML does not work

If you cannot cast this to XML you might try this

如果不能将其转换为XML,可以尝试使用这个

--String based
WITH Casted AS
(
    SELECT ID
          ,CAST(YourXML AS NVARCHAR(MAX)) AS TheXmlAsString
    FROM @yourTbl
)
,WithPosition AS
(
    SELECT Casted.*
          ,CHARINDEX(N'',TheXmlAsString) + LEN(N'') AS FirstLetter
    FROM Casted
)
SELECT ID 
      ,SUBSTRING(TheXmlAsString,FirstLetter,CHARINDEX('<',TheXmlAsString,FirstLetter)-FirstLetter)
FROM WithPosition

UPDATE 2

According to your edit the following returns a NULL value. This is good, because it shows, that the cast was successfull.

根据您的编辑,下面返回一个空值。这很好,因为它表明,演员阵容是成功的。

SELECT 
CAST(
    REPLACE(CAST(de.TRIGGERS_XML_DATA AS VARCHAR(MAX)), 'encoding="utf-16"', '')
  AS XML).value('(triggers/triggerDefinition/config/item/value)[1]',
'NVARCHAR(max)') as Item, de.ENVIRONMENT_ID
from dbo.DEPLOYMENT_ENVIRONMENT as de
where de.ENVIRONMENT_ID = 19234819

Try this (skip namespace with wildcard):

试试这个(用通配符跳过名称空间):

SELECT 
CAST(
    REPLACE(CAST(de.TRIGGERS_XML_DATA AS VARCHAR(MAX)), 'encoding="utf-16"', '')
  AS XML).value('(*:triggers/*:triggerDefinition/*:config/*:item/*:value)[1]', 'NVARCHAR(max)') as Item, de.ENVIRONMENT_ID
from dbo.DEPLOYMENT_ENVIRONMENT as de
where de.ENVIRONMENT_ID = 19234819

And this should be even better:

这应该更好:

SELECT 
CAST(CAST(de.TRIGGERS_XML_DATA AS NVARCHAR(MAX)) AS XML).value('(*:triggers/*:triggerDefinition/*:config/*:item/*:value)[1]', 'NVARCHAR(max)') as Item, de.ENVIRONMENT_ID
from dbo.DEPLOYMENT_ENVIRONMENT as de
where de.ENVIRONMENT_ID = 19234819

UPDATE 3

I'd rather cut away the full declaration. Your posted example would go like this

我宁愿删去全部的宣言。您发布的示例应该是这样的

DECLARE @DEPLOYMENT_ENVIRONMENT TABLE(ENVIRONMENT_ID INT, TRIGGERS_XML_DATA NTEXT);
INSERT INTO @DEPLOYMENT_ENVIRONMENT VALUES
(19234819,N'



    true
    
      1
      After successful deployment
      
      true
      com.atlassian.bamboo.triggers.atlassian-bamboo-triggers:afterSuccessfulDeployment
      
      
        
          deployment.trigger.afterSuccessfulDeployment.triggeringEnvironmentId
          19234819
        
      
    
  
  true
');

WITH Casted AS
(
    SELECT CAST(de.TRIGGERS_XML_DATA AS NVARCHAR(MAX)) AS XmlAsSting
    FROM @DEPLOYMENT_ENVIRONMENT as de
    where de.ENVIRONMENT_ID = 19234819
)
SELECT CAST(SUBSTRING(XmlAsSting,CHARINDEX('?>',XmlAsSting)+2,8000) AS XML).value('(/*:configuration/*:triggers/*:triggerDefinition/*:config/*:item/*:value)[1]', 'NVARCHAR(max)') as Item
FROM Casted;

推荐阅读
  • HTML:  将文件拖拽到此区域 ... [详细]
  • Maven + Spring + MyBatis + MySQL 环境搭建与实例解析
    本文详细介绍如何使用MySQL数据库进行环境搭建,包括创建数据库表并插入示例数据。随后,逐步指导如何配置Maven项目,整合Spring框架与MyBatis,实现高效的数据访问。 ... [详细]
  • 本文介绍了SIP(Session Initiation Protocol,会话发起协议)的基本概念、功能、消息格式及其实现机制。SIP是一种在IP网络上用于建立、管理和终止多媒体通信会话的应用层协议。 ... [详细]
  • 如何将955万数据表的17秒SQL查询优化至300毫秒
    本文详细介绍了通过优化SQL查询策略,成功将一张包含955万条记录的财务流水表的查询时间从17秒缩短至300毫秒的方法。文章不仅提供了具体的SQL优化技巧,还深入探讨了背后的数据库原理。 ... [详细]
  • 解决JavaScript中法语字符排序问题
    在开发一个使用JavaScript、HTML和CSS的Web应用时,遇到从SQLite数据库中提取的法语词汇排序不正确的问题,特别是带重音符号的字母未按预期排序。 ... [详细]
  • Web动态服务器Python基本实现
    Web动态服务器Python基本实现 ... [详细]
  • 本文探讨了如何通过Service Locator模式来简化和优化在B/S架构中的服务命名访问,特别是对于需要频繁访问的服务,如JNDI和XMLNS。该模式通过缓存机制减少了重复查找的成本,并提供了对多种服务的统一访问接口。 ... [详细]
  • Android与JUnit集成测试实践
    本文探讨了如何在Android项目中集成JUnit进行单元测试,并详细介绍了修改AndroidManifest.xml文件以支持测试的方法。 ... [详细]
  • 在处理大量联系人数据的批量插入操作时,发现现有方法的执行效率低下,尤其是在处理数十条记录以上时,与导出操作的速度形成鲜明对比。本文将探讨如何通过代码优化来提升批量插入联系人的效率。 ... [详细]
  • Bootstrap Paginator 分页插件详解与应用
    本文深入探讨了Bootstrap Paginator这款流行的JavaScript分页插件,提供了详细的使用指南和示例代码,旨在帮助开发者更好地理解和利用该工具进行高效的数据展示。 ... [详细]
  • Logging all MySQL queries into the Slow Log
    MySQLoptionallylogsslowqueriesintotheSlowQueryLog–orjustSlowLog,asfriendscallit.However,Thereareseveralreasonstologallqueries.Thislistisnotexhaustive:Belowyoucanfindthevariablestochange,astheyshouldbewritteninth ... [详细]
  • flea,frame,db,使用,之 ... [详细]
  • 本文介绍了如何在两个Oracle数据库(假设为数据库A和数据库B)之间设置DBLink,以便能够从数据库A中直接访问和操作数据库B中的数据。文章详细描述了创建DBLink前的必要准备步骤以及具体的创建方法。 ... [详细]
  • 本文详细探讨了在Web开发中常见的UTF-8编码问题及其解决方案,包括HTML页面、PHP脚本、MySQL数据库以及JavaScript和Flash应用中的乱码问题。 ... [详细]
  • 本文详细介绍了`android.os.Binder.getCallingPid()`方法的功能和应用场景,并提供了多个实际的代码示例。通过这些示例,开发者可以更好地理解如何在不同的开发场景中使用该方法。 ... [详细]
author-avatar
郭恭彦_522
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有