Hi Ankit,
Static SOQL is one which you write in square brackets. It is good to use when you didn’t have any dynamic changes in the SOQL query.
For e.g when the fields names or where conditions is needed to be defined dynamically we didn’t use static SOQL. Then we need to use dynamic SOQL.
________________________________________________________________________________
DYNAMIC SOQL :
public Boolean DynamicSOQL(string user_id, string yourField) {
String query = ‘SELECT ‘ + yourField + ‘ FROM Contact WHERE Id = ‘ + user_id;
Contact[] myContacts = Database.query(query);
if(myContacts.size() < 1){
return false;
}
else{
myContacts[0].put(yourField,false);
Database.update(myContacts[0]);
return true;
}
}
STATIC SOQL:
List<Contact> conList = [SELECT Id, LastName From Contact];
________________________________________________________________________________
One DISADVANTAGE with DYNAMIC SOQL is it causes SOQL injection in where condition which fetching on the basis of some text. To avoid which we need to use String.escapeSingleQuotes. There is no possibility of these in STATIC SOQL.
Thanks.