Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I think the site is just incredible....I am learning more here than anywhere else before and am looking forward to being able to help someone .... this is my idea of what the Internet is supposed to do..."

Geography

Where in the world do Tek-Tips members come from?

CFLOOP of recordset/insert into seperate tableHelpful Member! 

Dave3462 (TechnicalUser)
2 Jun 09 14:51
I need help doing a loop over the following recordsets to insert into another table. Hopefully the code/markup will explain it better.

<!--- select all kits associated with a service instruction from the form --->
<cfquery name="getKits" datasource="#dsn#">
SELECT kit
FROM ACFT_KIT
WHERE SERV_INSTR = '#form.mySERV_INSTR#'
ORDER BY kit
</cfquery>

<!---If this is first requisition posted today reset counter table to reflect todays date and start serial number at 1--->
<cfquery name="updSERNO" datasource="#dsn#">
update serno_ctr
set serno = '1',
upd_date = to_char(sysdate, 'dd-mon-yyyy')
where upd_date != to_char(sysdate, 'dd-mon-yyyy')
</cfquery>

<!---Select the serial number from counter table--->
<cfquery name="nexSerno" datasource="#dsn#">
SELECT serno
FROM serno_ctr
</cfquery>

<!---Increment the serial number--->
<cfset nextSERNO = #nexSerno.serno# + 1>

<!---
This is where I need the help. I need to loop over the INSERT query to insert seperate records for each kit retrieved in the getKits query and increment the serno for each new record with a final UPDATE query into the serno table with the last records serial number. The basic query if there were only one kit would look like this
--->
<cfquery name="insKITS" datasource="pbl_tracking">
INSERT INTO ACFT_PARTS_TBL (JDATE, PBL_CD, MATL_TYPE, BUNO, JON, SERV_INSTR, PROJECT, UNIT, QTY, DROP_LOCATION, PART_NO, serno)
VALUES('#form.jdate#', 'BV', 'KIT', '#form.myBUNO#', '#getJON.myJON#', '#form.mySERV_INSTR#',  '#getJON.myPROJECT#', 'EA', '1', 'STA9', '#getKits.kit#', '#nextSERNO#')
</cfquery>

<!---only one record is in the serno table, no need to use where--->
<cfquery name="updSERNO" datasource="#dsn#">
update serno_ctr
set serno = '#nextSERNO#'
</cfquery>

Thanks for your help.
Dave
LyndonOHRC (Programmer)
2 Jun 09 17:58
I don't see a getJON query so I'm guessing you have access to it in your actual template...

But from what I can tell from what you provided this should work:

CODE

<cfset nextSERNO = nexSerno.serno + 1>

<cfoutput query="getKits">
  <cfset nextSERNO = nextSERNO + 1>
  <cfquery name="insKITS" datasource="pbl_tracking">
  INSERT INTO ACFT_PARTS_TBL (JDATE, PBL_CD,    MATL_TYPE,      BUNO, JON, SERV_INSTR, PROJECT, UNIT, QTY,    DROP_LOCATION, PART_NO, serno)
  VALUES('#form.jdate#', 'BV', 'KIT', '#form.myBUNO#', '#getJON.myJON#', '#form.mySERV_INSTR#',  '#getJON.myPROJECT#', 'EA', '1', 'STA9', '#getKits.kit#', '#nextSERNO#')
</cfquery>
</cfoutput>

<cfquery name="updSERNO" datasource="#dsn#">
update serno_ctr
set serno = '#nextSERNO#'
</cfquery>

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel.  Covey

Dave3462 (TechnicalUser)
3 Jun 09 8:40
Thanks Lyndon,
I had it stuck in my head that a CFLOOP was going to be needed. Your solution worked out great.
LyndonOHRC (Programmer)
3 Jun 09 11:21
Oh BTW, in my example
<cfset nextSERNO = nexSerno.serno + 1>

should be
<cfset nextSERNO = nexSerno.serno>

or you will be skipping a number every time this code runs.

Sorry I just saw that...

Good luck.
 

Lyndon

---People Remember about 10% of what you say ---They never forget how you made them feel.  Covey

Dave3462 (TechnicalUser)
8 Jun 09 12:42
I caught it and already made the change.

<cfset nextSERNO = nexSerno.serno>

<cfoutput query="getKits">

<cfset nextSERNO = nextSERNO + 1>
  
<cfquery name="insKITS" datasource="pbl_tracking">.....
</cfoutput>

Again, thanks for your assistance.
Helpful Member!  cfSearching (Programmer)
11 Jun 09 16:44

Quote:

<cfset nextSERNO = nexSerno.serno>

<cfoutput query="getKits">
<cfset nextSERNO = nextSERNO + 1>
....

Since CF is multi-threaded, it is possible multiple requests could end up with the same serial numbers. The more concurrent users in the application, the higher the probability that will happen. If that is undesirable, you need to implement some sort of locking to avoid it.

The vast majority of the time, the database is the best place for this kind of logic, not CF.  Databases are better equipped to handle low data level locking. Check your database's documentation for information on transaction levels and locking.  

HTH

 

----------------------------------
http://cfsearching.blogspot.com/

LarrySteele (Programmer)
15 Jun 09 15:18
cfSearching - that's a great point, easily overlooked.  That deserves a star.
cfSearching (Programmer)
17 Jun 09 21:23
Thanks :)

----------------------------------
http://cfsearching.blogspot.com/

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close