Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Firebird e Interbase (https://www.clubdelphi.com/foros/forumdisplay.php?f=19)
-   -   Como lograr el último ID insertado? (https://www.clubdelphi.com/foros/showthread.php?t=16373)

anavarro 25-11-2004 14:07:24

Como lograr el último ID insertado?
 
Me gustaria saber si existe algún método o sentencia en Firebird 1.5 que devuelva el último ID insertado. En mysql si existe last_insert_id(), que devuelve el último indentificador insertado en un campo autoincrementable. Esta funcion creo que no existe en Firebird y quisiera saber si hay alguna que haga lo mismo, o en su caso como lo podria hacer.

Gracias.

LordBits 25-11-2004 16:19:52

esta es la solucion que dan en www.ibphoenix.com..

Assume that you have already created a generator in your database as:

CREATE GENERATOR my_gen_id;

Examples:
Method 1: (SQL)
SELECT DISTINCT(GEN_ID(my_gen_id, 0))FROM table_name;
NOTE: Please notice that the increment value is (0) zero. This is to ensure
that you do not increment the generator when querying for its next value.

Method 2: (ISQL)
While attached to the database via an ISQL session type the following.
SQL> SHOW GENERATOR my_gen_id;

Method 3: (STORED PROCEDURE)
Code:
SET TERM !!;
CREATE PROCEDURE get_next_gen_value
RETURNS (next_value INTEGER)
AS
BEGIN
next_value = GEN_ID(my_gen_id, 0);
SUSPEND;
END !!
SET TERM ;!!

NOTE: Please notice that the increment value is (0) zero. This is to ensure
that you do not increment the generator when querying for its next value.
Once the procedure is successfully created you can issue a SELECT statement
against it.

SQL:
SELECT * FROM get_next_gen_value;

defcon1_es 25-11-2004 16:22:42

Hola,
si al campo ID le asignas su valor mediante un generador,
puedes usar : GEN_ID(<MiGenerador>, 0)

Esta función devuelve el valor actual del Generador,
es decir, del último ID insertado.

Salu2.


La franja horaria es GMT +2. Ahora son las 03:27:42.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi