Solved

Insert records into a table from the results of another query

Posted on 2014-03-06
1
358 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

947 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now