Informix - 使用子选择的结果更新 SET 列

发布于 2024-10-27 02:32:44 字数 709 浏览 2 评论 0原文

我有一个带有集合列的表。我想做一个子选择,它返回几个整数并将结果放入该集合列中,但是我找不到通过 SQL 执行此操作的语法。我通过编写一个执行相同操作的 SQL 过程来做到这一点(将 SELECT 的结果放入 SET 变量并返回变量),但是我尝试在没有函数的情况下执行相同的操作。能做到吗?

首先,我创建一个临时表:

CREATE TEMP TABLE table1 (
  id INTEGER
, col2 SET(INT NOT NULL)
)

然后用测试数据填充它:

INSERT INTO table1 (id) VALUES (1);
INSERT INTO table1 (id) VALUES (2);

现在可以工作了:

UPDATE table1 SET col2 = SET{1,2};

...但是我试图这样做,但它不起作用:

UPDATE table1 SET col2 = (SELECT id FROM table1) WHERE id = 1;

它返回此错误:

[Error Code: -9632, SQL State: IX000]  Value does not match the type of column (col2).

I have a table with a collection column. I want to do a subselect which returns several integers and put the result in that collection column, however I can't find a syntax to do it through SQL. I did it by writing an SQL procedure which does the same thing (put results of SELECT in SET variable and return variable), however I'm trying to do the same without functions. Can it be done?

First, I create a temporary table:

CREATE TEMP TABLE table1 (
  id INTEGER
, col2 SET(INT NOT NULL)
)

Then I fill it with test data:

INSERT INTO table1 (id) VALUES (1);
INSERT INTO table1 (id) VALUES (2);

And now this works:

UPDATE table1 SET col2 = SET{1,2};

...but I'm trying to do this and it doesn't work:

UPDATE table1 SET col2 = (SELECT id FROM table1) WHERE id = 1;

It returns this error:

[Error Code: -9632, SQL State: IX000]  Value does not match the type of column (col2).

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

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

发布评论

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

评论(2

屌丝范 2024-11-03 02:32:44

在纯 SQL 中操作 SET 类型是一件痛苦的事情。

您的 UPDATE 尝试将一个 INTEGER 分配给 SET OF INTEGER,并且错误显示“您不能这样做”。

您应该能够这样做:

UPDATE table1
   SET col2 = SET { (SELECT id FROM table1 WHERE id = 1) }
 WHERE id = 1;

但是,我不确定正确的修改是什么才能将多个值放入集合中;内心的WHERE并不是闲着的。

Manipulating SET types in pure SQL is a pain.

Your UPDATE is trying to assign an INTEGER to a SET OF INTEGER, and the error says "you can't do that".

You should be able to do:

UPDATE table1
   SET col2 = SET { (SELECT id FROM table1 WHERE id = 1) }
 WHERE id = 1;

However, I'm not sure what the correct modification is to get more than one value into the set; the inner WHERE is not there idly.

山色无中 2024-11-03 02:32:44

您可以通过使用 MULTISETITEM 关键字
来实现此目的
以下示例将起作用:

CREATE TEMP TABLE table1 (
  id INTEGER
  ,col2 MULTISET(INT NOT NULL)
);

INSERT INTO table1 (id) VALUES (1);
INSERT INTO table1 (id) VALUES (2);

UPDATE table1 SET col2 = MULTISET{1,2};

UPDATE table1
   SET col2 = MULTISET(SELECT ITEM id FROM table1)
WHERE id = 1;

注意 SET 和 MULTISET 之间的差异

select set{1,2,1,3,1} from systables where tabid=1;

返回 SET{1,2,3}

select multiset{1,2,1,3,1} from systables where tabid=1;

返回 MULTISET{1,2,1,3,1}

You can achieve this by using MULTISET and ITEM keyword
the following example will work:

CREATE TEMP TABLE table1 (
  id INTEGER
  ,col2 MULTISET(INT NOT NULL)
);

INSERT INTO table1 (id) VALUES (1);
INSERT INTO table1 (id) VALUES (2);

UPDATE table1 SET col2 = MULTISET{1,2};

UPDATE table1
   SET col2 = MULTISET(SELECT ITEM id FROM table1)
WHERE id = 1;

Be aware of the differences between SET and MULTISET

select set{1,2,1,3,1} from systables where tabid=1;

returns SET{1,2,3}

select multiset{1,2,1,3,1} from systables where tabid=1;

returns MULTISET{1,2,1,3,1}

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