Sql server 使用T-SQL从XML检索值
有人能帮我使用T-SQL和XQuery从附加的XML文件中检索值吗 我正在尝试使用这个T-SQL,但运气不好Sql server 使用T-SQL从XML检索值,sql-server,xml,tsql,xquery,Sql Server,Xml,Tsql,Xquery,有人能帮我使用T-SQL和XQuery从附加的XML文件中检索值吗 我正在尝试使用这个T-SQL,但运气不好 declare @QuoteResponse XML select @QuoteResponse =gQuoteResponse FROM AutoRenew where policyid= '454544' SELECT b.value('(OverallResultStatus)[1]', 'varchar(100)') as status FROM @QuoteResponse
declare @QuoteResponse XML
select @QuoteResponse =gQuoteResponse FROM AutoRenew where policyid= '454544'
SELECT b.value('(OverallResultStatus)[1]', 'varchar(100)') as status
FROM
@QuoteResponse.nodes('GetQuoteResponse/OperationResult') as a(b)
这是我用来检索QuoteId
DECLARE @QuoteResponse XML
SELECT @QuoteResponse = gQuoteResponse FROM [dbo].[AutoRenew] WHERE policyid = '454544'
SELECT
@QuoteResponse.value('(GetQuoteResponse/QuoteResults/QuoteResult/QuoteId)[1]', 'bigint')
谢谢
<GetQuoteResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<OperationResult>
<OverallResultStatus>xxxx</OverallResultStatus>
<Results>
<Result>
<ResultStatus>xxxx</ResultStatus>
<ResultCode>ddf</ResultCode>
<ResultMessage>xxxxx</ResultMessage>
</Result>
</Results>
</OperationResult>
<QuoteResults>
<QuoteResult>
<OperationResult>
<OverallResultStatus>xxx</OverallResultStatus>
<Results>
<Result>
<ResultStatus>xx</ResultStatus>
<ResultCode>xxxx</ResultCode>
<ResultMessage>xx</ResultMessage>
</Result>
</Results>
</OperationResult>
<MainData>
<StartDate>2012-08-14T00:00:00</StartDate>
<ReturnDate>2013-08-13T00:00:00</ReturnDate>
<TripType>xxx</TripType>
<Area>xxx</Area>
<Relationship>Individual</Relationship>
<Product>
<ProductId>xxx</ProductId>
<Name>xxxx</Name>
</Product>
<Endorsements>
<Endorsement>
<EndorsementCode>xx</EndorsementCode>
<Selected>xx</Selected>
<Name>xxx</Name>
</Endorsement>
<Endorsement>
<EndorsementCode>xxx</EndorsementCode>
<Selected>xxx</Selected>
<Name>xxxx</Name>
</Endorsement>
</Endorsements>
<Promotion>
<PromotionCode />
</Promotion>
<Travellers>
<Traveller requestedMedicalScreening="false">
<TravellerId>xxxx</TravellerId>
<Title>xxx</Title>
<FirstName>xxx </FirstName>
<LastName>xxxx</LastName>
<Age>xxx</Age>
</Traveller>
</Travellers>
</MainData>
<Price>
<NetToParentAgent>xxx</NetToParentAgent>
<NetPrice>xxx</NetPrice>
<Tax>edew</Tax>
<GrossPriceWithAllSurchargesAndMedicalScreenings>103.02</GrossPriceWithAllSurchargesAndMedicalScreenings>
</Price>
<QuoteId>322423234</QuoteId>
<QuoteExpiryDate>xxx</QuoteExpiryDate>
<SummaryOfCover />
</QuoteResult>
</QuoteResults>
</GetQuoteResponse>
xxxx
xxxx
ddf
xxxxx
xxx
xx
xxxx
xx
2012-08-14T00:00:00
2013-08-13T00:00:00
xxx
xxx
个人
xxx
xxxx
xx
xx
xxx
xxx
xxx
xxxx
xxxx
xxx
xxx
xxxx
xxx
xxx
xxx
爱德
103.02
322423234
xxx
这个怎么样:
DECLARE @QuoteResponse XML
SELECT @QuoteResponse = gQuoteResponse FROM dbo.AutoRenew WHERE policyid = '454544'
SELECT
@QuoteResponse.value('(/GetQuoteResponse/QuoteResults/QuoteResult/QuoteId)[1]', 'bigint')
您只需遵循XML元素层次结构,直到读取所需的值:
<GetQuoteResponse -- this one is relevant ....
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<OperationResult> -- irrelvant - not in here.....
......
</OperationResult>
<QuoteResults> -- this one is relevant ....
<QuoteResult> -- this one is relevant ....
<OperationResult> -- irrelvant - not in here.....
....
</OperationResult>
<MainData> -- irrelvant - not in here.....
......
</MainData>
<Price> -- irrelvant - not in here.....
......
</Price>
<QuoteId>322423234</QuoteId> -- here's our value !
--无关-不在这里。。。。。
......
--这是相关的。。。。
--这是相关的。。。。
--无关-不在这里。。。。。
....
--无关-不在这里。。。。。
......
--无关-不在这里。。。。。
......
322423234——这是我们的价值!
您是否尝试过创建临时表,然后使用openxml填充临时表
DECLARE @QuoteResponse XML
SELECT @QuoteResponse =gQuoteResponse FROM AutoRenew where policyid= '454544'
DECLARE @idoc INT
EXEC sp_xml_preparedocument @idoc OUTPUT, @QuoteResponse
CREATE TABLE #QuoteIds(
[QuoteId] VARCHAR (10))
INSERT INTO #QuoteIds ([QuoteId])
SELECT QuoteId
FROM OPENXML(@idoc, '/QuoteResults/QuoteResult/QuoteId', 2) WITH #QuoteIds
DECLARE @quoteid VARCHAR (10)
SET @quoteid = (SELECT TOP 1 QuoteId FROM #QuoteIds)
还是不走运。它返回空值。@user740022:那么您的XML看起来一定不一样了-当我运行它时,这段代码运行得很好。你能再检查一下你的XML吗?是的,我检查过了。XML文件中没有任何更改。我使用的是同一个XML文件。但仍然返回空值。非常感谢你的帮助。谢谢。@user740022:-它精确地复制了您的表和XML-如果您运行它,您确实可以从
中获得正确的值。你的数据库里一定有不同的东西…谢谢Marc。是的,我的XML文件包含默认名称空间,这就是问题所在。现在它工作得很好。