You are on page 1of 3

MapInfo Products Knowledge Base

Product: MapBasic 
Version: 6.x 
Platform: Not Platform Related 
Category: Sample Applications

Summary:
Sample code in MapBasic for doing calculations based on several columns in a
table.

Question:
There are several ways to approach this; generally, it is possible to either loop through
the rows of a table one at a time and perform a calculation on each row, or perform an
SQL Select operation and create a new table with an extra column containing the
results of the calculation. It will be necessary to use a loop only if the calculation
depends on the values of more than one row of the table (e.g. calculating a cumulative
weighted sum).

Method 1 --

Declare Sub Main

Sub Main
dim fname, tname as string, targetvar1, targetvar2, targetvar3 as Float

fname="USA.TAB" ' hard-coding value of string for this example -- could be anything
tname="USA"
Open Table fname as tname Interactive

' method #1
Select * from tname into TempTable
Fetch First From TempTable
Do While Not EOT(TempTable)
targetvar1=TempTable.POP_1980 * log(TempTable.POP_1990)
targetvar2=sin(TempTable.POP_1980)
targetvar3=targetvar1-targetvar2
Print "targetvar3 = " & targetvar3
Fetch Next from TempTable
Loop
End Sub

Method 2 --
If it is possible to use SQL Select statements to do the calculations necessary, it will
generally be much faster than the method #1 Do... Fetch... Loop system. SQL Select
statements actually build new temporary tables with the results of the formulas
specified. Often, it isn't necessary to assign anything to a variable at all.

The code below returns the same results as the method #1 code but it displays it in a
browser window instead of a message window. To retrieve the results from this
browser window, row by row, it would again be necessary to use a Fetch First... Do ...
Fetch ... Loop structure.

Declare Sub Main

Sub Main
dim fname, tname as string, targetvar1, targetvar2, targetvar3 as Float

fname="USA.TAB" ' hard-coding value of string for this example -- could be anything
tname="USA"
Open Table fname as tname Interactive

' method #2
Select POP_1980 * log(POP_1990) "LogCalc", sin(POP_1980) "SineCalc" from tname into
TempTable
Select LogCalc "LogCalc", SineCalc "LogCalc", LogCalc-SineCalc "Difference" from
TempTable into TempTable2
Browse * from TempTable2
End Sub

Method 3 --

If it is necessary to use column aliases:

Declare Sub Main

Sub Main
dim fname, tname, colname1, colname2 as string, targetvar1, targetvar2, targetvar3 as Float
dim colalias1, colalias2 as alias

fname="USA.TAB" ' hard-coding value of string for this example -- could be anything
tname="USA"
colname1="POP_1980"
colname2="POP_1990"

Open Table fname as tname Interactive


' method #3 -- using column aliases
Select * from tname into TempTable
Fetch First From TempTable
colalias1 = tname & "." & colname1
colalias2 = tname & "." & colname2

Do While Not EOT(TempTable)


targetvar1=colalias1 * log(colalias2)
targetvar2=sin(colalias1)
targetvar3=targetvar1-targetvar2
Print "targetvar3 = " & targetvar3
Fetch Next from TempTable
Loop
End Sub

You might also like