Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: SQL question (using result of nested statement in arithmetic operation)

Re: SQL question (using result of nested statement in arithmetic operation)

From: Robert Klemme <bob.news_at_gmx.net>
Date: Fri, 25 Apr 2003 17:57:43 +0200
Message-ID: <b8blum$7p4f9$1@ID-52924.news.dfncis.de>

"Stephan Grimm" <stephan.grimm_at_igd.fhg.de> schrieb im Newsbeitrag news:3EA94CB4.DAC5C024_at_igd.fhg.de...
> In order to emulate SEQUENCES with a table I would like to use something
> like the following nested statement to read and at the same time
> increase a number:
>
> UPDATE sequences SET seq_number = ((SELECT seq_number FROM sequences
> WHERE seq_id = $id) + 1) WHERE seq_id = $id;

This works with MS SQL Server, maybe it does the job with MySQL, too:

UPDATE sequences SET seq_number = (SELECT seq_number + 1 FROM sequences WHERE seq_id = $id) WHERE seq_id = $id;

> I guess the problem is that I try to use the result of a SELECT
> statement as operand for an arithmetic operation?!
> How can I get the number out of the subquery and increment it by 1 just
> like count() get's out a number of a result set ?
>
> BTW: I need a solution that works with Oracle as well as MySQL using the
> same syntax!

If you are working with MySQL 5.x then you could create a stored procedure that encapsulates the different syntaxes.

    robert Received on Fri Apr 25 2003 - 10:57:43 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US