下面的代码将返回select $f/field/text() from table1
如何在SQL Server 2005/2008中使用XQuery将xml转换为非xml文本?
declare @x xml = '<meta><field>Column1</field></meta>';
select @x.query('
for $f in /meta
return
"select $f/field/text() from table1"
')
我试图返回部分更改为"select"+$f/field
或"select" $f/field
这些不能被解析。
更新:
的@x
将从表产生具有for xml raw,elements
。使用xml/xquery的目的是为了避免样板字符串连接。看来XQuery不能将xml转换为文本?
更新问题:
以下是我想要写的代码。
declare @meta table (field sysname primary key, validate varchar(8000));
insert into @meta values ('Col1', '< 100'), ('Col2', '> Col1');
declare @x xml = (select * from @meta for xml path('meta'));
select @x;
-- The following code cannot be parsed. It will be nice to replace /meta/field/text() with shorter variable too
select @x.query('
"select 1"
{"," /meta/field/text() "= case when" /meta/field/text() /meta/validate/text() "then" /meta/field/text()}
"from table1"
"where 1=1 "
{ "or (" /meta/field/text() /meta/validate/text() ")" }
')
和产生的结果应该是,
select 1
, Col1 = case when Col1 < 100 then Col1 end
, Col2 = case when Col2 > Col1 then Col2 end
from table1
where 1=1
or (Col1 < 100)
or (Col2 > Col1)
$ f将包围。 –
ca9163d9
2012-03-11 11:32:21