# Query Builder Result Clause

**URL:** <https://forum.servoy.com/t/query-builder-result-clause/17338>\
**Category:** Classic Servoy\
**Created:** [July 12, 2013, 10:54pm UTC](https://forum.servoy.com/t/query-builder-result-clause/17338 "2013-07-12T22:54:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kwpsd](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kwpsd](https://forum.servoy.com/u/kwpsd)\
**Post date:** [July 12, 2013, 10:54pm UTC](https://forum.servoy.com/t/query-builder-result-clause/17338/1 "2013-07-12T22:54:30Z")

</div>

Version: 6.1.4 - build 1429

I am attempting my first use of the Query Builder and need help replicating the following SQL statement:

> SELECT index, mailcode FROM eom WHERE period = ?

Simple, eh?

Here is the initial Query Builder code:

```auto
    var query = databaseManager.createSelect( 'db:/ucsd/eom' )

        query.where.add( query.columns.charge_period.eq( query.getParameter( 'period' ) ) )
        query.params['period'] = period

    var fs = databaseManager.getFoundSet( 'db:/ucsd/eom' )

    fs.loadRecords( query )

```

and, it loads all records for the given period. Now, I want to select a specific column:

```auto
    var query = databaseManager.createSelect( 'db:/ucsd/eom' )

        query.result.add( query.columns.index )
        query.where.add( query.columns.charge_period.eq( query.getParameter( 'period' ) ) )
        query.params['period'] = period

    var fs = databaseManager.getFoundSet( 'db:/ucsd/eom' )

    fs.loadRecords( query )

```

and, it returns a single column of data for the period as expected. Now, I want to select two specific columns:

```auto
    var query = databaseManager.createSelect( 'db:/ucsd/eom' )

        query.result.add( query.columns.index )
        query.result.add( query.columns.mailcode )
        query.where.add( query.columns.charge_period.eq( query.getParameter( 'period' ) ) )
        query.params['period'] = period

    var fs = databaseManager.getFoundSet( 'db:/ucsd/eom' )

    fs.loadRecords( query )

```

and, this is where the problem arises. The following error is emitted:

[attachment=0]QueryBuilderErrorMessage.png[/attachment]

I have tried re-writing the ‘query.result’ in two different ways:

```auto
        query.result
            .add( query.columns.index )
            .add( query.columns.mailcode )

```

and as:

```auto
query.result.add( query.columns.index ).add( query.columns.mailcode )

```

but, they all produce the same error.

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![rgansevles](https://avatars.discourse-cdn.com/v4/letter/r/3d9bf3/32.png) [@rgansevles](https://forum.servoy.com/u/rgansevles)\
**Post date:** [July 14, 2013, 11:15am UTC](https://forum.servoy.com/t/query-builder-result-clause/17338/2 "2013-07-14T11:15:48Z")

</div>

Kim,

Servoy always loads data in a 2-steps:

1. select pk from tab where ……
2. select column-data from tab where pk in (?. ?, …, ?)

A foundset query, either custom sql or QueryBuilder, must always be the pk-query.  
In your first try, you did not specify anything in the result clause, in that case Servoy defaults to the pk(s) of the table.

Just load the foundset with pk (or let Servoy default to pk columns) and use columns in the foundset records, they will be loaded in step 2.

Rob

---

<div class="post-metadata">

**Author:** ![kwpsd](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kwpsd](https://forum.servoy.com/u/kwpsd)\
**Post date:** [July 22, 2013, 3:06am UTC](https://forum.servoy.com/t/query-builder-result-clause/17338/3 "2013-07-22T03:06:48Z")

</div>

Thanks for responding, Rob, and for the explanation.
