
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
1
Tom
Green
12
2
Peter
Green
42
3
Pitt
Jess
21
Содержание COLDFUSION MX 61-GETTING STARTED BUILDING COLDFUSION...
Страница 1: ...Getting Started Building ColdFusion MX Applications...
Страница 6: ...6 Contents...
Страница 10: ......
Страница 30: ...30 Chapter 2 CFML Basics...
Страница 36: ...36 Chapter 3 Database Fundamentals...
Страница 48: ......
Страница 76: ...76 Chapter 6 Lesson 2 Writing Your First ColdFusion Application...
Страница 134: ...134 Index...