Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Insert records into a table from the results of another query

Posted on 2014-03-06
1
Medium Priority
?
422 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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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 …
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 …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

926 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