Menu

Generate CFML code to create a query from a query

29 May 2007
I've just realised I am terrible at writing good, descriptive subjects for my blog posts so here is a (bit of a) better description of what I mean.Last week I needed to use a query from a live DB to do some work on offline. Usually I use MySQL so I take a backup from the night before and run it locally so I am nearly up-to-date on my dev machine but this DB was MSSQL and I haven't gotten around to getting that setup on Parallels. Also there was no need to download 'X' amount of tables and stored procedures as all I wanted to actually use was one query output.So what I've created is a function which generates the CFML for a queryNew() statement needed to re-create the query you want to work on.#ltc#!--- First we create the query --->#chr(13)##ltc#cfset newQry = queryNew('#qry.columnList#') />#ltc#!--- Now start populating the query --->#chr(13)##ltc#cfset queryAddRow(newQry) />#chr(13)# #ltc#cfset querySetCell(newQry, '#cols#', '#qry[cols][i]#') />#chr(13)# #ltc#!--- Dump the query --->#chr(13)##ltc#cfdump var="##newQry##" label="Query Dump" /> So in action we would have the following which would create a new .cfm page called "test.cfm" as well as outputting the results to the screen SELECT id,sex,name FROM test#myVar.code#In the generated test.cfm you would seeAndy Jarrett's Picture