Ask a Question related to Coldfusion Database Access, Design and Development.
-
JimCu #1
Retrieving ID of Last Inserted Table
Hi, I've done a search of the forums and although I've found similar questions
none of them seemed to answer my question.
I'm currently writing an application for my business which will create
invoices for me. I have set up 2 MsSql tables, one called tbl_invoices and
another called tbl_invoice_items. The idea is that I create an invoice in
tbl_invoices and then start adding line items to it on the next page. The
invoice_ID field is auto generated by MsSq. Is there a way of retreiving the id
assigned by the database so I can write the correct invoice_ID to the
tbl_invoice_items table. Or am I approaching this from the wrong direction.
I envisaged the flow of the application like so.
1)Enter Data for Invoice in form eg customer name, date etc
2)Form Data written to database
3)Forwards to page with form fields for entering line items
4)Write invoice items to database and return to 3 until complete.
I appreciate your help.
Jim
JimCu Guest
-
Ordered Data Inserted into TEMP table, is Read out in Different ORDER??
Im using a query with OrderBy clause to populate a temp table. The Query works and Order and everything is Fine But when reading the data out of... -
Horizontal Scroll appears after 2nd Table inserted
Hello there, I'm new to Dreamweaver, yet alone programming HTML, so please bare with me... I'm designing a website for a 1024 x 768... -
retrieving last record inserted using @@IDENTITY - ASPJScript
Im triyng to retrieve the last record inserted usnig the "Insert" Server Behavior, in a table that have an auto-incrementig id column. Im trying... -
MSACCESS hangs when retrieving LIST type field (linked informix table)
I have fields like: ,field LIST(VARCHAR(255) NOT NULL) NOT NULL MSACCESS hangs when i retrieve this via a linked table. I also have fields... -
Could not load type VTFixup Table from assembly Invalid token in v-table fix-up table.
We are getting this error after clearing the web.config of database infomation - even after using the wizard to re-enter the information. I could... -
kabbi~thkek #2
Re: Retrieving ID of Last Inserted Record
Hi,
Do not no if this is something you can use:
<cfquery name="id" datasource="">
SELECT LAST_INSERT_ID() AS highest_id
</cfquery>
<cfset vardev_id = id.highest_id>
<cflocation url="page_2.cfm?invoice_ID=#vardev_id#">
then on page two make a query (WHERE invoice_ID = #URL.invoice_ID# or
something) And insert the values you want to insert into the new table.
Hope this was usefull
Bye, Kabbi
kabbi~thkek Guest
-
Dan Bracuk #3
Re: Retrieving ID of Last Inserted Record
wrap two queries inside cftransaction and cflock tags.
Query 1 - insert your record
Query 2 - select the max(id) and have a great big where clause.
Dan Bracuk Guest
-
JimCu #4
Re: Retrieving ID of Last Inserted Record
Thanks, I'll give them both a go and see how it goes. Should work okay for a
limited number of users, it's shame that an insert query doesn't return the
primary key as a value. Perhaps we'll see that in 7.1?
JimCu Guest
-
PaulH *TMM* #5
Re: Retrieving ID of Last Inserted Table
JimCu wrote:
well for starters don't use anything that uses a select MAX(), that will> I'm currently writing an application for my business which will create
> invoices for me. I have set up 2 MsSql tables, one called tbl_invoices and
eventually become a bottleneck. if by "MsSql" you mean ms sql server & by "auto
generated" you mean a column w/an IDENTITY characteristic, then use the native
sql server stuff in you're cfquery:
SET NOCOUNT ON
INSERT INVOICE(....
SELECT SCOPE_IDENTITY() AS invoiceID
SET NOCOUNT OFF
if you're sure there are no triggers that act on tables w/IDENTITY columns then
you could also use SELECT invoiceID=@@IDENTITY instead of the SCOPE_IDENTITY()
function. in either case the cfquery resultset will contain an "invoiceID" column.
PaulH *TMM* Guest
-
ptoretti #6
Re: Retrieving ID of Last Inserted Table
What Paul said - Make sure that the record you get back when going for the
maxID is the same one you wrote. If you have alot of transactions, you might
get the wrong one. Using some method other than MaxID is better.
ptoretti Guest



Reply With Quote

