# Load Records

**URL:** <https://forum.servoy.com/t/load-records/9981>\
**Category:** Classic Servoy\
**Created:** [December 5, 2008, 3:47pm UTC](https://forum.servoy.com/t/load-records/9981 "2008-12-05T15:47:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tommygill](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@tommygill](https://forum.servoy.com/u/tommygill)\
**Post date:** [December 5, 2008, 3:47pm UTC](https://forum.servoy.com/t/load-records/9981/1 "2008-12-05T15:47:41Z")

</div>

I would like to load a dataset that queries a column that is not the pk.

This is the method

```auto
var query = "SELECT DISTINCT totals_query FROM donation_detail WHERE detail_date = '" + utils.dateFormat(donation_date, 'MM/dd/yyyy') + "'"
var dataset = databaseManager.getDataSetByQuery(controller.getServerName(), query, null, -1);
var recCount = dataset.getMaxRowIndex()

forms.Enter_Donation_Detail.controller.loadRecords(dataset)
forms.rpt_Current_Totals.controller.showPrintPreview()

```

I get the following error message

> Error loading primary key data, for input string:

Is there a way to return the pk’s fo the dataset

Thanks

---

<div class="post-metadata">

**Author:** ![tommygill](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@tommygill](https://forum.servoy.com/u/tommygill)\
**Post date:** [December 5, 2008, 11:54pm UTC](https://forum.servoy.com/t/load-records/9981/2 "2008-12-05T23:54:51Z")

</div>

Is it possible to convert a dataset to a foundset?

Thanks

---

<div class="post-metadata">

**Author:** ![popdizzle](https://avatars.discourse-cdn.com/v4/letter/p/5e9695/32.png) [@popdizzle](https://forum.servoy.com/u/popdizzle)\
**Post date:** [December 6, 2008, 1:59am UTC](https://forum.servoy.com/t/load-records/9981/3 "2008-12-06T01:59:31Z")

</div>

Couple of things…

-Make sure your date format matches what your database is expecting.  
-Consider using a ‘?’ in place of your date string within the query, then use an array to pass the date value. This adds a little security to your system (to avoid sql injections) and also makes it a little easier when you don’t have to figure out the matching quotes.

Consider rewriting like this, using one dataset to get the results you want (distinct totals\_query) and another dataset to get the PK’s for those same records:

```auto
// This query will get the records you want in your dataset
var query = "SELECT DISTINCT totals_query FROM donation_detail WHERE detail_date = ?"

// This query will return the PK's of the records you're wanting
// which will pass to .loadRecords() below
var query_pk = "select pk from donation_detail where exists " +
"(SELECT DISTINCT totals_query FROM donation_detail WHERE detail_date = ?)"

var args = new Array()
args[0] = utils.dateFormat(donation_date, 'MM/dd/yyyy')

var dataset = databaseManager.getDataSetByQuery(controller.getServerName(), query, args, -1);
var ds_pk = databaseManager.getDataSetByQuery(controller.getServerName(), query_pk, args, -1);
var recCount = dataset.getMaxRowIndex()

// Load the desired records into a form's foundset using the PK's from ds_pk
forms.Enter_Donation_Detail.controller.loadRecords(ds_pk)
// put some code here to do what you want with dataset

```

Hope that helps.

---

<div class="post-metadata">

**Author:** ![tommygill](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@tommygill](https://forum.servoy.com/u/tommygill)\
**Post date:** [December 6, 2008, 2:46am UTC](https://forum.servoy.com/t/load-records/9981/4 "2008-12-06T02:46:10Z")

</div>

Thanks Loads Jason

I’m still green at this sql stuff. I wasn’t aware that you could nest one query inside of another. Interesting.

For test purposes I have six records in the “donation\_detail” table. Five of the six have unique data in the “totals\_query” column. The sixth is a duplicate. The “dataset” returns five records. The “ds\_pk” returns six records. When these records are loaded it still shows all six records instead of constraining the list to only the unique records.

Also, would you please explain what “exist” does in the “query\_pk”.

If you have any more suggestions or help it would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![popdizzle](https://avatars.discourse-cdn.com/v4/letter/p/5e9695/32.png) [@popdizzle](https://forum.servoy.com/u/popdizzle)\
**Post date:** [December 6, 2008, 4:01am UTC](https://forum.servoy.com/t/load-records/9981/5 "2008-12-06T04:01:02Z")

</div>

I could try to explain the WHERE EXISTS clause, but this link will do it more justice:

> **[MySQL :: MySQL 8.4 Reference Manual :: 15.2.15.6 Subqueries with EXISTS or...](https://dev.mysql.com/doc/refman/8.4/en/exists-and-not-exists-subqueries.html)**

I thought that query would work off the top of my head, but forgot that EXISTS ignores the field list in the sub-select statement, so it didn’t do what I originally thought. Try this code to see if it gets you the records you’re looking for. I tested this one and it seemed to work, if I understand what you want.

This one groups the records by totals\_query, then gets the smallest PK value out of each group, thus returning the equivalent of the DISTINCT statement.

See below:

```auto
    // This query will get the records you want in your dataset
    var query = "SELECT DISTINCT totals_query FROM donation_detail WHERE detail_date = ?"

    // This query will return the PK's of the records you're wanting
    // which will pass to .loadRecords() below
    var query_pk = "select pk from donation_detail where pk in " +
    "(SELECT min(pk) FROM donation_detail WHERE detail_date = ? group by totals_query)"

    var args = new Array()
    args[0] = utils.dateFormat(donation_date, 'MM/dd/yyyy')

    var dataset = databaseManager.getDataSetByQuery(controller.getServerName(), query, args, -1);
    var ds_pk = databaseManager.getDataSetByQuery(controller.getServerName(), query_pk, args, -1);
    var recCount = dataset.getMaxRowIndex()

    // Load the desired records into a form's foundset using the PK's from ds_pk
    forms.Enter_Donation_Detail.controller.loadRecords(ds_pk)
    // put some code here to do what you want with dataset

```

Let me know if it works okay.

---

<div class="post-metadata">

**Author:** ![tommygill](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@tommygill](https://forum.servoy.com/u/tommygill)\
**Post date:** [December 6, 2008, 1:30pm UTC](https://forum.servoy.com/t/load-records/9981/6 "2008-12-06T13:30:50Z")

</div>

Sweet!

What a solution. Thanks Jason. Everything looks great.

Just one more question. Can I add a sort to the end of the sql statement?

---

<div class="post-metadata">

**Author:** ![popdizzle](https://avatars.discourse-cdn.com/v4/letter/p/5e9695/32.png) [@popdizzle](https://forum.servoy.com/u/popdizzle)\
**Post date:** [December 6, 2008, 2:32pm UTC](https://forum.servoy.com/t/load-records/9981/7 "2008-12-06T14:32:10Z")

</div>

> Can I add a sort to the end of the sql statement?

Yes. Just put “order by asc” or “order by desc” at the very end of the query, outside the subquery.

Like so:

```auto
var query_pk = "select pk from donation_detail where pk in " +
    "(SELECT min(pk) FROM donation_detail WHERE detail_date = ? group by totals_query) " +
     "order by totals_query asc"

```

Using SQL is my favorite way to find records if there’s any complexity in the search. You can do much more with SQL than you can with FOUNDSET.find() / FOUNDSET.search().

A few weeks spent learning JOINS and other more complex SQL is time very well spent in Servoy.

Enjoy!

---

<div class="post-metadata">

**Author:** ![tommygill](https://avatars.discourse-cdn.com/v4/letter/t/aeb1de/32.png) [@tommygill](https://forum.servoy.com/u/tommygill)\
**Post date:** [December 6, 2008, 6:26pm UTC](https://forum.servoy.com/t/load-records/9981/8 "2008-12-06T18:26:42Z")

</div>

Thanks Jason

This has been a very fruitful and enjoyable conversation as well as pointing me in the direction that I wanted to go anyway.

Maybe as more challenges arise i can present them with the hope that we can have another one of these enlighting conversation.

Thanks again

---

<div class="post-metadata">

**Author:** ![popdizzle](https://avatars.discourse-cdn.com/v4/letter/p/5e9695/32.png) [@popdizzle](https://forum.servoy.com/u/popdizzle)\
**Post date:** [December 8, 2008, 2:41am UTC](https://forum.servoy.com/t/load-records/9981/9 "2008-12-08T02:41:59Z")

</div>

Glad I could help. Don’t know where I’d be without the forums!
