基于SQL中的列值查询XML
我想根据SQL中的列值查询XML 我的XML如下基于SQL中的列值查询XML,sql,sql-server,xml,sql-server-2008,Sql,Sql Server,Xml,Sql Server 2008,我想根据SQL中的列值查询XML 我的XML如下 declare @xmlValue xml = '<Accounts> <Account> <ID>1</ID> <Name>Alex</Name> <Age>10</Age> </Account> <Account> <ID>2</ID> <Na
declare @xmlValue xml =
'<Accounts>
<Account>
<ID>1</ID>
<Name>Alex</Name>
<Age>10</Age>
</Account>
<Account>
<ID>2</ID>
<Name>Richard</Name>
<Age>12</Age>
</Account>
<Account>
<ID>3</ID>
<Name>Shane</Name>
<Age>15</Age>
</Account>
</Accounts>';
此外,如果我有一个变量,我可以像下面一样使用sql:variable
:
declare @ID int
set @ID = 1
select @xmlValue.query('/Accounts[1]/Account[./ID/text()=sql:variable("@ID")]');
现在,我有一个带有xml列的表#tmpTable
(名为xmlValue
)。
我想用join toAccount
表查询#tmpTable
,并从那里获取AccountId,有点像这样:
select @xmlValue.query('/Accounts[1]/Account[./ID/text()="2"]');
-- This does not work
select xmlValue.query('/Accounts[1]/Account[./ID/text()=a.AccountId]') as XMLResult
from #tmpTable tmp
inner join Accounts a on tmp.Id = a.Id
where a.Active = 1
select xmlValue.query('/Accounts[1]/Account[./ID/text()=sql:column("a.AccountId")]') as XMLResult
from #tmpTable tmp
inner join Accounts a on tmp.Id = a.Id
where a.Active = 1
我怎样才能做到这一点?任何帮助都将不胜感激。请像这样尝试:
select @xmlValue.query('/Accounts[1]/Account[./ID/text()="2"]');
-- This does not work
select xmlValue.query('/Accounts[1]/Account[./ID/text()=a.AccountId]') as XMLResult
from #tmpTable tmp
inner join Accounts a on tmp.Id = a.Id
where a.Active = 1
select xmlValue.query('/Accounts[1]/Account[./ID/text()=sql:column("a.AccountId")]') as XMLResult
from #tmpTable tmp
inner join Accounts a on tmp.Id = a.Id
where a.Active = 1
sql:column()
与sql:variable()
类似,是将列的值引入XQuery的适当方法