[英]SQL- fetch value from XML parent node (separately)
在我下面的查詢中,它將所有重新編碼集作為XML返回到單個變量中,但是我需要將所有父節點值都放入單獨的while循環中,只需運行以下查詢即可:
---------聲明臨時表------------------------------------ -------------
IF OBJECT_ID('tempdb.dbo.##TestTable','U')IS NOT NULL DROP TABLE ##TestTable
CREATE TABLE ##TestTable(id int,Uname nvarchar(max),Uaddress nvarchar(max))
INSERT INTO ##TestTable values (1,'abc','NY')
INSERT INTO ##TestTable values (2,'def','WD')
INSERT INTO ##TestTable values (3,'','KL')
DECLARE @XML XML
DECLARE @WhereClause nvarchar(max)
DECLARE @CountVal int
SELECT @CountVal=count(*) from ##TestTable
SET @XML= (SELECT * FROM ##TestTable FOR XML PATH('ParentNode'), ELEMENTS XSINIL)
SELECT @XML
;with cte as
( select xr.value('fn:local-name(.)','nvarchar(max)') name, xr.value('.','nvarchar(max)') val from @xml.nodes('//.') xq(xr)
where xr.value('fn:local-name(.)','nvarchar(max)')<>''
)
SELECT @WhereClause= STUFF((select ' and ' + name + '='''+val+'''' from cte for xml path('')),1,4,'')
SELECT @WhereClause
WHILE (@CountVal>0)
BEGIN
SELECT @WhereClause
SET @CountVal=@CountVal-1
END
IF OBJECT_ID('tempdb.dbo.##TestTable','U')IS NOT NULL DROP TABLE ##TestTable
當前示例XML(以@XML格式):
<ParentNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><id>1</id><Uname>abc</Uname><Uaddress>NY</Uaddress></ParentNode><ParentNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><id>2</id><Uname>def</Uname><Uaddress>WD</Uaddress></ParentNode><ParentNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><id>3</id><Uname /><Uaddress>KL</Uaddress></ParentNode>
當前@WhereClause的輸出是(全部輸出到單個@WhereClause變量中):
ParentNode='1abcNY' and id='1' and Uname='abc' and Uaddress='NY' and ParentNode='2defWD' and id='2' and Uname='def' and Uaddress='WD' and ParentNode='3KL' and id='3' and Uname='' and Uaddress='KL'
但是我的預期輸出是:
Firstly(in @WhereClause): id='1' and Uname='abc' and Uaddress='NY'
Secondly(in @WhereClause):id='2' and Uname='def' and Uaddress='WD'
Thirdly(in @WhereClause):id='3' and Uname='' and Uaddress='KL'
。 。
我怎么得到的。 提前致謝。
嘗試這個:
declare @WhereClause nvarchar(max)
declare @CountVal int
select @CountVal=count(*)
from ##TestTable
while @CountVal>0
begin
select @WhereClause =
(
select ' and '+T.N.value('local-name(.)', 'nvarchar(max)')+'='+T.N.value('.', 'nvarchar(max)')
from (
select *
from ##TestTable
where id = @CountVal
for xml path(''), type
) as C(X)
cross apply C.X.nodes('/*') as T(N)
for xml path(''), type
).value('substring((./text())[1], 6)', 'nvarchar(max)')
select @WhereClause
set @CountVal=@CountVal-1
end
似乎遲到了並且被誤解了,那將是我的方法
DECLARE @XML XML
DECLARE @WhereClause nvarchar(max)
DECLARE @CountVal int
SELECT @CountVal=count(*) from ##TestTable
SET @XML= (SELECT * FROM ##TestTable FOR XML PATH('ParentNode'), ELEMENTS XSINIL)
SELECT @XML
;with cte as
( select xr.value('fn:local-name(.)','nvarchar(max)') name, xr.value('.','nvarchar(max)') val from @xml.nodes('//.') xq(xr)
where xr.value('fn:local-name(.)','nvarchar(max)')<>'' and xr.value('fn:local-name(.)','nvarchar(max)')<>'ParentNode'
)
SELECT @WhereClause= SubString((select Case when Name ='id' then CHAR(10) +''+ name + '='''+val+'''' else ' and ' + name + '='''+val+'''' end from cte for xml path('')),2,100000000)
Print @WhereClause
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.