tags:

views:

333

answers:

3

Afternoon Gents.

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

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.

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

Thanks for your time :)

+1  A: 

for IBM Imformix

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

  • 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
KM
+7  A: 

You cannot use sequences in queries with ORDER BY.

Remove the ORDER BY or put in into a subquery:

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
Quassnoi
This is correct and one of the limitations. If you want to pull sequences for the records in a particular order move the query (with order by) into an inline view and select the contents + the sequence from that
ChrisCM
`@ChrisCM`: nice point, adding. Thanks.
Quassnoi
Thanks for the help on this guys, appreciate it.
fras85
+1  A: 

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

Yeah, I was wondering why he was using a sequence here. Doesn't seem appropriate...
Dan Diplo
Most of the customers in the database already have ID's so I need to continue from that number onwards. I'm inserting these results into another table. Thanks again.
fras85