So we all know we can put an aggregate directly into an Integer, like this:

Integer countAccounts = [SELECT COUNT() FROM Account];

Is it possible to do the same thing, using Database.query()?

Integer countAccounts = Database.query('SELECT COUNT() FROM Account');

If so - how?

1 Answer 1


Yes. You can query using Database.countQuery(query)

String QueryString = 'SELECT count() FROM Account'; 
Integer countAccounts  = Database.countQuery(QueryString);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .