Oracle 10g简单查询错误

发布于 2024-11-14 15:16:25 字数 854 浏览 1 评论 0原文

Oracle SQL Developer 抱怨下一个 SQL,尽管我似乎找不到原因:

IF to_number(to_char(sysdate, 'HH24')) > 6 THEN
  IF to_number(to_char(sysdate, 'HH24')) < 9 THEN
    SELECT 1 FROM dual;
  ELSE
    SELECT (CASE WHEN result = 'SUCCESS' THEN 1 ELSE 0 END) FROM t_job WHERE to_char(start_time, 'yyyy/mm/dd') = to_char(sysdate, 'yyyy/mm/dd');
  END IF;
ELSE
    SELECT (CASE WHEN result = 'SUCCESS' THEN 1 ELSE 0 END) FROM t_job WHERE to_char(start_time, 'yyyy/mm/dd') = to_char(sysdate, 'yyyy/mm/dd');
END IF;

提供的查询有什么错误?

错误报告:

Error starting at line 7 in command:
ELSE
Error report:
Unknown Command
(CASEWHENRESULT='SUCCESS'THEN1ELSE0END) 
--------------------------------------- 
1                                       


Error starting at line 9 in command:
END IF
Error report:
Unknown Command

Oracle SQL Developer complains about next SQL though I can't seem to find the reason:

IF to_number(to_char(sysdate, 'HH24')) > 6 THEN
  IF to_number(to_char(sysdate, 'HH24')) < 9 THEN
    SELECT 1 FROM dual;
  ELSE
    SELECT (CASE WHEN result = 'SUCCESS' THEN 1 ELSE 0 END) FROM t_job WHERE to_char(start_time, 'yyyy/mm/dd') = to_char(sysdate, 'yyyy/mm/dd');
  END IF;
ELSE
    SELECT (CASE WHEN result = 'SUCCESS' THEN 1 ELSE 0 END) FROM t_job WHERE to_char(start_time, 'yyyy/mm/dd') = to_char(sysdate, 'yyyy/mm/dd');
END IF;

What's the mistake in provided query?

Error report:

Error starting at line 7 in command:
ELSE
Error report:
Unknown Command
(CASEWHENRESULT='SUCCESS'THEN1ELSE0END) 
--------------------------------------- 
1                                       


Error starting at line 9 in command:
END IF
Error report:
Unknown Command

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

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

发布评论

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

评论(2

奢望 2024-11-21 15:16:25

您的代码(是 PL/SQL,而不仅仅是 SQL)存在一些问题:

1) 您缺少块周围的 beginend

2)您的 select 需要一个 into 子句

尝试:

DECLARE
  l_result number;
BEGIN
  IF to_number(to_char(sysdate, 'HH24')) > 6 THEN
    IF to_number(to_char(sysdate, 'HH24')) < 9 THEN
      SELECT 1 INTO l_result FROM dual;
    ELSE
      SELECT (CASE WHEN result = 'SUCCESS' THEN 1 ELSE 0 END) 
       INTO l_result 
       FROM t_job WHERE to_char(start_time, 'yyyy/mm/dd') = to_char(sysdate, 'yyyy/mm/dd');
    END IF;
  ELSE
      SELECT (CASE WHEN result = 'SUCCESS' THEN 1 ELSE 0 END) 
       INTO l_result 
       FROM t_job WHERE to_char(start_time, 'yyyy/mm/dd') = to_char(sysdate, 'yyyy/mm/dd');
  END IF;
  dbms_output.put_line('result is '||l_result);
END;

There are a couple of problems with your code (which is PL/SQL, not just SQL):

1) You are missing the begin and end around the block.

2) Your selects need an into clause

try:

DECLARE
  l_result number;
BEGIN
  IF to_number(to_char(sysdate, 'HH24')) > 6 THEN
    IF to_number(to_char(sysdate, 'HH24')) < 9 THEN
      SELECT 1 INTO l_result FROM dual;
    ELSE
      SELECT (CASE WHEN result = 'SUCCESS' THEN 1 ELSE 0 END) 
       INTO l_result 
       FROM t_job WHERE to_char(start_time, 'yyyy/mm/dd') = to_char(sysdate, 'yyyy/mm/dd');
    END IF;
  ELSE
      SELECT (CASE WHEN result = 'SUCCESS' THEN 1 ELSE 0 END) 
       INTO l_result 
       FROM t_job WHERE to_char(start_time, 'yyyy/mm/dd') = to_char(sysdate, 'yyyy/mm/dd');
  END IF;
  dbms_output.put_line('result is '||l_result);
END;
烧了回忆取暖 2024-11-21 15:16:25

由于这是一个 PL/SQL 块,因此您的 SELECT 语句需要将数据选择到某个局部变量中,或者需要在游标中使用它们。您想要哪种方法取决于 T_JOB 中有多少行可能符合您指定的条件。假设所有三个语句都返回 1 行,您可以执行类似的操作(简化代码以避免重复相同的查询两次)

DECLARE
  l_some_local_variable PLS_INTEGER;
BEGIN
  IF( to_number( to_char( sysdate, 'HH24' ) ) > 6 and
      to_number( to_char( sysdate, 'HH24' ) ) < 9 )
  THEN
    SELECT 1 
      INTO l_some_local_variable 
      FROM dual;
  ELSE
    SELECT (CASE WHEN result = 'SUCCESS' 
                 THEN 1
                 ELSE 0
             END) 
      INTO l_some_local_variable 
      FROM t_job
     WHERE trunc( start_time ) = trunc( sysdate );
  END IF;
END;

当然,一旦您将数据填充到局部变量中,您将需要实际对该值执行一些操作。您可能想要创建一个返回局部变量的函数,而不是像我在这里所做的那样使用匿名 PL/SQL 块。

Since this is a PL/SQL block, your SELECT statements would need to select the data into some local variable or they would need to be used in a cursor. Which approach you want would depend on how many rows in T_JOB could potentially match the criteria you're specifying. Assuming all three statements would return exactly 1 row, you could do something like this (code simplified to avoid repeating the same query twice)

DECLARE
  l_some_local_variable PLS_INTEGER;
BEGIN
  IF( to_number( to_char( sysdate, 'HH24' ) ) > 6 and
      to_number( to_char( sysdate, 'HH24' ) ) < 9 )
  THEN
    SELECT 1 
      INTO l_some_local_variable 
      FROM dual;
  ELSE
    SELECT (CASE WHEN result = 'SUCCESS' 
                 THEN 1
                 ELSE 0
             END) 
      INTO l_some_local_variable 
      FROM t_job
     WHERE trunc( start_time ) = trunc( sysdate );
  END IF;
END;

Of course, once you populate the data in your local variable, you would need to actually do something with the value. Potentially, you may want to create a function that returns the local variable rather than using an anonymous PL/SQL block as I have done here.

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