需要帮助在 Informix 上运行此 MS SQL Server 语句

发布于 2024-10-16 22:53:46 字数 351 浏览 5 评论 0原文

我需要帮助在 Informix(版本 11)上运行此 MS SQL Server UPDATE 语句:

update b  
set Colname = 'StringValue'
from Table1 b right join Table1 c 
on ((b.Col1 = c.Col1) and (b.Col2 = c.Col2)) 
where ((b.Col3 = 'S' and b.Col4 <> 'S') and (c.Col3 = 'Z' and c.Col4 <> 'S'))

我不断收到错误号 -201(语法错误)。

你能看到任何语法错误吗?有什么想法吗?

I need help running this MS SQL Server UPDATE statement on Informix (version 11):

update b  
set Colname = 'StringValue'
from Table1 b right join Table1 c 
on ((b.Col1 = c.Col1) and (b.Col2 = c.Col2)) 
where ((b.Col3 = 'S' and b.Col4 <> 'S') and (c.Col3 = 'Z' and c.Col4 <> 'S'))

I keep getting error number -201 (syntax error).

Can you see any syntax error? Any ideas?

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

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

发布评论

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

评论(1

等风来 2024-10-23 22:53:46

您使用的是哪个版本的 Informix?

实际上,我认为这并不重要...IDS 不支持 UPDATE 语句,即使在最新版本中也是如此。所以,问题是您正在尝试使用 DBMS 不支持的符号,因此您会得到恼人的(但在本例中是准确的)通用“-201:发生语法错误”。我认为即使是最新的 GA 版本 IDS 11.70.xC1 也不支持 UPDATE 语句中的表别名(这使查询变得复杂)。

我承认 RIGHT {self} JOIN 让我感到困惑 - 我不确定我是否理解它应该如何工作。然而,这里是对所请求的更新的一个适度的近似:

UPDATE Table1
   SET Colname = 'StringValue'
 WHERE Table1.Col3 = 'S'
   AND Table1.Col4 <> 'S'
   AND EXISTS(SELECT * FROM Table1 AS C
               WHERE C.Col1 = Table1.Col1 AND C.Col2 = Table1.Col2
                 AND C.Col3 = 'Z'
                 AND C.Col4 <> 'S'
             )

令人烦恼的疑问有两个方面:

  • IDS 能​​否正确消除 EXISTS 子查询中对 Table1 的引用的歧义?
  • RIGHT JOIN 是什么意思?

不幸的是,当查询运行时,我得到了答案:

SQL -360: Cannot modify table or view used in subquery.

有一些解决方法,使用临时表,但它们很麻烦。然而,这个示例代码似乎按照我的预期工作(鉴于我仍然无法理解 RIGHT JOIN 在原始代码中所做的事情)。

CREATE TABLE table1
(
    col1    INTEGER NOT NULL,
    col2    INTEGER NOT NULL,
    col3    CHAR(1) NOT NULL,
    col4    CHAR(1) NOT NULL,
    colname VARCHAR(32) NOT NULL
);

-- The first row shown is updated - the others are unchanged
INSERT INTO table1 VALUES(1, 1, 'S', 'A', 'Old value'); 
INSERT INTO table1 VALUES(1, 1, 'Z', 'A', 'Old value'); 
INSERT INTO table1 VALUES(1, 2, 'S', 'A', 'Old value'); 
INSERT INTO table1 VALUES(1, 2, 'Z', 'S', 'Old value'); 
INSERT INTO table1 VALUES(1, 3, 'S', 'S', 'Old value'); 
INSERT INTO table1 VALUES(1, 3, 'Z', 'S', 'Old value'); 
INSERT INTO table1 VALUES(1, 4, 'S', 'S', 'Old value'); 
INSERT INTO table1 VALUES(1, 4, 'Z', 'A', 'Old value');     

SELECT * FROM Table1 WHERE Col3 = 'Z' AND Col4 <> 'S' INTO TEMP C;

UPDATE Table1
   SET Colname = 'StringValue'
 WHERE Table1.Col3 = 'S'
   AND Table1.Col4 <> 'S'
   AND EXISTS(SELECT * FROM {Table1 AS} C
               WHERE C.Col1 = Table1.Col1 AND C.Col2 = Table1.Col2
                 AND C.Col3 = 'Z'
                 AND C.Col4 <> 'S'
             );

片段“{Table1 AS}”是 Informix 中的注释。由于临时表 C 的创建方式,Col3 和 Col4 上的条件并不是严格必需的。

我在 UPDATE 语句之前和之后从 SELECT * FROM Table1 ORDER BY Col1, Col2, Col3, Col4 得到的结果是:

Before
1   1   S   A   Old value
1   1   Z   A   Old value
1   2   S   A   Old value
1   2   Z   S   Old value
1   3   S   S   Old value
1   3   Z   S   Old value
1   4   S   S   Old value
1   4   Z   A   Old value

After
1   1   S   A   StringValue
1   1   Z   A   Old value
1   2   S   A   Old value
1   2   Z   S   Old value
1   3   S   S   Old value
1   3   Z   S   Old value
1   4   S   S   Old value
1   4   Z   A   Old value

Which version of Informix are you using?

Actually, I don't think it matters...IDS does not support join notations in the UPDATE statement, even in the latest version. So, the problem is you are trying to use a notation that is not supported by the DBMS, and hence you get back the annoying (but, in this case, accurate) generic "-201: A syntax error has occurred". I don't think even IDS 11.70.xC1, the latest GA version, supports table aliases in the UPDATE statement, either (which complicates the query).

I confess that the RIGHT {self} JOIN has me bemused - I'm not sure I understand how it should work. However, here is a moderate approximation to the requested update:

UPDATE Table1
   SET Colname = 'StringValue'
 WHERE Table1.Col3 = 'S'
   AND Table1.Col4 <> 'S'
   AND EXISTS(SELECT * FROM Table1 AS C
               WHERE C.Col1 = Table1.Col1 AND C.Col2 = Table1.Col2
                 AND C.Col3 = 'Z'
                 AND C.Col4 <> 'S'
             )

The nagging doubts are two-fold:

  • Will IDS disambiguate the references to Table1 in the EXISTS sub-query correctly?
  • What does that RIGHT JOIN mean?

Unfortunately, when the query is run, I get back:

SQL -360: Cannot modify table or view used in subquery.

There are workarounds for that, using temporary tables, but they're a nuisance. However, this example code seems to work according to my expectations (given that I still can't wrap my brain around what the RIGHT JOIN is doing in the original).

