T.update(P:D )

Read(1078) Label: entity table, update,

Description:

Update records in an entity table.

Syntax:

T.update(P:D)

Note:

The function updates entity table T’s records according to table sequences/record sequences P and/or D; P should have same structure as T.

 

If a key value in P exists in entity table T, update the corresponding T’s record; if a key value in P doesn’t exist in T, append or insert the corresponding P’s record in T.

 

If a key value in D exists in entity table T, delete the corresponding T’s record; parameter D can be omitted.

 

When both parameter P and parameter D are present, the function will first handle P and then D.

 

Ignore parameter D when entity table T doesn’t have dimension. In this case, all records of P will be appended to the end of T.

Parameter:

T

A composite table’s entity table

P

Data to be updated to T, which is a table sequence/record sequence having same structure as T; can be omitted

D

A table sequence/record sequence; delete records from T according to its key values; can be omitted

Option:

@i

Only append or insert non-matching records and ignore the matching ones

@u

Only update the matching records and ignore the non-matching ones

@n

Return updated, inserted and deleted records

@w

When parameter P is a cursor having same order as the original entity table, update the latter’s fields that P has; only valid when T is stored in column-wise format and no records should be added

@y

Keep table sequences/record sequences P and D in the memory and perform the MERGE when computation begins without writing the updated result set to the external memory

Return value:

Entity table

Example: 

Update data in an entity table:

 

A

 

1

=file("emp.ctx")

Generate a composite table file.

2

=A1.create@y(#EID,NAME)

Create the composite table’s base table.

3

=connect("demo").cursor("select EID,NAME from employee where EID<20")

Return a cursor.

4

=A2.append@i(A3)

Append data in A3’s cursor to A2’s base table.

5

=A2.attach(table3,SURNAME)

Add attached table table3 that has two fields – EID, which is the key, and SURNAME, to the base table.

6

=connect("demo").cursor("select EID,SURNAME from employee where EID< 5")

Return a cursor.

7

=A5.append@i(A6)

Append data in A6’s cursor to A5’s pseudo table.

8

=A2.cursor().fetch()

View data in the base table:

9

=A5.cursor().fetch()

View data in the attached table:

10

=create(EID,NAME).record([1,"A",21,"B"])

Return a table sequence.

11

=A2.update(A10)

Update the base table using A10’s table sequence; update the corresponding base table record when a key value of table sequence A10 exists in the base table, and append the corresponding A10’s record to the base table when its key value doesn’t exist in the latter.

12

=A2.cursor().fetch()

View data in the base table again:

13

=create(EID,SURNAME).record([2,"aa",10,"bb"])

Return a table sequence.

14

=A5.update@in(A13)

With @i option, only handles insert and ignore records with same key values; the cooperation with @n option makes the function return only the inserted records.

15

=A5.cursor().fetch()

View data in the attached table again:

16

=create(EID,SURNAME).record([1,"ss",6,"cc"])

Return a table sequence.

17

=A5.update@un(A16)

With @u option, only handles update and ignore records with different key values; the cooperation with @n option makes the function return only the updated records.

18

=A5.cursor().fetch()

View data in the attached table:

 

Delete data from an entity table:

 

A

 

1

=file("empD.ctx")

 

2

=A1.create@y(#EID,NAME)

Create a composite table using EID as the key

3

=connect("demo").cursor("select top 10 EID,NAME from employee")

Return a cursor whose data is as follows:

4

=A2.append@i(A3)

Append data of cursor A3 to entity table A2.

5

=create(EID,NAME).record([10,"bb",11,"cc"])

Generate a table sequence:

6

=create(EID,NAME).record([1,"Rebecca"])

Generate a table sequence:

7

=A2.update@n(A5:A6)

Update entity table A2; when a table sequence A5’s key value exists in A2, update the corresponding record in the latter; otherwise, insert the corresponding record in A5 to A2. Delete a record in A2 when it matches an A6’s key value. @n option works to return the updated, inserted and deleted records.

8

=A2.import()

Now import data from entity table A2 as follows:

 

When there are records having same key values in P and D:

 

A

 

1

=file("empPD.ctx")

 

2

=A1.create@y(#EID,NAME)

Create a composite table using EID as the key.

3

=connect("demo").cursor("select top 10 EID,NAME from employee")

Return a cursor, whose data is as follows:

4

=A2.append@i(A3)

Append data in cursor A3 to entity table A2.

5

=create(EID,NAME).record([1,"bb",2,"cc"])

Generate a table sequence:

6

=create(EID,NAME).record([1,])

Generate a table sequence:

7

=A2.update@n(A5:A6)

Update entity table A2 - append A5’s records to A2 when their key values exist in A2 and when key values of records of A6 exist in A2, delete corresponding records from A2, and use @n option to return updated records, inserted records and deleted records:

8

=A2.import()

As both parameter P and parameter D are present and the function first handles P and then D, the record whose key value is 1 will be deleted in the end; below is the final data in entity table A2:

 

When the entity table doesn’t have dimension:

 

A

 

1

=file("empD.ctx")

 

2

=A1.create@y(EID,NAME)

Create a composite table.

3

=connect("demo").cursor("select top 10 EID,NAME from employee")

Return a cursor whose data is as follows:

4

=A2.append@i(A3)

Append cursor A3’s data to entity table A2.

5

=create(EID,NAME).record([10,"bb"])

Generate a table sequence:

6

=create(EID,NAME).record([1,"Rebecca"])

Generate a table sequence:

7

=A2.update(A5:A6)

As entity table A2 doesn’t have dimension, parameter D (A6) is ignored during the update and append all records of P (A5) to A2.

8

=A2.import()

Now import data from entity table A2 as follows:

 

When using @y option:

 

A

 

1

=file("empD.ctx")

 

2

=A1.create@y(#EID,NAME)

Create a composite table.

3

=connect("demo").cursor("select top 10 EID,NAME from employee")

Return a cursor whose data is as follows:

4

=A2.append@i(A3)

Append cursor A3’s data to entity table A2.

5

=create(EID,NAME).record([1,"Rebecca"])

Generate a table sequence:

6

=A2.update@y(:A5)

Update entity table A2 by deleting records that match A5’s key values; @y option works to keep the update result set in the memory and perform MERGE when computation really begins.

7

=A2.import()

Now import data from entity table A2 as follows:

8

=A2.close()

Close entity table A2.

9

=A1.open().import@x()

Re-open entity table A1 to retrieve data from it; the updated data in A6 has been restored:

 

When using @w option:

 

A

 

1

=demo.cursor("select STOCKID,DATE,CLOSING from STOCKRECORDS  where STOCKID = ? and DATE<?","000062","2015-01-20")

 

2

=file("STOCKRECORDS.ctx")

Create a composite table file.

3

=A2.create@y(#STOCKID,DATE,CLOSING)

Create the composite table’s base table.

4

=A3.append@i(A1)

Append data of A1’s cursor to the composite table’s base table.

5

=A2.open().cursor()@x.fetch()

Fetch data from the composite table as follows:

6

=demo.cursor("select STOCKID,DATE  from STOCKRECORDS  where STOCKID = ?","000792")

 

7

=A2.open()

 

8

=A7.update@w(A6)

Update content of A6’s cursor to composite table STOCKRECORD.ctx, during which @w option is used to rewrite fields A6 has. The process is this: update A6’s STOCKID field and Date field into A7’s composite table and do not change A7’s CLOSING field while keeping the original number of records in A7 without adding any new record to it.

9

=A7.cursor()@x.fetch()

Fetch data from the updated composite table file as follows: