在 Oracle SQL 语句中构建逗号分隔的值列表

发布于 2024-11-04 13:52:04 字数 379 浏览 3 评论 0原文

我正在尝试从 Oracle 中的字段构建一个以逗号分隔的值列表。

我找到了一些执行此操作的示例代码:

DECLARE @List VARCHAR(5000)
SELECT @List = COALESCE(@List + ', ' + Display, Display)
FROM TestTable
Order By Display

但是当我尝试这样做时,我总是收到有关 FROM 关键字不是预期的错误。我可以使用 SELECT INTO ,它可以工作,但如果我有多于一行,我会收到获取错误。

为什么我不能执行以下操作:

SELECT myVar = Field1
FROM myTable

I'm trying to build a comma-separated list of values out of a field in Oracle.

I find some sample code that does this:

DECLARE @List VARCHAR(5000)
SELECT @List = COALESCE(@List + ', ' + Display, Display)
FROM TestTable
Order By Display

But when I try that I always get an error about the FROM keyword not being were it was expected. I can use SELECT INTO and it works but if I have more than one row I get the fetch error.

Why can't I do as follows:

SELECT myVar = Field1
FROM myTable

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

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

发布评论

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

评论(3

挖鼻大婶 2024-11-11 13:52:04

在 Oracle 中,您可以使用由以下组织收集的众多字符串聚合技术之一蒂姆·霍尔在本页。

如果您使用的是 11.2,

SELECT LISTAGG(display, ',') WITHIN GROUP (ORDER BY display) AS employees
  INTO l_list
  FROM TestTable

在早期版本中,我的偏好是使用用户定义的聚合函数方法(Tim 的称为 string_agg)来执行

SELECT string_agg( display )
  INTO l_list
  FROM TestTable

In Oracle, you would use one of the many string aggregation techniques collected by Tim Hall on this page.

If you are using 11.2,

SELECT LISTAGG(display, ',') WITHIN GROUP (ORDER BY display) AS employees
  INTO l_list
  FROM TestTable

In earlier versions, my preference would be to use the user-defined aggregate function approach (Tim's is called string_agg) to do

SELECT string_agg( display )
  INTO l_list
  FROM TestTable
寄与心 2024-11-11 13:52:04

也许尝试 DBMS_UTILITY.COMMA_TO_TABLE 和 TABLE_TO_COMMA 来拆分/连接 csv:

DECLARE
  l_list1   VARCHAR2(50) := 'Tom,Dick,Harry,William';
  l_list2   VARCHAR2(50);
  l_tablen  BINARY_INTEGER;
  l_tab     DBMS_UTILITY.uncl_array;
BEGIN
  DBMS_OUTPUT.put_line('l_list1 : ' || l_list1);

  DBMS_UTILITY.comma_to_table (
     list   => l_list1,
     tablen => l_tablen,
     tab    => l_tab);

  FOR i IN 1 .. l_tablen LOOP
    DBMS_OUTPUT.put_line(i || ' : ' || l_tab(i));
  END LOOP;

  DBMS_UTILITY.table_to_comma (
     tab    => l_tab,
     tablen => l_tablen,
     list   => l_list2);

  DBMS_OUTPUT.put_line('l_list2 : ' || l_list2);
END;

Maybe try DBMS_UTILITY.COMMA_TO_TABLE and TABLE_TO_COMMA to split/join csv:

DECLARE
  l_list1   VARCHAR2(50) := 'Tom,Dick,Harry,William';
  l_list2   VARCHAR2(50);
  l_tablen  BINARY_INTEGER;
  l_tab     DBMS_UTILITY.uncl_array;
BEGIN
  DBMS_OUTPUT.put_line('l_list1 : ' || l_list1);

  DBMS_UTILITY.comma_to_table (
     list   => l_list1,
     tablen => l_tablen,
     tab    => l_tab);

  FOR i IN 1 .. l_tablen LOOP
    DBMS_OUTPUT.put_line(i || ' : ' || l_tab(i));
  END LOOP;

  DBMS_UTILITY.table_to_comma (
     tab    => l_tab,
     tablen => l_tablen,
     list   => l_list2);

  DBMS_OUTPUT.put_line('l_list2 : ' || l_list2);
END;
过去的过去 2024-11-11 13:52:04

不能将多个值插入到单个变量中,除非以某种方式将它们连接起来。

仅获取单个值(不确定 Oracle 语法),

select @myVar = select top 1 Field1 From myTable

否则,连接这些值(同样,不确定 Oracle)

set @myVar = ''  -- Get rid of NULL
select @myVar = @MyVar + ', ' +  Field1 From myTable

You cannot insert multiple values into a single variable, unless you concatenate them somehow.

To get only a single value (not sure of the oracle syntax),

select @myVar = select top 1 Field1 From myTable

Otherwise, to concatenate the values (again, not sure of Oracle)

set @myVar = ''  -- Get rid of NULL
select @myVar = @MyVar + ', ' +  Field1 From myTable
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文