Skip to main content
Participating Frequently
October 4, 2007
Answered

Passing a value from one table to another

  • October 4, 2007
  • 1 reply
  • 397 views
Access Database:
I have 2 tables, table 1 has a field that is a primary key (indexed, no dups), table 2 has a field that needs to receive the same number as represented in table 1 .
The problem is that when the query runs it gets the MAX (number) but when it's inserted into table 2, all of the records in table 2 are updated with this number.
Is there a way to prevent all of the records in table 2 form being updated and only the newest record receiving the number from table 1?
Any Ideas?

This is my query:

<cftransaction>

<cfif len(trim(form.Time_Spent)) gt 0>
<cfquery name="InsertRecord" datasource="NBProdReports">
Insert Into TblMasterTrans (Time_Spent) Values ('#form.Time_Spent#')
</cfquery>
</cfif>

<cfif len(trim(form.Notes)) gt 0>
<cfquery name="InsertRecord" datasource="NBProdReports">
Insert Into TblContractInfo_SubMenuTable (Contract_Number , Transaction_Type , Num_Checks_Trans , Notes)
Values ('#form.Contract_Number#','#form.Trans_Type#','#form.Num_Checks_Trans#','#form.Notes#')
</cfquery>
</cfif>

<cfquery name="getTask_Number" datasource="NBProdReports">
SELECT MAX (Task_Number) AS theTask_Number
From TblMasterTrans
</cfquery>

<cfquery name="SetTaskNumber" datasource="NBProdReports">
Update TblContractInfo_SubMenuTable
Set
Task_Number=#getTask_Number.theTask_Number#
Where Task_Number = Null
</cfquery>

</cftransaction>
This topic has been closed for replies.
Correct answer paross1
In fact, you might even get away with doing it with one query by SELECTing the MAX Task_Number within the second insert statement.

<cftransaction>

<cfif len(trim(form.Time_Spent)) gt 0>
<cfquery name="InsertRecord" datasource="NBProdReports">
Insert Into TblMasterTrans (Time_Spent) Values ('#form.Time_Spent#')
</cfquery>
</cfif>

<cfif len(trim(form.Notes)) gt 0>
<cfquery name="InsertRecord" datasource="NBProdReports">
Insert Into TblContractInfo_SubMenuTable (Task_Number, Contract_Number,
Transaction_Type , Num_Checks_Trans , Notes)
SELECT MAX(Task_Number), '#form.Contract_Number#',
'#form.Trans_Type#','#form.Num_Checks_Trans#','#form.Notes#'
From TblMasterTrans
</cfquery>
</cfif>

</cftransaction>

Phil

1 reply

Participating Frequently
October 4, 2007
First of all, Where Task_Number = Null is not a valid statement, since if you were trying to update all rows where Task_Number is null, you would say WHERE Task_Number IS NULL, not = NULL.

Plus, why don't you query TblMasterTrans for the MAX Task_Number BEFORE you do the insert into the TblContractInfo_SubMenuTable so that you don't have to do an insert AND an update?
<cftransaction>

<cfif len(trim(form.Time_Spent)) gt 0>
<cfquery name="InsertRecord" datasource="NBProdReports">
Insert Into TblMasterTrans (Time_Spent) Values ('#form.Time_Spent#')
</cfquery>
</cfif>

<cfquery name="getTask_Number" datasource="NBProdReports">
SELECT MAX (Task_Number) AS theTask_Number
From TblMasterTrans
</cfquery>

<cfif len(trim(form.Notes)) gt 0>
<cfquery name="InsertRecord" datasource="NBProdReports">
Insert Into TblContractInfo_SubMenuTable (Task_Number, Contract_Number,
Transaction_Type , Num_Checks_Trans , Notes)
Values (#getTask_Number.theTask_Number#, '#form.Contract_Number#',
'#form.Trans_Type#','#form.Num_Checks_Trans#','#form.Notes#')
</cfquery>
</cfif>

</cftransaction>

Phil
paross1Correct answer
Participating Frequently
October 4, 2007
In fact, you might even get away with doing it with one query by SELECTing the MAX Task_Number within the second insert statement.

<cftransaction>

<cfif len(trim(form.Time_Spent)) gt 0>
<cfquery name="InsertRecord" datasource="NBProdReports">
Insert Into TblMasterTrans (Time_Spent) Values ('#form.Time_Spent#')
</cfquery>
</cfif>

<cfif len(trim(form.Notes)) gt 0>
<cfquery name="InsertRecord" datasource="NBProdReports">
Insert Into TblContractInfo_SubMenuTable (Task_Number, Contract_Number,
Transaction_Type , Num_Checks_Trans , Notes)
SELECT MAX(Task_Number), '#form.Contract_Number#',
'#form.Trans_Type#','#form.Num_Checks_Trans#','#form.Notes#'
From TblMasterTrans
</cfquery>
</cfif>

</cftransaction>

Phil