Solved

Insert records into a table from the results of another query

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

14 Experts available now in Live!

Get 1:1 Help Now