CREATE TABLE table1
(
    col1    INTEGER NOT NULL,
    col2    INTEGER NOT NULL,
    col3    CHAR(1) NOT NULL,
    col4    CHAR(1) NOT NULL,
    colname VARCHAR(32) NOT NULL
);

-- The first row shown is updated - the others are unchanged
INSERT INTO table1 VALUES(1, 1, 'S', 'A', 'Old value'); 
INSERT INTO table1 VALUES(1, 1, 'Z', 'A', 'Old value'); 
INSERT INTO table1 VALUES(1, 2, 'S', 'A', 'Old value'); 
INSERT INTO table1 VALUES(1, 2, 'Z', 'S', 'Old value'); 
INSERT INTO table1 VALUES(1, 3, 'S', 'S', 'Old value'); 
INSERT INTO table1 VALUES(1, 3, 'Z', 'S', 'Old value'); 
INSERT INTO table1 VALUES(1, 4, 'S', 'S', 'Old value'); 
INSERT INTO table1 VALUES(1, 4, 'Z', 'A', 'Old value');     

SELECT * FROM Table1 WHERE Col3 = 'Z' AND Col4 <> 'S' INTO TEMP C;

UPDATE Table1
   SET Colname = 'StringValue'
 WHERE Table1.Col3 = 'S'
   AND Table1.Col4 <> 'S'
   AND EXISTS(SELECT * FROM {Table1 AS} C
               WHERE C.Col1 = Table1.Col1 AND C.Col2 = Table1.Col2
                 AND C.Col3 = 'Z'
                 AND C.Col4 <> 'S'
             );

The fragment '{Table1 AS}' is a comment in Informix. The conditions on Col3 and Col4 are not strictly necessary because of the way that temp table C is created.

The results I get from SELECT * FROM Table1 ORDER BY Col1, Col2, Col3, Col4 before and after the UPDATE statement are:

Before
1   1   S   A   Old value
1   1   Z   A   Old value
1   2   S   A   Old value
1   2   Z   S   Old value
1   3   S   S   Old value
1   3   Z   S   Old value
1   4   S   S   Old value
1   4   Z   A   Old value

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