Skip to main content

Storing Data in Globals

Storing Data in Globals

Storing data in global nodes is simple: you treat a global as you would any other variable. The difference is that operations on globals are automatically written to the database.

Creating Globals

There is no setup work required to create a new global; simply setting data into a global implicitly creates a new global structure. You can create a global (or a global subscript) and place data in it with a single operation, or you can create a global (or subscript) and leave it empty by setting it to the null string. In ObjectScript, these operations are done using the SET command.

The following examples define a global named Color (if one does not already exist) and associate the value “Red” with it. If a global already exists with the name Color, then these examples modify it to contain the new information.

In ObjectScript:

 SET ^Color = "Red"

Storing Data in Global Nodes

To store a value within a global subscript node, simply set the value of the global node as you would any other variable. If the specified node did not previously exist, it is created. If it did exist, its contents are replaced with the new value.

You can store any data in a global node, with the exception that any global node cannot contain a string longer than the string length limit, which is extremely long. See General System Limits.

Setting the value of a global node is an atomic operation: It is guaranteed to succeed and you do not need to use any locks to ensure concurrency.

   SET ^TEST = 2
   SET ^TEST("Color")="Red"
   SET ^TEST(1,1)=100        /* The 2nd-level subscript (1,1) is set
                                to the value 100. No value is stored at
                                the 1st-level subscript (^TEST(1)). */   
   SET ^TEST(^TEST)=10       /* The value of global variable ^TEST 
                                is the name of the subscript. */
   SET ^TEST(a,b)=50         /* The values of local variables a and b
                                are the names of the subscripts. */
   SET ^TEST(a+10)=50       

Also, you can construct global references at runtime using indirection.

FeedbackOpens in a new tab