TSQL 存储过程和 XQuery?

发布于 2024-09-24 19:40:33 字数 1326 浏览 0 评论 0原文

我想编写一个存储过程,在输入要查找的特定字符串模式后查询 XML 文件。

我已经陷入了输入参数的困境,请考虑以下 XML 文档。

<root>
  <container>
      <element>A</element>
      <option>1</option>
  </container>
  <container>
      <element>B</element>
      <option>-1</option>
  </container>
  <container>
      <element>C</element>
  </container>
</root>

我想要实现的是找到并将元素标签和选项标签组合在一起的特定模式输出到表中。

例如: EXEC search "A1,B-1,C" 是输入字符串并且为 true,然后需要将其放入表中。但“A、B、C”是错误的。

我不太擅长 TSQL,所以我不知道如何分割或安排搜索模式,以便我可以使用它来处理元素标签和选项标签并将它们放入变量等中。

编辑:我认为下面的代码朝着正确的方向发展,但我还有另一个大问题。 我需要用相应的表来分析模式的每个值。

我最好举个例子:输入是“A1,B-1,C”顺便说一句输入长度应该灵活。

在我现有的表中,我有 4 列,其中包含以下数据:

  ID| Value | Meaning | Info
  1 |   A   |   text  | text
  2 |   A-1 |   text  | text
  3 |   A1  |   text  | text
  4 |   B   |   text  | text
  5 |   B-1 |   text  | text

依此类推...

现在,我需要以某种方式检查每个输入字符串的值,并将输入字符串与“含义”和“信息”列一起输出到另一张桌子。

对于上面的示例,我必须找到“A1,B-1,C”的序列,然后将上表的相应文本(包括字符串)输出到新表。 所以它看起来像这样:

  | Value | Meaning | Info
  |   A1  |   text  | text
  |   B-1 |   text  | text
  |   C   |   text  | text

我不知道上表是否太复杂,或者过程中的 CASE / IF-ELSE 结构是否会更好。

有谁知道如何实现这一点?

I want to write a stored procedure that queries XML files after I have input a certain string pattern to look for.

I'm already stuck at the input parameters, consider the following XML-document.

<root>
  <container>
      <element>A</element>
      <option>1</option>
  </container>
  <container>
      <element>B</element>
      <option>-1</option>
  </container>
  <container>
      <element>C</element>
  </container>
</root>

What I want to achieve is find and output a certain pattern combining element-tag and option-tag to a table.

For example: EXEC search "A1,B-1,C" is the input string and would be true which then needs to be put in a table. But "A,B,C" would be false.

I'm not that great with TSQL, so I don't know how I could split or arrange the search pattern so that I could use this to work with both element-tag and option-tag and put them into variables or so.

EDIT: The code below I think is going in the right direction but I have another big issue.
I need to analyze each value of the pattern with a corresponding table.

I better give an example: Input is "A1,B-1,C" btw input length should be flexible.

In my existing table I have 4 columns with the following data:

  ID| Value | Meaning | Info
  1 |   A   |   text  | text
  2 |   A-1 |   text  | text
  3 |   A1  |   text  | text
  4 |   B   |   text  | text
  5 |   B-1 |   text  | text

and so on...

Now somehow I need to check each single input-string with the value and output the input-string with both "Meaning" and "Info" column to another table.

With the example above I would have to find the sequence of "A1,B-1,C" and then output the corresponding text (including the string) of the table above to a new table.
So that it could look like this:

  | Value | Meaning | Info
  |   A1  |   text  | text
  |   B-1 |   text  | text
  |   C   |   text  | text

I don't know if I'm making it too complicated with the above table or if a CASE / IF-ELSE structure in the procedure would work better.

Does anyone know how this can be achieved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹雪碎 2024-10-01 19:40:33

看起来您似乎使用了错误的工具来完成这项工作,但如果您面临这些非常严格的限制,那么您可以使用以下工具,例如:

drop table #xml
drop table #queryparams

declare @x xml
select @x = '<code><root><items><element>A</element><option>1</option></items><items><element>B</element><option>-1</option></items><items><element>C</element></items></root></code>'

create table #xml (element varchar(60), [option] int null)

insert into #xml
select code.item.value('(element)[1]', 'varchar(60)'),
  code.item.value('(option)[1]', 'int')
from 
  @x.nodes('/code/root/items') AS code(item)

declare @queryParam varchar(60)
select @queryParam = 'A1,B-1,C'

create table #queryparams (element varchar(60), [option] int null)

insert into #queryparams
select left(data,1), RIGHT(data, len(data)-1) 
from dbo.split(@queryParam,',')

if not exists (
 select *
 from #xml x
 left join #queryparams q
  on x.element = q.element
 where q.[option] is null
)
select 1
else
select 0

ma​​rc_s 建议您更好在公共标记内使用 标记。在本例中,我选择了名称不佳的

这个解决方案非常糟糕,足以表明该方法在某种程度上不太正确。

It looks as if you're using the wrong tools for the job but if these are pretty firm constraints you're working against then you could use the following, for example:

drop table #xml
drop table #queryparams

declare @x xml
select @x = '<code><root><items><element>A</element><option>1</option></items><items><element>B</element><option>-1</option></items><items><element>C</element></items></root></code>'

create table #xml (element varchar(60), [option] int null)

insert into #xml
select code.item.value('(element)[1]', 'varchar(60)'),
  code.item.value('(option)[1]', 'int')
from 
  @x.nodes('/code/root/items') AS code(item)

declare @queryParam varchar(60)
select @queryParam = 'A1,B-1,C'

create table #queryparams (element varchar(60), [option] int null)

insert into #queryparams
select left(data,1), RIGHT(data, len(data)-1) 
from dbo.split(@queryParam,',')

if not exists (
 select *
 from #xml x
 left join #queryparams q
  on x.element = q.element
 where q.[option] is null
)
select 1
else
select 0

As marc_s suggests you're better off with <element> and <option> tags inside a common tag. I've opted for the poorly named <items> in this case.

This solution is nasty enough to suggest the approach isn't quite right somehow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文