I want to create a function that will loop through an arbitrary query and perform an insert into another table based on the arbitrary column names.
For instance, the idea here would be to output
(data, data, data...)
(data, data, data...)
Ultimately, I'm going to do an insert query. For now, I just want to output the rows.
EDIT: I can't just do an INSERT/SELECT
because the prod data and dev data are on different servers. So I have to first collect the data from the table on the prod server into a CF query object, and then loop through it and insert into the table on the dev server.
Code:
<cffunction name="copyProdToDev">
<cfargument name="devDatasource" >
<cfargument name="prodDataSource" type="string">
<cfargument name="devTableName" type="string">
<cfargument name="prodTableName" type="string">
<cfargument name="dateColumnName" default="none">
<cfquery name="ProdData" datasource="#prodDatasource#" timeout="60">
SELECT *
FROM #prodTableName#
</cfquery>
<cfset columnNames = ProdData.getColumnNames()>
<cfset numColumns = ArrayLen(columnNames)>
<cfloop query="#ProdData#">
(
<cfloop index="colNumber" from="1" to="#ArrayLen(columnNames)-1#">
<cfoutput><dynamic column name for colNumber>,</cfoutput>
</cfloop>
<cfoutput><dynamic column name for numColumns></cfoutput>
)<br />
</cfloop>
</cffunction>
Array notation is your friend.
<cfoutput>
<cfloop query = "ProdData">
<cfloop array = "#ProdData.getColumnList()#" index = columnName>
#prodData[columnName][currentrow]#
closing tags and formatting stuff