oracle SQL Select 中的序列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1245125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 18:46:20  来源:igfitidea点击:

Sequence within SQL Select

sqloracle

提问by fras

I'm having a bit of a problem with using my sequence within a SELECTstatement.

SELECT语句中使用我的序列时遇到了一些问题。

SELECT
     c.cust_name,
     c.site,
     customer_id_seq.nextval    
FROM
     customer c
WHERE
     c.customer_id IS NULL
ORDER BY
     c.site_code ASC
;

Is giving me an error:

给我一个错误:

  1. 00000 - "sequence number not allowed here" *Cause: The specified sequence number (CURRVAL or NEXTVAL) is inappropriate here in the statement. *Action: Remove the sequence number.
  1. 00000 - “此处不允许使用序列号” *原因:在语句中指定的序列号(CURRVAL 或 NEXTVAL)在此处不合适。*操作:删除序列号。

It's probably something obvious I'm doing wrong so hopefully this will be an easy answer.

很明显我做错了,所以希望这将是一个简单的答案。

回答by Quassnoi

You cannot use sequences in queries with ORDER BY.

您不能在带有 的查询中使用序列ORDER BY

Remove the ORDER BYor put in into a subquery:

删除ORDER BY或放入子查询:

SELECT  q.*, customer_id_seq.nextval    
FROM    (
        SELECT  c.cust_name,
                c.site
        FROM    customer c
        WHERE   c.customer_id IS NULL
        ORDER BY
                c.site_code ASC
        ) q

回答by KM.

for IBM Imformix

用于IBM Imformix

In a SELECT statement, you cannot specify NEXTVAL or CURRVAL in the following contexts:

在 SELECT 语句中,您不能在以下上下文中指定 NEXTVAL 或 CURRVAL:

  • In the projection list when the DISTINCT keyword is used
  • In the WHERE, GROUP BY, or ORDER BY clauses
  • In a subquery
  • When the UNION operator combines SELECT statements
  • 在使用 DISTINCT 关键字时的投影列表中
  • 在 WHERE、GROUP BY 或 ORDER BY 子句中
  • 在子查询中
  • 当 UNION 运算符组合 SELECT 语句时

回答by KM.

Why don't you use rownum instead of fetching values from sequence?

为什么不使用 rownum 而不是从序列中获取值?