+ Start a Discussion
MycodexMycodex 

difference between storing SOQL results in list vs sObject

 So what is the different? To illustrate my point, here are both way returning a similar set.

 

 #1

Set<String> nameSet = new Set<String>(); List<Account> acct = [SELECT Name FROM Account]; for(Account a_insert: acct){ nameSet.add(a_insert.Name); }

 

#2

Set<String> nameSet = new Set<String>(); for(Account acct : [SELECT Name FROM Account]){ nameSet.add(Name); }

 

Would I hit a limit earlier with #2 since I am storing the results in the sObject instead a list?




 

 

jhartfieldjhartfield

There is nothing different between the two.  The limits are the same and everything.  #2 is just a sort of short-cut.

Anand@SAASAnand@SAAS

Until Spring'10 release(latest release), Collections had a 1000 limit. So #1 would run into errors when your SOQL returned more than 1000 records. With the latest release this limit has been removed. However, storing the results of your SOQL in a collection and then iterating over it is inefficient. #2 is the preferred route for two reasons:

 

1. You can potentially iterate over upto 10000 records without hitting any limits

2. It's much more efficient from governor limit standpoint.