Sql server 如何在SQL中查询XML字段
以下是SQL中“我的XML”字段的内容:Sql server 如何在SQL中查询XML字段,sql-server,xml,Sql Server,Xml,以下是SQL中“我的XML”字段的内容: - <TestingConfig xmlns="http://tempuri.org/TestingConfig.xsd"> `- <Config>` `<ConfigId>75</ConfigId>` `<PlaceId>2</PlaceId>` `<Identifier>05</Identifier>`
- <TestingConfig xmlns="http://tempuri.org/TestingConfig.xsd">
`- <Config>`
`<ConfigId>75</ConfigId>`
`<PlaceId>2</PlaceId>`
`<Identifier>05</Identifier>`
`<Name>TEST1</Name>`
`</Config>`
`- <Config>`
`<ConfigId>76</ConfigId>`
`<PlaceId>2</PlaceId>`
`<Identifier>06</Identifier>`
`<Name>TEST2</Name>`
`</Config>`
`</TestingConfig>`
我需要它在结果中不包含名称空间。我不熟悉XML查询。谢谢您的帮助。如果您使用的是SQL Server 2005或2008,类似的功能应该适合您
DECLARE @xml XML
SELECT @xml = '<TestingConfig>
<Config>
<ConfigId>75</ConfigId>
<PlaceId>2</PlaceId>
<Identifier>05</Identifier>
<Name>TEST1</Name>
</Config>
<Config>
<ConfigId>76</ConfigId>
<PlaceId>2</PlaceId>
<Identifier>06</Identifier>
<Name>TEST2</Name>
</Config>
</TestingConfig>'
SELECT node.ref.value( 'ConfigId[1]', 'int' ) AS [ConfigId],
node.ref.value( 'PlaceId[1]', 'int' ) AS [PlaceId],
node.ref.value( 'Identifier[1]', 'varchar(32)' ) AS [Ident],
node.ref.value( 'Name[1]', 'varchar(32)' ) AS [Name]
FROM @xml.nodes( '/TestingConfig/Config' ) AS node(ref)
这对我有用。谢谢大家的回复 使用XMLNAMESPACES作为CC 挑选 CC.Config.value'CC:ConfigId[1],'int'作为[ConfigId] CC.Config.value'CC:PlaceId[1],'int'作为[PlaceId] CC.Config.value'CC:Identifier[1],'char2'作为[Identifier] CC.Config.value'CC:Name[1],'varchar8'作为[Name] 从TestingConfig
交叉应用TestingConfig.ConfigField.nodes'//CC:Config'作为CCConfig您使用的是什么数据库引擎?XML文件是如何存储的?它是作为文件读取、存储在XML数据类型列中还是存储为text/varchar?对此不确定。当前,我的XML存储在SQL表中。它是否更像TestingConfig中的DECLARE@xml-xml-SET@xml=select-ConfigField,但当我这样做时,我没有得到任何结果。我在这方面很幸运,但它不是我想要的格式-你上面展示的看起来更符合它。除非声明名称空间,否则我似乎无法提取数据。有什么建议吗?谢谢对我有效的语句,但不是我需要的格式。选择TestingConfig.ConfigField.query'声明命名空间x=/x:TestingConfig/x:Config/x:ConfigId'作为ConfigId,TestingConfig.ConfigField.query'declare namespace x=/x:TestingConfig/x:Config/x:Name'作为TestingConfig GO中的名称
DECLARE @xml XML
SELECT @xml = '<TestingConfig>
<Config>
<ConfigId>75</ConfigId>
<PlaceId>2</PlaceId>
<Identifier>05</Identifier>
<Name>TEST1</Name>
</Config>
<Config>
<ConfigId>76</ConfigId>
<PlaceId>2</PlaceId>
<Identifier>06</Identifier>
<Name>TEST2</Name>
</Config>
</TestingConfig>'
SELECT node.ref.value( 'ConfigId[1]', 'int' ) AS [ConfigId],
node.ref.value( 'PlaceId[1]', 'int' ) AS [PlaceId],
node.ref.value( 'Identifier[1]', 'varchar(32)' ) AS [Ident],
node.ref.value( 'Name[1]', 'varchar(32)' ) AS [Name]
FROM @xml.nodes( '/TestingConfig/Config' ) AS node(ref)