User Guide
Completing the Trip Maintenance application 129
The table contains the following rows:
Updating multiple records
The
cfupdate statement works well when you want to update the current record within a
cfquery. Alternatively, you can update several rows within a table by issuing a single query using
cfquery and the SQL UPDATE statement. For example, if the base cost of all trips increased by
5%, you could issue the following query:
<!-- Routine to increase trip base Cost by 5% -->
<cfquery name="TripQuery" dataSource="CompassTravel">
UPDATE Trips SET baseCost = baseCost * 1.05
</cfquery>
Exercise: using SQL UPDATE with cfquery
In this exercise, you will develop a page to increase the price of every trip by 10%. This page runs
only once and is not part of the Trips Maintenance application. This exercise shows how to
update many database rows using a single SQL UPDATE statement and the ColdFusion
cfquery tag.
To update multiple database rows using SQL UPDATE with cfquery:
1 In an editor, open a new page and save it as priceincrease.cfm in the my_app directory.
2 Remove any lines of code that your editor added.
3 Add the following code:
<!---Routine to increase trip price by 10% --->
<cfquery name="TripQuery" dataSource="CompassTravel">
UPDATE trips SET price = price * 1.1
</cfquery>
<cfoutput> New prices are now in effect.</cfoutput>
4 Save the page then test the page by doing the following tasks:
a Use the Trip Maintenance application to take note of the price of any trip by viewing
tripdetail.cfm in a browser.
b Test by opening the priceincrease.cfmpage in your browser. This page automatically
updates the prices in the trips table.
c Use the Trip Maintenance application to verify that the query successfully increased the
price of the trip by 10%. To do this, navigate to the tripdetail.cfm and locate the trip you
noted in step 4a. The price of this trip is now 10% higher.
PersonID LastName FirstName Age
1TomGreen12
2 Peter Green 42
3PittJess21