SqlServer表中XML类型字段查询操作
以前从未在SQLServer中使用过XML类型字段,偶尔遭遇,很是试验了几次方才通过。记录脚本以资纪念。
/****** Object: Table [dbo].[XMLTest] Script Date: 01/17/2012 16:15:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[XMLTest]( [ID] [int] IDENTITY(1,1) NOT NULL, [XmlContent] [xml] NOT NULL, CONSTRAINT [PK_XMLTest] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO DECLARE @XmlContent XML Set @XmlContent='<book id="0001"><title>九阳真经</title><author>张三丰</author><price>10</price></book>' --Select @XmlContent.query('/book/title') SELECT @XmlContent.value('(/book/title)[1]', 'nvarchar(max)') INSERT INTO XMLTest (XmlContent) VALUES (@xmlContent) Set @XmlContent='<book id="0001"><title>九阴真经</title><author>张四丰</author><price>10。5</price></book>' INSERT INTO XMLTest (XmlContent) VALUES (@xmlContent) GO SELECT * FROM XMLTest WHERE XmlContent.value('(/book/title)[1]', 'nvarchar(max)') LIKE '%真经%' GO