Here’s how to use alter() functions.
Description:
Alter fields of a record.
r.alter(Fi,…;F’i,…)
Note:
The function alters fields of record r in the order of Fi,…. If the to-be-altered fields are not specified, perform the modification according to the original order of fields; if field Fi does not exist in the record, add it as a new field. F’i is the to-be-deleted fields.
Parameter:
r |
A record |
Fi |
Field names |
F’i |
Names of to-be-deleted fields |
Return value:
Record
Example:
|
A |
|
1 |
=demo.query("select top 9 class,studentid,subject,score from scores") |
Return a table sequence:
|
2 |
=A1(1) |
Return the 1st record of table sequence A1:
|
3 |
=A2.alter(classname;CLASS) |
Add classname field to A2’s record while deleting CLASS field:
|
4 |
=A2.alter(classname,STUDENTID,gender) |
Add classname field and gender field to A2’s record according to the order classname,STUDENTID,gender; put the rest of the fields CLASS,SUBJECT,SCORE after them:
|
5 |
=A2.alter(;CLASS) |
Delete CLASS field from A2’s record:
|
Description:
The function alters fields in a table sequence.
Syntax:
T.alter(Fi,…;F’i,…)
Note:
The function modifies fields of table sequence T according to the specified order Fi,…, and arranges the other fields in their original order. Add parameter Fi as a new field if there isn’t such a field in the table sequence; parameter F’I is the field to be deleted.
Parameter:
T |
A table sequence |
Fi |
Field name |
F’i |
Field name |
Return value:
Table sequence
Example:
|
A |
|
1 |
=demo.query("select class,studentid,subject,score from scores") |
The scores table contains columns as follows:
|
2 |
=A1.alter(classname;CLASS) |
Add a new field classname to A1’s table sequence and deletes CLASS field.
|
3 |
=A2.alter(classname,STUDENTID,gender) |
Add a new field gender to A2’s table sequence according to the specied position and arrange the rest of the fields after the specific ones in their original order.
|
4 |
=A3.alter(;gender,classname) |
Delete gender field and classname field.
|
Description:
Alter one or more fields in an entity table.
Syntax:
T.alter(F:x,…;F’,…)
Note:
The function, supported only when a table is of columnar storage, adds a field F to entity table/multizone entity table T and deletes an old field F’. Values of the new field are obtained by calculating expression x and populated to the table. The dimension and sorting field before it must not be modified.
Parameter:
T |
An entity table/multizone entity table |
F |
Name of the to-be-added field |
x |
An expression |
F’ |
An existing field |
Return value:
An entity table/multizone entity table
Example:
|
A |
|
1 |
=file("emp.ctx") |
An existing composite table file. |
2 |
=A1.open() |
Open the composite table’s base table. |
3 |
=A2.cursor().fetch() |
Query data in the base table.
|
4 |
=A2.alter(NEW_Salary:SALARY+1000;GENDER) |
Add NEW_Salary field to the base table through calculating expression SALARY+1000, and delete existing field GENDER. |
5 |
=A2.cursor().fetch() |
Check the base table and we can see that the new field is added and GENDER field is deleted.
|