Solved

Insert records into a table from the results of another query

Posted on 2014-03-06
1
365 Views
Last Modified: 2014-03-12
I have a function similar to the one below. I want to perform the update in the first query and then insert some data into a different table in the 2nd query using the record numbers (ID)s that were updated in the first query. Could someone assist?  The first query is fine... the 2nd doesn't work obviously. The ID is the primary key for the records in the first query.

<cffunction name="myFunction" output="false" access="remote" returntype="string" hint="">
	<cfargument required="true" type="string" name="Name" />
	<cfargument required="true" type="string" name="Age" />

<cfquery ...>
			UPDATE MYDB.table
			set Status = 'Complete', Notes = 'blah' 
			where Age = <cfqueryparam value = "#Trim(ARGUMENTS.Age)#" CFSQLType = "CF_SQL_VARCHAR"> 			
</cfquery>

--------this is where i want to insert a record for each of the ones that were updated above....

<cfquery ...>
	INSERT INTO myDB.Table2 (ID, age, blah)
	VALUES ...
</cfquery>

Open in new window

I know this isn't a complete query/example but I am hoping I gave enough information. Thank you.
0
Comment
Question by:earwig75
1 Comment
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 39909803
You didn't mention your DBMS, but if it's SQL Server 2005+ use an OUTPUT clause to capture the id's of the records updated in a table. Then JOIN to it in an INSERT/SELECT.

* Omitted cftransaction for brevity

<cfquery ....>
<!--- temp table to hold changed id's --->
DECLARE @UpdatedRecords TABLE ( ID INT )

<!--- update value and save ID's to temp table --->
UPDATE  SomeTable
SET       Status = 'Complete', Notes = 'blah'
OUTPUT Inserted.ID INTO @UpdatedRecords
WHERE  Age = <cfqueryparam value = "#Trim(ARGUMENTS.Age)#" ....etc.... >

<!--- insert NEW values into another table --->
INSERT INTO OtherTable (ID, Age, Status)
SELECT  t.ID, t.Age, t.Status
FROM    SomeTable t INNER JOIN @UpdatedRecords tmp ON tmp.ID = t.ID
</cfquery>
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question