Update statement - MapBasic - 2023

MapInfo MapBasic Reference

Product type
Software
Portfolio
Locate
Product family
MapInfo
Product
MapInfo > MapBasic
Version
2023
Language
English
Product name
MapBasic
Title
MapInfo MapBasic Reference
First publish date
1985
Last updated
2023-09-12
Published on
2023-09-12T16:32:32.686312

Purpose

Modifies one or more rows in a table. You can issue this statement from the MapBasic window in MapInfo Pro.

Syntax

Update table Set column = expr [ , column = expr, ...] 
	[ Where [ RowID = id ] | expr ]
	[ DropIndex ( Auto | On | Off ) ]

table is the name of an open table.

column is the name of a column.

expr is one or more expressions, separated by the keywords AND or OR.

id is the number of a row in the table.

Description

The Update statement modifies one or more columns in a table. By default, the Update statement will affect all rows in the specified table. However, if the statement includes a Where RowID clause, only one particular row will be updated. The Set clause specifies what sort of changes should be made to the affected row or rows.

To update the map object that is attached to a row, specify the column name Obj in the Set clause; see example below.

Note: After performing an Update statement (or other statements that modify tables), you can retrieve table values using a column reference expression (for example, tablename.columnname). However, before referencing any expression, you must perform the Fetch statement to specify a table row. For details, see the Working with Tables chapter of the MapBasic User Guide.

Specifying that the DropIndex clause is On suspends updating transaction indexes while executing the operation and recreates them when the operation is complete. In some cases this significantly improves performance, but not always. For the best performance, we recommend setting this clause to Auto, so that MapInfo Pro decides whether or not to suspend and recreate indexes. MapInfo Pro will suspend and recreate indexes later if the operation is deleting or updating the rows and the table contains more than 1000 rows and more than 0.1% of the total rows are getting updated. When the DropIndex clause is Off the operation executes without suspending and recreating the transaction indexes.

The Update statement also supports providing an expression in the Where clause. In case the statement include a Where expression clause, only the rows matching the expression are updated.
Update PopAreas Set Area_sqm = Area(obj, "sq_m") Where Area_sqm = 0
If you want to use RowId as an expression in the WHERE clause, use like:
"4 > RowId OR 2 < RowId" instead of "RowId > 4 OR RowId < 2"

Examples

In the following example, we have a table of employee data; each record states the employee's department and salary. Let us say we wish to give a seven percent raise to all employees of the marketing department currently earning less than $20,000. The example below uses a Select statement to select the appropriate employee records, and then uses an Update statement to modify the salary column accordingly.

Select * From employees 
	Where department ="marketing" And salary < 20000
Update Selection
	Set salary = salary * 1.07

By using a Where RowID clause, you can tell MapBasic to only apply the Set operation to one particular row of the table. The following example updates the salary column of the tenth record in the employees table:

Update employees 
	Set salary = salary * 1.07
	Where Rowid = 10

By using a Where expression clause, you can tell MapBasic to only apply the Set operation to selected rows of the table. The following example updates the salary column of employees having more than five years of experience in the employees table:

Update employees 
	Set salary = salary * 1.07
	Where EmpExp > 5

The next example stores a point object in the first row of a table:

Update sites 
	Set Obj = CreatePoint(x, y) 
	Where Rowid = 1

See Also:

Insert statement, Fetch statement