Solved

Insert records into a table from the results of another query

Posted on 2014-03-06
1
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

632 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