You need to sign in to do that
Don't have an account?
Difference between database.query and SOQL statement?
Seems to me that the following achomplish the same thing:
List<Account> aa = [SELECT Id, Name FROM Account WHERE Name = 'Acme'];
List<Account> aa = databse.query('SELECT Id, Name FROM Account WHERE Name = 'Acme'');
What are the advantages of using one over the other?
List<Account> aa = [SELECT Id, Name FROM Account WHERE Name = 'Acme'];
List<Account> aa = databse.query('SELECT Id, Name FROM Account WHERE Name = 'Acme'');
What are the advantages of using one over the other?
Having said that, you should prefer the SOQL query whenever possible. When you use a dynamic query, Salesforce doesn't know what the query is until your code actually runs, and it can't prevent you (or someone not familiar with your code) from accidentally deleting fields needed in a query. If you use a hard-coded query, Salesforce will track those fields, making your code more robust.
Basically, we need database.query, whenever we need to make a dynamic SOQL statement .
egample:
1. I'm creating a managed package and don't want to take an explicit dependency on a feature. E.g. List<sobject> revenueSchedules = Database.query('Select Id from OpportunityLineItemSchedule where OpportunityLineItemId in :oliIds'); If I did this in static SOQL all the installers of the managed package would need Product Schedules enabled.
2. I want to dynamically include an optional field or filter in the SOQL query. A custom setting may define the name of a field that should be used to populate something else. As it isn't known at "compile" time it needs to be dynamic.