The table contains the following rows:
PersonID
1
2
3
Updating multiple records
The
statement works well when you want to update the current record within a
cfupdate
. 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
cfquery
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
tag.
cfquery
To update multiple database rows using SQL UPDATE with cfquery:
In an editor, open a new page and save it as priceincrease.cfm in the my_app directory.
1
Remove any lines of code that your editor added.
2
Add the following code:
3
<!---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>
Save the page then test the page by doing the following tasks:
4
Use the Trip Maintenance application to take note of the price of any trip by viewing
a
tripdetail.cfm in a browser.
Test by opening the priceincrease.cfmpage in your browser. This page automatically
b
updates the prices in the trips table.
Use the Trip Maintenance application to verify that the query successfully increased the
c
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.
LastName
Tom
Peter
Pitt
FirstName
Green
Green
Jess
Completing the Trip Maintenance application
Age
12
42
21
129
Need help?
Do you have a question about the COLDFUSION MX 61-GETTING STARTED BUILDING COLDFUSION MX and is the answer not in the manual?
Questions and answers