Archive for April, 2008

Reduce CF Markup In Queries Through A Simple SQL Trick

Wednesday, April 30th, 2008

Here are a couple examples of how SQL offers the ability to simplify your SQL/CF mix-in markup.

<cfquery name="qFoo" datasource="bigdb">
    UPDATE mytable SET
    <cfloop item="MyObj" collection="ObjectsGalore">
        #MyObj.column# = <cfqueryparam value="#MyObj.value#" />,
    </cfloop>
    1=1
</cfquery>

<cfquery name="qFoo" datasource="bigdb">
    SELECT * FROM mytable
    WHERE 1=1
    <cfif Len(arguments.somethingOptional)>
        AND baz = <cfqueryparam value="#arguments.somethingOptional#" />
    </cfif>
    <cfif Len(arguments.anotherOptional)>
        AND qux = <cfqueryparam value="#arguments.anotherOptional#" />
    </cfif>
</cfquery>