Skip to content

Get

Executes the specified query and returns the results as a DataTable instance.

1
$Database.Get(query: Query):DataTable

Parameters

Query query
    The query to execute.

Returns

A DataTable instance containing the query results.

Remarks

Without Parameters.TargetDataStore, the proxy resolves the script context's default data store. Repository errors are converted to script errors. The returned table is mutable in memory; call DataTable.Save only when the intended changes should be persisted.

Column values are available as properties of each row, for example row.Name. Date-time columns are returned as DateTimeOffset values.

Examples

Query rows and read values

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
var customers = $Database.Get({
    TargetSchema : 'Sales',
    TargetTable : 'Customers',
    Where : {
        Criteria : [
            { Expression : 'City', Value : $Xml.Evaluate('City') }
        ]
    },
    Order : [
        { Expression : 'Name', Type : 'Ascending' }
    ]
});

customers.Each(function () {
    console.log(this.Id + ': ' + this.Name);
});

Read a single row

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
var result = $Database.Get({
    TargetSchema : 'Sales',
    TargetTable : 'Customers',
    Columns : [
        { Name : 'Name' },
        { Name : 'Email' }
    ],
    Where : {
        Criteria : [
            { Expression : 'Id', Value : $Xml.Evaluate('Customer/Id') }
        ]
    },
    MaxLength : 1
});

if (result.RowCount() === 1) {
    var customer = result.Rows()[0];
    $Xml.SetValue('Customer/Name', customer.Name);
    $Xml.SetValue('Customer/Email', customer.Email);
}

Page through results

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// rows 21-40, newest first
var page = $Database.Get({
    TargetSchema : 'Sales',
    TargetTable : 'Orders',
    Start : 20,
    MaxLength : 20,
    Order : [
        { Expression : 'OrderDate', Type : 'Descending' }
    ]
});

Combine AND and OR conditions with blocks

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// (User.EMailAddress = A OR User.EMailAddress = B) AND User.Disabled != true
var logons = $Database.Get({
    TargetSchema : 'HR',
    TargetTable : 'UserLogons',
    Where : {
        Blocks : [{
            Criteria : [
                { Expression : 'User.EMailAddress', Value : '[email protected]', Condition : 'Or' },
                { Expression : 'User.EMailAddress', Value : '[email protected]' }
            ]
        }],
        Criteria : [
            { Expression : 'User.Disabled', Value : true, Comparison : 'Different' }
        ]
    }
});

Calculate an aggregate value

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
var result = $Database.Get({
    TargetSchema : 'HR',
    TargetTable : 'LeaveAllowance',
    Columns : [
        { Name : 'DaysGained', Expression : 'Sum(DaysGained)' }
    ],
    Where : {
        Criteria : [
            { Expression : 'Employee', Value : $Initiator.Id }
        ]
    }
});

$Xml.SetValue('GeneralInfo/EarnedDays', result.Rows()[0].DaysGained);

Group with expressions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// number of offers per day
var offersByDay = $Database.Get({
    TargetSchema : 'Purchasing',
    TargetTable : 'Offers',
    Columns : [
        { Name : 'Day', Expression : "DateTrunc('Day', OfferDate)" },
        { Name : 'Count', Expression : "Count(DateTrunc('Day', OfferDate))" }
    ],
    Order : [
        { Expression : "DateTrunc('Day', OfferDate)", Type : 'Ascending' }
    ]
});

offersByDay.Each(function () {
    console.info(this.Count + ' offers on ' + this.Day.toISOString());
});

See Also