I use something like this in all the CF statements I write. You can use CFIF or CFCASE, or nest the two, or anything else. Unless its a relatively small table you will want to use SElect, column names from the table You can use the args to do anything from sort order, joins, so on and so forth. There is no end to how extendable this is, other than the time you want to spend writing code and what you want your query to do. The reason I would keep the tag format is, what if you need to have an SQL developer help you debug your statement. It's easier to read in simple tag format as it is far closer to the actual SQL statement and its far easier to easily take SQL developer's SQL statements and place them in ColdFusion. Give you an idea of how complex you can make your statement with a single pass. <cfquery name="myQuery" datasource="myDatasource">
SELECT column1, column2, column3, column4, column5
FROM sample
WHERE columnName = <cfqueryparam value="#inputSample#" cfsqltype="cf_sql_varchar">
<cfif inputSample EQ "apple">
UPDATE sample
SET inputsample1 = UCase(inputsample1)
<cfelseif inputSample EQ "banana">
<cfif dayOfWeek EQ "Monday">
SELECT *
FROM sample_2
WHERE customerName LIKE '%Orange%'
<cfelseif dayOfWeek EQ "Wednesday">
SELECT *
FROM sample_2
WHERE customerID LIKE '%7'
</cfif>
</cfif>
</cfquery> This still is readable to the average SQL Developer, and follows the ancient coldfusion holy texts (HHAHAH) where as the script version, is q = new Query();
q.setDataSource("myDatasource");
q.addParam(name="inputSample", value=inputSample, cfsqltype="cf_sql_varchar");
q.addSql("SELECT c1, c2, c3, c4, c5 FROM s WHERE n = :inputSample");
if (inputSample EQ "apple") {q.addSql("UPDATE s SET i1 = UCase(i1)");}
else if (inputSample EQ "banana") {
if (dayOfWeek EQ "Monday") {
q.addSql("SELECT * FROM s2 WHERE cn LIKE '%Orange%'");
} else if (dayOfWeek EQ "Wednesday") {
q.addSql("SELECT * FROM s2 WHERE ci LIKE '%7'");
}
}
result = q.execute().getResult();