Introducing Dynamic SQL

You’ve used lots of <cfquery> tags thus far, and each of them has contained hard-coded SQL—SQL that you typed and that stays the same (the results may differ if the data in the database changes, but the SQL itself always stays the same). But SQL passed to ColdFusion need not be static and hard-coded; the real power of <cfquery> is seen when SQL is constructed dynamically.

To demonstrate what we mean, Listing 10.11 contains the code for a new file named dynamicsql.cfm. Save the code and execute it to see a screen like the one shown in Figure 10.13.

Listing 10.11. dynamicsql.cfm—Dynamic SQL Demonstration
<!--- Name: dynamicsql.cfm Author: Ben Forta (ben@forta.com) Description: Dynamic SQL demonstration Created: 07/01/07 ...

Get Adobe ColdFusion 8 Web Application Construction Kit, Volume 1: Getting Started now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.