I am inserting into two tables. Into the first table goes a new ‘item’. There are a lot of fields, but for simplicity I’ll only show you what I need. This is my coldfusion page:
<cfquery>
INSERT INTO my_item_table (itemid, a bunch of other stuff)
VALUES (itemid_sequence.nextval, a bunch of other stuff)
</cfquery>
My issue is I want to insert that auto-incremented itemid as a value into another table, a file-attachment table. The same coldfusion page:
<cfquery>
INSERT INTO my_attachments_table (attachno, itemid, filename)
VALUES (attachment_sequence.nextval, **AUTO-INCREMENT VALUE HERE**, '#url.fileName#')
</cfquery>
I know I could query the item table for the values that were just entered into the first table, but even though it’s not likely, there is a possibility that the user input isn’t unique, meaning a query of the same fields could return more than one row. In which case, I couldn’t get the itemid. The itemid created in the query is the only unique identifier.
My question is: is there a way to set the auto=increment to a value inside the query so I can use it outside? If not, how would you suggest getting my itemid? Thanks.
You could start by retrieving the next sequence value into a variable with a query like this:
(DUAL is a dummy table in Oracle). You could then proceed to use this variable in both INSERT queries.
Another option: create a stored procedure in the database that will fire both queries for you.