![]() ![]() If you execute such statement in your admin tool, you would get a single-row resultset with column ID containing newly inserted record ID. INSERT INTO t1(field1) VALUES('my stuff') RETURNING id Here's an example that would return the newly inserted ID from an insert statement: If you wish to get the new ID from an INSERT statement, use the RETURNING clause (only available in Firebird 2.1 and above). Most GUI administration tools have options to generate this code for you automatically, so it is not a problem. Please refer to FAQ #78 for more details.Īll this might seem awkward, but it's a proper way to do it in a multiuser environment. If you wonder why are those SET TERM statements needed: it's because many tools use semi-colon to separate statements and trigger code contains a semi-colon so statement terminator character needs to be changed. It can be done in many different ways, but this is the most simple approach: if value for column is not supplied - autoincrement it. ![]() If (NEW.ID is NULL) then NEW.ID = GEN_ID(GEN_T1_ID, 1) Ĭhecking whether NEW.ID is NULL is a common way to prevent getting incremented values when you copy data from some other database or import from some other data source. Since we set it to zero, the first record will get a value of one. SET GENERATOR statement is used to set the starting value of a sequence. ![]() To make sure we get unique values consistently we use a GENERATOR (a.k.a. To make column ID autoincrement, we don't have to do anything special to the column itself, but we need to create a BEFORE INSERT trigger that will put a new value each time a new record is inserted. Generators are also named SEQUENCES in Firebird 2.0 and above - and are compliant to the SQL standard.įor example, let's suppose you have a table named T1, with the following columns: Firebird does support autoincrement columns via BEFORE INSERT TRIGGERs and GENERATORs. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |