Created
August 30, 2013 17:21
-
-
Save anonymous/6392229 to your computer and use it in GitHub Desktop.
Insert list of id's example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- id's of the records to transfer ---> | |
<cfset questionList = "1,2"> | |
<!--- get questions and answer to copy ---> | |
<cfquery name="getQuestions" datasource="#dsn#"> | |
SELECT QuestionID, QuestionText | |
FROM Question | |
WHERE QuestionID IN (<cfqueryparam value="#questionList#" cfsqltype="cf_sql_integer" list="true">) | |
</cfquery> | |
<cfquery name="getAnswers" datasource="#dsn#"> | |
SELECT AnswerID, QuestionID, AnswerText | |
FROM Answer | |
WHERE QuestionID IN (<cfqueryparam value="#questionList#" cfsqltype="cf_sql_integer" list="true">) | |
</cfquery> | |
<!--- copy question records and store id's ---> | |
<cfset idMapping = {}> | |
<cfloop query="getQuestions"> | |
<cfquery result="addRecord" datasource="#dsn#"> | |
INSERT INTO Question (QuestionText) | |
VALUES ( <cfqueryparam value="#questionID# Copy" cfsqltype="cf_sql_varchar">); | |
</cfquery> | |
<cfset idMapping[ QuestionID ] = addRecord.GENERATED_KEY> | |
</cfloop> | |
<cfquery result="addRecord" datasource="#dsn#"> | |
INSERT INTO Answer ( QuestionID, AnswerText ) | |
VALUES | |
<cfloop query="getAnswers"> | |
( | |
<cfqueryparam value="#idMapping[ QuestionID ]#" cfsqltype="cf_sql_integer"> | |
, <cfqueryparam value="#AnswerText# Copy" cfsqltype="cf_sql_varchar"> | |
) | |
<cfif getAnswers.CurrentRow LT getAnswers.RecordCount>,</cfif> | |
</cfloop>; | |
</cfquery> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment